Microcontrollers
Download
1 / 212

Microcontrollers ... - PowerPoint PPT Presentation


  • 80 Views
  • Uploaded on

Microcontrollers. A microcontroller often serves as the “brain” of a system. Like a self-contained computer, it can be programmed to interact with both the hardware of the system and the user.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Microcontrollers ...' - jalena


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Microcontrollers
Microcontrollers ...

  • A microcontroller often serves as the “brain” of a system.

  • Like a self-contained computer, it can be programmed to interact with both the hardware of the system and the user.

  • Even the most basic microcontroller can perform simple math operations, control digital outputs, and monitor digital inputs.

  • As the computer industry has evolved, so has the technology associated with microcontrollers.


Microcontrollers1
Microcontrollers ...

  • Newer microcontrollers are much faster, have more memory, and have a host of input and output features that dwarf the ability of earlier models.

  • Most modern controllers have analog-to-digital converters, high-speed timers and counters, interrupt capabilities, outputs that can be pulse-width modulated, serial communication ports, etc.


Microcontrollers2
Microcontrollers ...

  • There are a number of popular families of microcontrollers which are used in different applications as per their capability and feasibility to perform the desired task

  • most common of these are

    • Intel’s 8051,

    • Atmel’s AVR and

    • General Instruments’ PIC microcontrollers.


Microcontrollers3
Microcontrollers ...

  • In this class we will introduce you with AVR family of microcontrollers.

  • We will study one of Atmel’s AVR 8-Bit microcontrollers --- ATMega16


Resources and references
Resources and References

  • http://en.wikipedia.org/wiki/Atmel_AVR

  • www.atmel.com

  • http://www.atmel.com/Images/2466S.pdf

  • www.microchip.com

  • www.alldatasheet.com

  • www.avrfreaks.net


Avr microcontrollers
AVR microcontrollers

  • AVR microcontrollers are available in three categories:

    1. TinyAVR – Less memory, small size, suitable only for simpler applications

    2. MegaAVR – These are the most popular ones having good amount of memory (up to 256 KB), higher number of inbuilt peripherals and suitable for moderate to complex applications.

    3. XmegaAVR – Used commercially for complex applications, which require large program memory and high speed.


Avr features
AVR Features

  • The AVR --- a 8-bitRISC single chip microcontroller developed by Atmel in 1996 --- uses modified Harvard architecture .

    • In Harvard architecture program and data are stored in separate physical memory systems that appear in different address spaces


Avr features1
AVR Features ...

  • The AVR was one of the first microcontroller families to use on-chip flash memory for program storage, as opposed to one-time programmable ROM, EPROM, or EEPROM used by other microcontrollers at the time.


What does avr risc mean
What does AVR RISC mean?

  • Atmel says that the name AVR is not an acronym and does not stand for anything in particular.

  • The creators of the AVR give no definitive answer as to what the term "AVR" stands for.


What does avr risc mean1
What does AVR RISC mean? ....

  • However, some says the acronym AVR stands for: Advanced Virtual RISC

  • Though, it is commonly accepted that AVR stands for Alf (Egil Bogen) and Vegard (Wollan)'s RISC processor after the names who designed the basic architecture at the Norwegian Institute of Technology when they were student.



Atmega16 features memory
ATMega16 Features (memory)

  • High Endurance Non-volatile Memory segments

    • 16 Kbytes of In-System Self-programmable Flash program memory

    • 512 Bytes EEPROM

    • 1 Kbyte Internal SRAM

    • Write/Erase Cycles: 10,000 Flash/100,000 EEPROM

    • Data retention: 20 years at 85°C/100 years at 25°C


Atmega16 features
ATMega16 Features ...

  • 32 8-bit General Purpose Working Registers

  • 32 Programmable I/O Lines

  • On-chip 2-cycle Multiplier

  • 8-channel, 10-bit ADC

  • Two 8-bit Timer/Counters with Separate Prescaler and Compare Modes

  • One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode


Atmega16 features1
ATMega16 Features ...

  • Real Time Counter with Separate Oscillator

  • Four PWM Channels

  • Programmable Serial USART

  • Master/Slave SPI Serial Interface

  • Digital to Analog Comparator.


Atmega16 features2
ATMega16 Features ...

  • 131 Powerful Instructions – Most Single-clock Cycle Execution

  • Up to 16 MIPS Throughput at 16 MHz

  • True Read-While-Write Operation


Atmega16 features3
ATMega16 Features ...

  • Fully Static Operation

  • In-System Programming (ISP) by On-chip Boot Program

  • Programming Lock for Software Security

  • 8 Single-ended Channels

  • Byte-oriented Two-wire Serial Interface


Atmega16 features4
ATMega16 Features ...

  • Special Microcontroller Features

    • Power-on Reset and Programmable Brown-out Detection

    • Internal Calibrated RC Oscillator

    • External and Internal Interrupt Sources

    • Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-down, Standby and Extended Standby


Atmega16 features5
ATMega16 Features ...

  • Available in 40-Pin DIP

  • Operating Voltages

    • 4.5V - 5.5V

  • Speed Grades

    • 0 - 16 MHz


  • The most significant difference amongst different ATmega is the amount of “flash” storage space in the chip

    • which is roughly analogous to a hard drive in a PC (room for programs).

  • The amounts are 8kB, 16kB and 32kB.

  • Here’s the rub, if you don’t exceed the flash size limitation, the difference is utterly useless!

  • There will be no difference in execution speed, or anything else, if your code fits into a smaller size.


Pin diagram description
Pin Diagram & Description the amount of “flash” storage space in the chip


Pin descriptions
Pin Descriptions the amount of “flash” storage space in the chip

  • Notice that most of the pins have alternate functions (shown in parentheses).

  • It can be found in a pdf format on Atmel’s website (www.atmel.com), AVRFreaks (http://www.avrfreaks.net/), or by searching the web.


Pin descriptions1
Pin Descriptions ... the amount of “flash” storage space in the chip

  • GND: Ground (0 V). Note there are 2 ground Pins.

  • VCC: Digital supply voltage. (+5V)

  • AVCC: AVCC is the supply voltage pin for Port A and the A/D Converter.

    • It should be externally connected to VCC, even if the ADC is not used.

    • If the ADC is used, it should be connected to VCC through a low-pass filter.

  • AREF: AREF is the analog reference pin for the A/D Converter.


Pin descriptions2
Pin Descriptions ... the amount of “flash” storage space in the chip

  • XTAL1 : External oscillator pin 1

  • XTAL2 : External oscillator pin 2


Pin descriptions port a pa7 pa0
Pin Descriptions ... Port the amount of “flash” storage space in the chipA (PA7 -PA0)

  • Port A serves as the analog inputs to the A/D Converter.

  • Port A also serves as an 8-bit bi-directional I/O port, if the A/D Converter is not used.

  • When pins PA0 to PA7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated.

  • The Port A pins are tri-stated when a reset condition becomes active, even if the clock is not running.


Pin descriptions port b pb7 pb0
Pin Descriptions ... Port B the amount of “flash” storage space in the chip(PB7 -PB0)

  • Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit).

  • Port B also serves the functions of various special features of the ATmega16 as listed on page 58 of datasheet.


Pin descriptions port c pc7 pc0
Pin Descriptions ... Port C the amount of “flash” storage space in the chip(PC7 -PC0)

  • Port C is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit).

  • Port C also serves the functions of the JTAG interface and other special features of the ATmega16 as listed on page 61 of datasheet.

  • If the JTAG interface is enabled, the pull-up resistors on pins PC5 (TDI), PC3 (TMS) and PC2 (TCK) will be activated even if a reset occurs.


Pin descriptions port d pd7 pd0
Pin Descriptions ... Port D the amount of “flash” storage space in the chip(PD7 -PD0)

  • Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit).

  • Port D also serves the functions of various special features of the ATmega16 as listed on page 63 of datasheet.


Block diagram description
Block Diagram & Description the amount of “flash” storage space in the chip


Architecture diagram of atmega16
Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip

?


Oscillator
Oscillator the amount of “flash” storage space in the chip

  • The ATMega16 can use an internal or external clock signal

  • Clock signals are usually generated by an RC oscillator or a crystal

  • By default Atmega16 is set to operate at internal calibrated oscillator of 1 MHz.

  • The internal clock is an RC oscillator programmable to 1, 2, 4, or 8 MHz

  • The maximum frequency of internal oscillator is 8Mhz.


Oscillator1
Oscillator the amount of “flash” storage space in the chip

  • Alternatively, ATmega16 can be operated using an external crystal oscillator with a maximum frequency of 16MHz.

  • An external clock signal (crystal controlled) can be more precise for time critical applications

  • In this case you need to modify the fuse bits. (Fuse Bits will be explained later).


Architecture diagram of atmega161

the amount of “flash” storage space in the chip

Architecture Diagram of Atmega16

?


Avr cpu
AVR CPU the amount of “flash” storage space in the chip

  • PC: Program Counter

    • address of next instruction

  • IR: Instruction Register

    • pre-fetched instruction

  • ID: Instruction decoder

    • current instruction

  • GPR: R0-R31

  • ALU


Avr cpu1
AVR CPU the amount of “flash” storage space in the chip

  • In order to maximize performance and parallelism, the AVR uses a Harvard architecture

    • with separate memories and buses for program and data.

  • Instructions in the program memory are executed with a single level pipelining.

    • While one instruction is being executed, the next instruction is pre-fetched from the program memory.

  • This concept enables instructions to be executed in every clock cycle.


Avr cpu2
AVR CPU the amount of “flash” storage space in the chip

  • The fast-access Register File contains 32 × 8-bit general purpose working registers with a single clock cycle access time.

  • This allows single-cycle ALU operation.

  • In a typical ALU operation, two operands are output from the Register File,

  • The operation is executed, and the result is stored back in the Register File - in one clock cycle.


Avr cpu3
AVR CPU the amount of “flash” storage space in the chip

  • Six of the 32 registers can be used as three 16-bit indirect address register pointers for Data Space addressing

    • enabling efficient address calculations.

  • One of the these address pointers can also be used as an address pointer for look up tables in Flash Program memory.

  • These added function registers are the 16-bit X-register, Y-register, and Z-register


Avr cpu4
AVR CPU the amount of “flash” storage space in the chip

  • The ALU supports arithmetic and logic operations between registers or between a constant and a register.

  • Single register operations can also be executed in the ALU.

  • After an arithmetic operation, the Status Register is updated to reflect information about the result of the operation.


Avr cpu5
AVR CPU the amount of “flash” storage space in the chip

  • Program flow is provided by conditional and unconditional jump and call instructions

    • able to directly address the whole address space.

  • Most AVR instructions have a single 16-bit word format.

  • Every program memory address contains a 16-bit or 32-bit instruction.


Avr cpu alu
AVR CPU --- the amount of “flash” storage space in the chipALU

  • The high-performance AVR ALU operates in direct connection with all the 32 general purpose working registers.

  • Within a single clock cycle, arithmetic operations between general purpose registers or between a register and an immediate are executed.

  • The ALU operations are divided into three main categories - arithmetic, logical, and bit-functions.


Avr cpu status register
AVR CPU --- the amount of “flash” storage space in the chipStatus Register

  • The Status Register contains information about the result of the most recently executed arithmetic instruction.

  • This information can be used for altering program flow in order to perform conditional operations.


Avr cpu status register1
AVR CPU --- the amount of “flash” storage space in the chipStatus Register

  • Note that the Status Register is updated after all ALU operations, as specified in the Instruction Set Reference.

  • This will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code.

  • The Status Register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. This must be handled by software.


Avr cpu status register2
AVR CPU --- the amount of “flash” storage space in the chipStatus Register

  • • Bit 0 - C: Carry Flag

    • The Carry Flag C indicates a carry in an arithmetic or logic operation.

  • • Bit 1 - Z: Zero Flag

    • The Zero Flag Z indicates a zero result in an arithmetic or logic operation.


Avr cpu status register3
AVR CPU --- the amount of “flash” storage space in the chipStatus Register

  • Bit 2 - N: Negative Flag

    The Negative Flag N indicates a negative result in an arithmetic or logic operation.

  • Bit 3 - V: Two's Complement Overflow Flag

    The Two's Complement Overflow Flag V supports two's complement arithmetic

  • Bit 4 - S: Sign Bit, S = N V

    The S-bit is always an exclusive-OR between the Negative Flag N and the Two's Complement Overflow Flag V


Avr cpu status register4
AVR CPU --- the amount of “flash” storage space in the chipStatus Register

  • Bit 5 - H: Half Carry Flag

    The Half Carry Flag H indicates a Half Carry in some arithmetic operations. Half Carry is useful in BCD arithmetic

  • Bit 6 - T: Bit Copy Storage

    The Bit Copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T-bit as source or destination for the operated bit. A bit from a register in the Register File can be copied into T by the BST instruction, and a bit in T can be copied into a bit in a register in the Register File by the BLD instruction


Avr cpu status register5
AVR CPU --- the amount of “flash” storage space in the chipStatus Register

  • Bit 7 - I: Global Interrupt Enable

    • The Global Interrupt Enable bit must be set for the interrupts to be enabled.

    • The individual interrupt enable control is then performed in separate control registers.

    • If the Global Interrupt Enable Register is cleared, none of the interrupts are enabled independent of the individual interrupt enable settings.


Avr cpu status register6
AVR CPU --- the amount of “flash” storage space in the chipStatus Register

  • The I-bit is cleared by hardware after an interrupt has occurred, and is set by the RETI instruction to enable subsequent interrupts.

  • The I-bit can also be set and cleared by the application with the SEI and CLI instructions

  • Finally, you must set the I bit in SREG enabling internal and external interrupts that have been set to operate. To stop all interrupts, just set SREG_I to 0.


Architecture diagram of atmega162

the amount of “flash” storage space in the chip

Architecture Diagram of Atmega16


Memory
Memory the amount of “flash” storage space in the chip

Atmega16 consist of three different memory sections: 

  • Flash : Machine instructions go here

  • EEPROM : Secondary storage, Byte Addressable

  • SRAM : For runtime data

  • EEPROM and Flash memories have a limited lifetime of erase/write cycles


  • Memory flash
    Memory ... ( the amount of “flash” storage space in the chipFlash)

    • flash memory is used to store the program dumped or burnt by the user on to the microcontroller.

    • It can be easily erased electrically as a single unit.

    • Flash memory is non-volatile i.e., it retains the program even if the power is cut-off.

    • Atmega16 is available with 16KB of in system programmable Flash.


    Memory flash1
    Memory ... ( the amount of “flash” storage space in the chipFlash)

    • Programs reside in word addressable flash storage

    • Word addresses range from 0000-1FFF

    • Byte addresses range 0000-3FFF

    • Violating the true spirit of Harvard Architecture, in AVR it is possible to use this storage area for constant data as well as instructions

    • Instructions are 16 or 32-bits

    • Most are 16-bits and are executed in a single clock cycle


    Memory sram
    Memory ... ( the amount of “flash” storage space in the chipSRAM)

    • Static Random Access Memory, this is the volatile memory of microcontroller i.e., data is lost as soon as power is turned off.

    • The ATMega16 has 1K (1024 bytes) of byte addressable static RAM

    • A small portion of SRAM is set aside for general purpose registers used by CPU and some for the peripheral subsystems of the microcontroller.


    Memory sram1
    Memory ... ( the amount of “flash” storage space in the chipSRAM)

    • This is used for variable storage and stack space during execution

    • SRAM addresses start at $0060 and go through $045F

    • The reason for not starting at zero will be covered later


    Memory eeprom
    Memory ... ( the amount of “flash” storage space in the chipEEPROM)

    • This is also a nonvolatile memory used to store data like values of certain variables.

    • Typically reserved for variables that must retain their value in the event of a shutdown (e.g., system calibration data unique to each board)

      • Slow speed writing (1 millisecond for 1 byte of memory)

      • Limited number of write cycles

  • Atmega16 has 512 bytes of EEPROM

  • They are byte addressable


  • Architecture diagram of atmega163
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    Timers counters
    Timers/Counters the amount of “flash” storage space in the chip

    • Most commonly used complex peripheral

    • Atmega16 consists of two 8-bit and one 16-bit timer/counter.

    • Think of them as binary up-counters

      • In timing mode, count time periods

      • In counting mode, counting events or pulses

    • Timers are useful for generating precision actions e.g., for creating time delays between two operations.


    Architecture diagram of atmega164
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    Usart
    USART the amount of “flash” storage space in the chip

    • Universal Synchronous and Asynchronous Receiver and Transmitter interface is available for interfacing with external device capable of communicating serially (data transmission bit by bit).


    Architecture diagram of atmega165
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    Two wire interface twi
    Two Wire Interface the amount of “flash” storage space in the chip (TWI)

    • TWI can be used to set up a network of devices

      • many devices can be connected over TWI interface forming a network

      • the devices can simultaneously transmit and receive and have their own unique address.


    Architecture diagram of atmega166
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    Adc interface
    ADC Interface the amount of “flash” storage space in the chip

    • Atmega16 is equipped with an 8 channel ADC (Analog to Digital Converter) with a resolution of 10-bits.

    • ADC reads the analog input for e.g., a sensor input and converts it into digital information which is understandable by the microcontroller.


    Architecture diagram of atmega167
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    I o ports
    I/O Ports the amount of “flash” storage space in the chip

    • Atmega16 has four (PORTA, PORTB, PORTC and PORTD) 8-bit input-output ports.


    Architecture diagram of atmega168
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    Watchdog timer
    Watchdog Timer the amount of “flash” storage space in the chip

    • Watchdog timer is present with internal oscillator.

    • Watchdog timer continuously monitors and resets the controller if the code gets stuck at any execution action for more than a defined time interval.


    Architecture diagram of atmega169
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    Serial peripheral interface spi
    Serial Peripheral Interface the amount of “flash” storage space in the chip(SPI)

    • SPI port is used for serial communication between two devices on a common clock source.

    • The data transmission rate of SPI is more than that of USART.


    Architecture diagram of atmega1610
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    In system programmable isp
    In System Programmable the amount of “flash” storage space in the chip(ISP)

    • AVR family of controllers have In System Programmable Flash Memory which can be programmed without removing the IC from the circuit

    • ISP allows to reprogram the controller while it is in the application circuit.


    Architecture diagram of atmega1611
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    Interrupts
    Interrupts the amount of “flash” storage space in the chip

    • Atmega16 consists of 21 interrupt sources out of which four are external.

    • The remaining are internal interrupts which support the peripherals like USART, ADC, Timers etc.


    Architecture diagram of atmega1612
    Architecture Diagram of Atmega16 the amount of “flash” storage space in the chip


    General Purpose Registers the amount of “flash” storage space in the chip


    General purpose registers
    General Purpose Registers the amount of “flash” storage space in the chip

    • Atmega16 is equipped with 32 general purpose registers which are coupled directly with the Arithmetic Logical Unit (ALU) of CPU.

    • In most variants of the AVR architecture,

      • the working registers are mapped in as the first 32 memory addresses (000016–001F16)

      • followed by the 64 I/O registers (002016–005F16).


    General purpose registers1
    General Purpose Registers... the amount of “flash” storage space in the chip

    • Actual SRAM starts after these register sections (from address 006016).

    • Note that the I/O register space may be larger on some more extensive devices, in which case the memory mapped I/O registers will occupy a portion of the SRAM address space.


    General purpose registers2
    General Purpose Registers... the amount of “flash” storage space in the chip

    • Even though there are separate addressing schemes and optimized opcodes for register file and I/O register access, all can still be addressed and manipulated as if they were in SRAM.

    • Most of the instructions operating on the Register File have direct access to all registers, and most of them are single cycle instructions.


    General purpose registers3
    General Purpose Registers... the amount of “flash” storage space in the chip


    General purpose registers4
    General Purpose Registers... the amount of “flash” storage space in the chip

    • As shown, each register is also assigned a data memory address, mapping them directly into the first 32 locations of the user Data Space.

    • Although not being physically implemented as SRAM locations, this memory organization provides great flexibility in access of the registers, as the X-, Y-, and Z-pointer Registers can be set to index any register in the file.


    The x register y register and z register
    The X-register, Y-register and Z-register the amount of “flash” storage space in the chip

    • The registers R26..R31 have some added functions to their general purpose usage.

      • These registers are 16-bit address pointers for indirect addressing of the Data Space.

      • The three indirect address registers X, Y, and Z are defined as described in following slide

    • In the different addressing modes these address registers have functions as fixed displacement, automatic increment, and automatic decrement (see the Instruction Set Reference for details).


    The x register y register and z register1
    The X-register, Y-register and Z-register the amount of “flash” storage space in the chip

    15 XH XL 0

    • X - register

      R27 ($1B) R26 ($1A)

      15 YH YL 0

    • Y - register

      R29 ($1D) R28 ($1C)

      15 ZH ZL 0

    • Z - register

      R31 ($1F) R30 ($1E)


    Data addressing modes
    Data Addressing modes the amount of “flash” storage space in the chip


    Data addressing modes1
    Data Addressing modes the amount of “flash” storage space in the chip

    • The five different addressing modes are :

      • Direct,

      • Indirect,

      • Indirect with Displacement,

      • Indirect with Pre-decrement, and

      • Indirect with Post-increment.

  • The direct addressing reaches the entire data space.

  • Registers R26 to R31 feature the indirect addressing pointer registers.


  • Data addressing modes2
    Data Addressing modes... the amount of “flash” storage space in the chip

    • The Indirect with Displacement mode reaches 63 address locations from the base address given by the Y-register or Z-register.

    • When using register indirect addressing modes with automatic pre-decrement and post-increment, the address registers X, Y, and Z are decremented or incremented.

    • The 32 general purpose working registers, 64 I/O Registers, and the 1024 bytes of internal data SRAM in the ATmega16 are all accessible through all these addressing modes.


    I o memory

    I/O Memory the amount of “flash” storage space in the chip


    I o memory1
    I/O Memory the amount of “flash” storage space in the chip

    • All ATmega16 I/Os and peripherals are placed in the I/O space.

    • The I/O locations are accessed by the IN and OUT instructions, transferring data between the 32 general purpose working registers and the I/O space.

    • I/O Registers within the address range $00 - $1F are directly bit- accessible using the SBI and CBI instructions.


    I o memory2
    I/O Memory the amount of “flash” storage space in the chip

    • In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. (Refer to the Instruction Set section of the data sheet for more details.)

    • When using the I/O specific commands IN and OUT, the I/O addresses $00 - $3F must be used.

    • When addressing I/O Registers as data space using LD and ST instructions, $20 must be added to these addresses.


    I o ports1
    I/O Ports the amount of “flash” storage space in the chip


    I o ports2
    I/O Ports the amount of “flash” storage space in the chip

    • Atmega16 has four (PORTA, PORTB, PORTC and PORTD) 8-bit input-output ports.

    • All ports initially set to input


    I o ports introduction
    I/O Ports Introduction the amount of “flash” storage space in the chip

    • All AVR ports have true Read-Modify-Write functionality when used as general digital I/O ports.

    • This means that the direction of one port pin can be changed without changing the direction of any other pin

    • The same applies when changing drive value (if configured as output) or enabling/disabling of pull-up resistors (if configured as input).


    I o ports introduction1
    I/O Ports Introduction... the amount of “flash” storage space in the chip

    • The pin driver is strong enough to drive LED displays directly.

    • All port pins have individually selectable pull-up resistors with a supply-voltage invariant resistance.

    • All I/O pins have protection diodes to both VCC and Ground


    I o ports introduction2
    I/O Ports Introduction... the amount of “flash” storage space in the chip

    • Three I/O memory address locations are allocated for each port, one each for

      • the Data Register - PORTx,

        • Port Driver Register

      • Data Direction Register - DDRx,

        • Determines which bits of the port are input and which are output

      • the Port Input Pins - PINx.

        • Returns the status of all 8 port pins


    Port registers
    Port Registers the amount of “flash” storage space in the chip

    PORTx: Portx Data Register

    DDRx: Portx Data Direction Register

    PINx: PortxInput Pins


    I o ports introduction3
    I/O Ports Introduction... the amount of “flash” storage space in the chip

    • Most port pins are multiplexed with alternate functions for the peripheral features on the device.

    • Note that enabling the alternate function of some of the port pins does not affect the use of the other pins in the port as general digital I/O.


    I o ports configuring the pins
    I/O Ports Configuring the Pins the amount of “flash” storage space in the chip

    • Each port pin consists of three register bits: DDRxn, PORTxn, and PINxn

      • the DDRxn bits are accessed at the DDRx I/O address,

      • the PORTxn bits at the PORTx I/O address,

      • the PINxn bits at the PINx I/O address.

    • x stands for Port id e.g., A, B, C,or D

    • n stands for Pin# e.g., 0 to 7


    Configuring the pins of ddrx
    Configuring the Pins of the amount of “flash” storage space in the chipDDRx

    DDRx Register selects the direction of its pin.

    If DDRxn is written logic one, PORTxn is configured as an output pin.

    If DDRxn is written logic zero, PORTxn is configured as an input pin.

    DDRB = 0x02; /* sets the second lowest of port B to output */


    Configuring the pins of portx
    Configuring the Pins of the amount of “flash” storage space in the chipPORTx

    Port Driver Register

    The default for input port pins is floating.

    Can supply a pull-up resistor by writing logic 1 to the corresponding bit of the port driver register

    DDRA = 0xC0; /* upper 2 bits are output, lower 6 bits are input*/

    PORTA = 0x03; /enable internal pull-ups on lowest 2 bits*/

    Must declare all output pins using DDRx


    Configuring the pins of portx1
    Configuring the Pins of the amount of “flash” storage space in the chipPORTx...

    • If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated.

    • To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin.

    • The port pins are tri-stated when a reset condition becomes active, even if no clocks are running.


    Configuring the pins of pinx
    Configuring the Pins of the amount of “flash” storage space in the chip PINx

    Port Pins Register

    Returns the status of all 8 port pins.

    NOT CONFIGURABLE, READ ONLY

    unsigned int x;

    x = PINB; /* Places the status of port B into variable x */


    Alternate port functions
    Alternate Port Functions the amount of “flash” storage space in the chip

    Most port pins have alternate functions in addition to being General Digital I/Os


    Port a pins alternate functions
    Port A Pins Alternate Functions the amount of “flash” storage space in the chip


    Port a pins alternate functions1
    Port A Pins Alternate Functions... the amount of “flash” storage space in the chip

    • Port A has an alternate function as analog input for the ADC

    • If some Port A pins are configured as outputs, it is essential that these do not switch when a conversion is in progress

    • This might corrupt the result of the conversion.


    Port b pins alternate functions
    Port B Pins Alternate Functions the amount of “flash” storage space in the chip


    Port b pins alternate functions bit 7 sck spi bus serial clock
    Port B Pins Alternate Functions... the amount of “flash” storage space in the chip Bit 7 SCK (SPI Bus Serial Clock)

    • Master Clock output, Slave Clock input pin

    • When the SPI is enabled as a Master, the data direction of this pin is controlled by DDB7.

    • When the SPI is enabled as a Slave, this pin is configured as an input regardless of the setting of DDB7.

    • When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the PORTB7 bit.


    Port b pins alternate functions bit 6 miso master data input slave data output
    Port B Pins Alternate Functions... the amount of “flash” storage space in the chipBit 6 MISO Master Data input, Slave Data output

    • Master Data input, Slave Data output pin for SPI channel.

    • When the SPI is enabled as a Master, this pin is configured as an input regardless of the setting of DDB6.

    • When the SPI is enabled as a Slave, the data direction of this pin is controlled by DDB6.

    • When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the PORTB6 bit.


    Port b pins alternate functions bit 5 mosi master data output slave data input
    Port B Pins Alternate Functions... the amount of “flash” storage space in the chipBit 5 MOSI Master Data output, Slave Data input

    • Master Data output, Slave Data input for SPI channel.

    • When the SPI is enabled as a Slave, this pin is configured as an input regardless of the setting of DDB5.

    • When the SPI is enabled as a Master, the data direction of this pin is controlled by DDB5.

    • When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the PORTB5 bit


    Port b pins alternate functions bit 4 ss slave select input
    Port B Pins Alternate Functions... the amount of “flash” storage space in the chip Bit 4 SS: Slave Select input

    • When the SPI is enabled as a Slave, this pin is configured as an input regardless of the setting of DDB4.

    • As a Slave, the SPI is activated when this pin is driven low.

    • When the SPI is enabled as a Master, the data direction of this pin is controlled by DDB4.

    • When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the PORTB4 bit.


    Port b pins alternate functions bit 3 ain1 oc0
    Port B Pins Alternate Functions... the amount of “flash” storage space in the chip Bit 3 AIN1/OC0

    • AIN1, Analog Comparator Negative Input

      • Configure the port pin as input with the internal pull-up switched off to avoid the digital port function from interfering with the function of the analog comparator.

    • OC0, Output Compare Match output

      • The PB3 pin can serve as an external output for the Timer/Counter0 Compare Match.

      • The PB3 pin has to be configured as an output to serve this function.

      • The OC0 pin is also the output pin for the PWM mode timer function.


    Port b pins alternate functions bit 2 ain0 int2
    Port B Pins Alternate Functions... the amount of “flash” storage space in the chip Bit 2 AIN0/INT2

    • AIN0, Analog Comparator Positive input

      • Configure the port pin as input with the internal pull-up switched off to avoid the digital port function from interfering with the function of the Analog Comparator.

    • INT2, External Interrupt Source 2

      • The PB2 pin can serve as an external interrupt source to the MCU


    Port b pins alternate functions bit 1 timer counter1 counter
    Port B Pins Alternate Functions... the amount of “flash” storage space in the chip Bit 1 Timer/Counter1 Counter

    • Timer/Counter1 Counter Source


    Port b pins alternate functions bit 0 t0 xck
    Port B Pins Alternate Functions... the amount of “flash” storage space in the chip Bit 0 T0/XCK

    • T0 : Timer/Counter0 Counter Source.

    • XCK: USART External Clock.

      • The Data Direction Register (DDB0) controls whether the clock is output or input.

      • The XCK pin is active only when the USART operates in Synchronous mode


    Port c pins alternate functions
    Port C Pins Alternate Functions the amount of “flash” storage space in the chip


    Port c pins alternate functions bit 7 tosc2 timer oscillator pin 2
    Port C Pins Alternate Functions... the amount of “flash” storage space in the chipBit 7 TOSC2, Timer Oscillator pin 2

    • When the AS2 bit in ASSR is set (one) to enable asynchronous clocking of Timer/Counter2, pin PC7 is disconnected from the port, and becomes the inverting output of the Oscillator amplifier.

    • In this mode, a Crystal Oscillator is connected to this pin, and the pin can not be used as an I/O pin.


    Port c pins alternate functions bit 6 tosc1 timer oscillator pin 1
    Port C Pins Alternate Functions... the amount of “flash” storage space in the chipBit 6 TOSC1 Timer Oscillator pin 1

    • When the AS2 bit in ASSR is set (one) to enable asynchronous clocking of Timer/Counter2, pin PC6 is disconnected from the port, and becomes the input of the inverting Oscillator amplifier.

    • In this mode, a Crystal Oscillator is connected to this pin, and the pin can not be used as an I/O pin.


    Port c pins alternate functions bit 5 tdi jtag test data in
    Port C Pins Alternate Functions... the amount of “flash” storage space in the chipBit 5 TDI, JTAG Test Data In

    • Serial input data to be shifted in to the Instruction Register or Data Register (scan chains).

    • When the JTAG interface is enabled, this pin can not be used as an I/O pin.


    Port c pins alternate functions bit 4 tdo jtag test data out
    Port C Pins Alternate Functions... the amount of “flash” storage space in the chipBit 4 TDO, JTAG Test Data Out

    • Serial output data from Instruction Register or Data Register.

    • When the JTAG interface is enabled, this pin can not be used as an I/O pin.

    • The TD0 pin is tri-stated unless TAP states that shifts out data are entered.


    Port c pins alternate functions bit 3 tms jtag test mode select
    Port C Pins Alternate Functions... the amount of “flash” storage space in the chipBit 3 TMS, JTAG Test Mode Select

    • This pin is used for navigating through the TAP-controller state machine.

    • When the JTAG interface is enabled, this pin can not be used as an I/O pin.


    Port c pins alternate functions bit 2 tck jtag test clock
    Port C Pins Alternate Functions... the amount of “flash” storage space in the chipBit 2 TCK, JTAG Test Clock

    • JTAG operation is synchronous to TCK.

    • When the JTAG interface is enabled, this pin can not be used as an I/O pin.


    Port c pins alternate functions bit 1 sda two wire serial interface data
    Port C Pins Alternate Functions... the amount of “flash” storage space in the chipBit 1 SDA, Two-wire Serial Interface Data

    • When the TWEN bit in TWCR is set (one) to enable the Two-wire Serial Interface, pin PC1 is disconnected from the port and becomes the Serial Data I/O pin for the Two-wire Serial Interface.

    • In this mode, there is a spike filter on the pin to suppress spikes shorter than 50 ns on the input signal, and the pin is driven by an open drain driver with slew-rate limitation.

    • When this pin is used by the Two-wire Serial Interface, the pull-up can still be controlled by the PORTC1 bit.


    Port c pins alternate functions bit 0 scl two wire serial interface clock
    Port C Pins Alternate Functions... the amount of “flash” storage space in the chipBit 0 SCL, Two-wire Serial Interface Clock

    • When the TWEN bit in TWCR is set (one) to enable the Two-wire Serial Interface, pin PC0 is disconnected from the port and becomes the Serial Clock I/O pin for the Two-wire Serial Interface.

    • In this mode, there is a spike filter on the pin to suppress spikes shorter than 50 ns on the input signal, and the pin is driven by an open drain driver with slew-rate limitation.

    • When this pin is used by the Two-wire Serial Interface, the pull-up can still be controlled by the PORTC0 bit.


    Port d pins alternate functions
    Port D Pins Alternate Functions the amount of “flash” storage space in the chip


    Port d pins alternate functions bit 7 oc2 timer counter2 output compare match output
    Port D Pins Alternate Functions... the amount of “flash” storage space in the chipBit 7 OC2 : Timer/Counter2 Output Compare Match output

    • The PD7 pin can serve as an external output for the Timer/Counter2 Output Compare.

    • The pin has to be configured as an output to serve this function.

    • The OC2 pin is also the output pin for the PWM mode timer function.


    Port d pins alternate functions bit 6 icp1 input capture pin
    Port D Pins Alternate Functions... the amount of “flash” storage space in the chipBit 6 ICP1 : Input Capture Pin

    • The PD6 pin can act as an Input Capture pin for Timer/Counter1.


    Port d pins alternate functions bit 5 oc1a output compare match a output
    Port D Pins Alternate Functions... the amount of “flash” storage space in the chipBit 5 OC1A : Output Compare Match A output

    • The PD5 pin can serve as an external output for the Timer/Counter1 Output Compare A.

    • The pin has to be configured as an output to serve this function.

    • The OC1A pin is also the output pin for the PWM mode timer function.


    Port d pins alternate functions bit 4 oc1b output compare match b output
    Port D Pins Alternate Functions... the amount of “flash” storage space in the chipBit 4 OC1B : Output Compare Match B output

    • The PD4 pin can serve as an external output for the Timer/Counter1 Output Compare B.

    • The pin has to be configured as an output to serve this function.

    • The OC1B pin is also the output pin for the PWM mode timer function.


    Port d pins alternate functions bit 3 int1 external interrupt source 1
    Port D Pins Alternate Functions... the amount of “flash” storage space in the chipBit 3 INT1 : External Interrupt Source 1

    • The PD3 pin can serve as an external interrupt source


    Port d pins alternate functions bit 2 int0 external interrupt source 0
    Port D Pins Alternate Functions... the amount of “flash” storage space in the chipBit 2 INT0, External Interrupt Source 0

    • The PD2 pin can serve as an external interrupt source.


    Port d pins alternate functions bit 1 txd transmit data
    Port D Pins Alternate Functions... the amount of “flash” storage space in the chipBit 1 TXD : Transmit Data

    • Data output pin for the USART

    • When the USART Transmitter is enabled, this pin is configured as an output regardless of the value of DDD1.


    Port d pins alternate functions bit 0 rxd receive data
    Port D Pins Alternate Functions... the amount of “flash” storage space in the chipBit 0 RXD : Receive Data

    • Data input pin for the USART

    • When the USART Receiver is enabled this pin is configured as an input regardless of the value of DDD0.

    • When the USART forces this pin to be an input, the pull-up can still be controlled by the PORTD0 bit.


    Interrupts1
    Interrupts the amount of “flash” storage space in the chip


    Interrupts2
    Interrupts the amount of “flash” storage space in the chip

    • The ATmega has 21 interrupts

    • They are divided into two groups

      • external interrupts ( 4 in numbers) and

      • internal interrupts (17 in numbers)


    External interrupts
    External Interrupts the amount of “flash” storage space in the chip

    • 1 reset interrupt

    • 3 external interrupts. They are

      • External Interrupt Request0 (INT0)

      • External Interrupt Request1 (INT1)

      • External Interrupt Request2 (INT2)

    • Priority

      RESET>INT0 > INT1 > INT2


    Internal interrupts
    Internal Interrupts the amount of “flash” storage space in the chip

    • 17 internal interrupts support the peripherals like Timers, USART, ADC etc.

      • 8 timer interrupts

      • 3 serial port interrupts

      • 2 memory interrupts

      • 1 ADC interrupt

      • 1 analogue comparator interrupt

      • 1 SPI interrupt

      • 1 TWI interrupt


    Interrupts how to enable them
    Interrupts... the amount of “flash” storage space in the chiphow to enable them?

    • All interrupts are assigned individual enable bits which must be written logic one together with the Global Interrupt Enable bit in the Status Register in order to enable the interrupt.

    • The interrupts have a separate program vector in the program memory space.


    External interrupts1
    External Interrupts the amount of “flash” storage space in the chip

    • The External Interrupts are triggered by the INT0, INT1, and INT2 pins.

    • if enabled, the interrupts will trigger even if the INT0..2 [pd2, pd3, pb2] pins are configured as outputs.

    • The external interrupts can be triggered by a falling or rising edge or a low level (INT2 is only an edge triggered interrupt).

    • This is set up by the MCU Control Register (MCUCR) and MCU Control and Status Register (MCUCSR)


    External interrupts2
    External Interrupts the amount of “flash” storage space in the chip

    • When the external interrupt is enabled and is configured as level triggered (only INT0/INT1), the interrupt will trigger as long as the pin is held low

    • Note that recognition of falling or rising edge interrupts on INT0 and INT1 requires the presence of an I/O clock (more in "Clock Systems and their Distribution" in Atmel datasheet)

    • Low level interrupts on INT0/INT1 and the edge interrupt on INT2 are detected asynchronously


    External interrupts3
    External Interrupts the amount of “flash” storage space in the chip

    • A level triggered interrupt can be used for wake-up from Power-down mode

      • in that case, the changed level must be held for some time to wake up the MCU.

    • This makes the MCU less sensitive to noise.

    • The changed level is sampled twice by the Watchdog Oscillator clock.

    • The period of the Watchdog Oscillator is 1 µs (nominal) at 5.0V and 25°C.

    • The frequency of the Watchdog Oscillator is voltage dependent


    External interrupts4
    External Interrupts the amount of “flash” storage space in the chip

    • The MCU will wake up if the input has the required level during this sampling or if it is held until the end of the start-up time.

    • The start-up time is defined by the SUT Fuses

    • If the level is sampled twice by the Watchdog Oscillator clock but disappears before the end of the start-up time, the MCU will still wake up, but no interrupt will be generated.

    • The required level must be held long enough for the MCU to complete the wake up to trigger the level interrupt.


    External interrupt enable
    External Interrupt... the amount of “flash” storage space in the chipenable


    External interrupt sense control
    External Interrupt... the amount of “flash” storage space in the chipSense control


    External interrupt sense control int0
    External Interrupt... the amount of “flash” storage space in the chipSense control...INT0


    External interrupt sense control int1
    External Interrupt... the amount of “flash” storage space in the chipSense control...INT1


    External interrupt sense control int2
    External Interrupt the amount of “flash” storage space in the chip Sense control...INT2

    • MCU Control and Status Register (MCUCSR)

      ISC2 bit

      ISC2=1, The rising edge of INT2 generates an interrupt request.

      ISC2=0, The falling edge of INT2 generates an interrupt request.


    Interrupt handling
    Interrupt Handling the amount of “flash” storage space in the chip

    • The lowest addresses in the program memory space are by default defined as the Reset and Interrupt Vectors.

    • The complete list of vectors is shown later

    • The list also determines the priority levels of the different interrupts.

      • The lower the address the higher is the priority level.

    • RESET has the highest priority, and next is INT0 - the External Interrupt Request 0.


    Interrupt handling1
    Interrupt Handling... the amount of “flash” storage space in the chip

    • When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are disabled.

    • The user software can write logic one to the I-bit to enable nested interrupts.

    • All enabled interrupts can then interrupt the current interrupt routine.

    • The I-bit is automatically set when a Return from Interrupt instruction - RETI - is executed.


    Interrupt handling2
    Interrupt Handling the amount of “flash” storage space in the chip...

    • There are basically two types of interrupts.

    • The first type is triggered by an event that sets the Interrupt Flag.

    • For these interrupts, the Program Counter is vectored to the actual Interrupt Vector in order to execute the interrupt handling routine, and hardware clears the corresponding Interrupt Flag.


    Interrupt handling3
    Interrupt Handling... the amount of “flash” storage space in the chip

    • Interrupt Flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared.

    • If an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the Interrupt Flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software.

    • Similarly, if one or more interrupt conditions occur while the Global Interrupt Enable bit is cleared, the corresponding Interrupt Flag(s) will be set and remembered until the global interrupt enable bit is set, and will then be executed by order of priority.


    Interrupt handling4
    Interrupt Handling the amount of “flash” storage space in the chip...

    • The second type of interrupts will trigger as long as the interrupt condition is present.

    • These interrupts do not necessarily have Interrupt Flags. If the interrupt condition disappears before the interrupt is enabled, the interrupt will not be triggered.

    • When the AVR exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served.

    • Note that the Status Register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine.

    • This must be handled by software.


    Interrupt handling5
    Interrupt Handling... the amount of “flash” storage space in the chip

    • When using the CLI instruction to disable interrupts, the interrupts will be immediately disabled.

    • No interrupt will be executed after the CLI instruction, even if it occurs simultaneously with the CLI instruction.

    • When using the SEI instruction to enable interrupts, the instruction following SEI will be executed before any pending interrupts


    Interrupt response time
    Interrupt Response Time the amount of “flash” storage space in the chip

    • The interrupt execution response for all the enabled AVR interrupts is four clock cycles minimum.

    • After four clock cycles the program vector address for the actual interrupt handling routine is executed.

    • During this four clock cycle period, the Program Counter is pushed onto the Stack. The vector is normally a jump to the interrupt routine, and this jump takes three clock cycles.

    • If an interrupt occurs during execution of a multi-cycle instruction, this instruction is completed before the interrupt is served.


    Interrupt response time1
    Interrupt Response Time... the amount of “flash” storage space in the chip

    • If an interrupt occurs when the MCU is in sleep mode, the interrupt execution response time is increased by four clock cycles. This increase comes in addition to the start-up time from the selected sleep mode.

    • A return from an interrupt handling routine takes four clock cycles.

    • During these four clock cycles, the Program Counter (two bytes) is popped back from the Stack, the Stack Pointer is incremented by two, and the I-bit in SREG is set.


    Interrupt vectors in atmega16
    Interrupt Vectors in ATmega16 the amount of “flash” storage space in the chip


    Interrupt vectors in atmega161
    Interrupt Vectors in ATmega16... the amount of “flash” storage space in the chip


    Interrupt vectors in atmega162
    Interrupt Vectors in ATmega16... the amount of “flash” storage space in the chip


    Interrupt vectors in atmega163
    Interrupt Vectors in ATmega16... the amount of “flash” storage space in the chip


    ADC the amount of “flash” storage space in the chip


    Adc features
    ADC Features the amount of “flash” storage space in the chip

    • 10-bit Resolution

    • 0.5 LSB Integral Non-linearity

    • ±2 LSB Absolute Accuracy

    • 13 µs- 260 µs Conversion Time

    • 8 Multiplexed Single Ended Input Channels

    • 7 Differential Input Channels


    Adc features1
    ADC Features... the amount of “flash” storage space in the chip

    • 2 Differential Input Channels with Optional Gain of 10x and 200x

    • Optional Left adjustment for ADC Result Readout

    • 0 - VCC ADC Input Voltage Range

    • Selectable 2.56V ADC Reference Voltage

    • Free Running or Single Conversion Mode

    • ADC Start Conversion by Auto Triggering on Interrupt Sources

    • Interrupt on ADC Conversion Complete


    ADC... the amount of “flash” storage space in the chip

    • The ATmega16 features a 10-bit successive approximation ADC.

    • The ADC is connected to an 8-channel Analog Multiplexer which allows 8 single-ended voltage inputs constructed from the pins of Port A.

    • The single-ended voltage inputs refer to 0V (GND).

    • The device also supports 16 differential voltage input combinations.

    • Two of the differential inputs (ADC1, ADC0 and ADC3, ADC2) are equipped with a programmable gain stage, providing amplification steps of 0 dB (1x), 20 dB (10x), or 46 dB (200x) on the differential input voltage before the A/D conversion.


    ADC... the amount of “flash” storage space in the chip

    • Seven differential analog input channels share a common negative terminal (ADC1), while any other ADC input can be selected as the positive input terminal.

    • If 1x or 10x gain is used, 8-bit resolution can be expected.

    • If 200x gain is used, 7-bit resolution can be expected.


    ADC the amount of “flash” storage space in the chip

    • The ADC contains a Sample and Hold circuit which ensures that the input voltage to the ADC is held at a constant level during conversion.

    • The ADC has a separate analog supply voltage pin, AVCC.

    • AVCC must not differ more than ±0.3V from VCC.

    • Internal reference voltages of nominally 2.56V or AVCC are provided On-chip.


    Adc operation
    ADC the amount of “flash” storage space in the chipOperation

    • The ADC converts an analog input voltage to a 10-bit digital value through successive approximation.

    • The minimum value represents GND and the maximum value represents the voltage on the AREF pin minus 1 LSB.

    • Optionally, AVCC or an internal 2.56V reference voltage may be connected to the AREF pin by writing to the REFSn bits in the ADMUX Register.


    Adc operation1
    ADC the amount of “flash” storage space in the chipOperation...

    • The internal voltage reference may thus be decoupled by an external capacitor at the AREF pin to improve noise immunity.

    • The analog input channel and differential gain are selected by writing to the MUX bits in ADMUX.

    • A selection of ADC input pins can be selected as positive and negative inputs to the differential gain amplifier.


    Adc operation2
    ADC the amount of “flash” storage space in the chipOperation...

    • If differential channels are selected, the differential gain stage amplifies the voltage difference between the selected input channel pair by the selected gain factor.

    • This amplified value then becomes the analog input to the ADC.

    • If single ended channels are used, the gain amplifier is bypassed altogether.

    • The ADC is enabled by setting the ADC Enable bit, ADEN in ADCSRA.


    Adc operation3
    ADC the amount of “flash” storage space in the chipOperation...

    • Voltage reference and input channel selections will not go into effect until ADEN is set.

    • The ADC does not consume power when ADEN is cleared, so it is recommended to switch off the ADC before entering power saving sleep modes.

    • The ADC generates a 10-bit result which is presented in the ADC Data Registers, ADCH and ADCL.

    • By default, the result is presented right adjusted, but can optionally be presented left adjusted by setting the ADLAR bit in ADMUX


    Adc operation4
    ADC the amount of “flash” storage space in the chipOperation...

    • If the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read ADCH.

    • Otherwise, ADCL must be read first, then ADCH, to ensure that the content of the Data Registers belongs to the same conversion.

    • Once ADCL is read, ADC access to Data Registers is blocked.

    • This means that if ADCL has been read, and a conversion completes before ADCH is read, neither register is updated and the result from the conversion is lost.

    • When ADCH is read, ADC access to the ADCH and ADCL Registers is re-enabled.


    Adc operation5
    ADC the amount of “flash” storage space in the chipOperation...

    • The ADC has its own interrupt which can be triggered when a conversion completes.

    • When ADC access to the Data Registers is prohibited between reading of ADCH and ADCL, the interrupt will trigger even if the result is lost.


    Adc starting a conversion
    ADC the amount of “flash” storage space in the chipStarting a Conversion

    • A single conversion is started by writing a logical one to the ADC Start Conversion bit, ADSC.

    • This bit stays high as long as the conversion is in progress and will be cleared by hardware when the conversion is completed.

    • If a different data channel is selected while a conversion is in progress, the ADC will finish the current conversion before performing the channel change.


    Adc starting a conversion1
    ADC the amount of “flash” storage space in the chipStarting a Conversion...

    • Alternatively, a conversion can be triggered automatically by various sources. Auto Triggering is enabled by setting the ADC Auto Trigger Enable bit, ADATE in ADCSRA.

    • The trigger source is selected by setting the ADC Trigger Select bits, ADTS in SFIOR

    • When a positive edge occurs on the selected trigger signal, the ADC prescaler is reset and a conversion is started.

    • This provides a method of starting conversions at fixed intervals.


    Adc starting a conversion2
    ADC the amount of “flash” storage space in the chipStarting a Conversion...

    • If the trigger signal still is set when the conversion completes, a new conversion will not be started.

    • If another positive edge occurs on the trigger signal during conversion, the edge will be ignored.

    • Note that an Interrupt Flag will be set even if the specific interrupt is disabled or the global interrupt enable bit in SREG is cleared.

    • A conversion can thus be triggered without causing an interrupt.

    • However, the Interrupt Flag must be cleared in order to trigger a new conversion at the next interrupt event


    Adc starting a conversion3
    ADC the amount of “flash” storage space in the chipStarting a Conversion...

    • Using the ADC Interrupt Flag as a trigger source makes the ADC start a new conversion as soon as the ongoing conversion has finished.

    • The ADC then operates in Free Running mode, constantly sampling and updating the ADC Data Register.

    • The first conversion must be started by writing a logical one to the ADSC bit in ADCSRA.


    Adc starting a conversion4
    ADC the amount of “flash” storage space in the chipStarting a Conversion...

    • In this mode the ADC will perform successive conversions independently of whether the ADC Interrupt Flag, ADIF is cleared or not.

    • If Auto Triggering is enabled, single conversions can be started by writing ADSC in ADCSRA to one.

    • ADSC can also be used to determine if a conversion is in progress.

    • The ADSC bit will be read as one during a conversion, independently of how the conversion was started.


    Adc adc voltage reference
    ADC the amount of “flash” storage space in the chipADC Voltage Reference

    • The reference voltage for the ADC (VREF) indicates the conversion range for the ADC.

    • Single ended channels that exceed VREF will result in codes close to 0x3FF.

    • VREF can be selected as either AVCC, internal 2.56V reference, or external AREF pin.

    • AVCC is connected to the ADC through a passive switch.


    Adc adc voltage reference1
    ADC the amount of “flash” storage space in the chipADC Voltage Reference

    • In either case, the external AREF pin is directly connected to the ADC, and the reference voltage can be made more immune to noise by connecting a capacitor between the AREF pin and ground.

    • If no external voltage is applied to the AREF pin, the user may switch between AVCC and 2.56V as reference selection.

    • The first ADC conversion result after switching reference voltage source may be inaccurate, and the user is advised to discard this result.


    Adc adc conversion result
    ADC the amount of “flash” storage space in the chipADC Conversion Result

    • After the conversion is complete (ADIF is high), the conversion result can be found in the ADC Data Registers (ADCL, ADCH).

    • For single ended conversion, the result is

      VIN ⋅ 1024

      ADC = -------------------------- VREF

      where VIN is the voltage on the selected input pin and VREF the selected voltage reference

    • 0x000 represents ground, and 0x3FF represents the selected reference voltage minus one LSB.


    Adc adc conversion result1
    ADC the amount of “flash” storage space in the chipADC Conversion Result

    • If differential channels are used, the result is

      (VPOS- VNEG) ⋅ GAIN ⋅ 512

      ADC = -----------------------------------------

      VREF

      where VPOS is the voltage on the positive input pin,

      VNEG the voltage on the negative input pin,

      GAIN the selected gain factor, and

      VREF the selected voltage reference.

    • The result is presented in two's complement form, from 0x200 (-512d) through 0x1FF (+511d).

    • Note that if the user wants to perform a quick polarity check of the results, it is sufficient to read the MSB of the result (ADC9 in ADCH).

    • If this bit is one, the result is negative, and if this bit is zero, the result is positive.


    ADC the amount of “flash” storage space in the chip

    • When an ADC conversion is complete, the result is found in these two registers.

    • If differential channels are used, the result is presented in two's complement form.

    • When ADCL is read, the ADC Data Register is not updated until ADCH is read.

    • Consequently, if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read ADCH.

    • Otherwise, ADCL must be read first, then ADCH.

    • The ADLAR bit in ADMUX, and the MUXn bits in ADMUX affect the way the result is read from the registers.

    • If ADLAR is set, the result is left adjusted.

    • If ADLAR is cleared (default), the result is right adjusted.


    ADC the amount of “flash” storage space in the chip

    • ADC Multiplexer Selection Register (ADMUX)

    • ADC Control and Status Register A (ADCSRA)

    • The ADC Data Register (ADCL and ADCH)


    Analog comparator
    Analog Comparator the amount of “flash” storage space in the chip


    Analog comparator1
    Analog Comparator the amount of “flash” storage space in the chip

    • The Analog Comparator compares the input values on the positive pin AIN0 and negative pin AIN1.

    • When the voltage on the positive pin AIN0 is higher than the voltage on the negative pin AIN1, the Analog Comparator Output, ACO, is set.

    • The comparator's output can be set to trigger the Timer/Counter1 Input Capture function.

    • In addition, the comparator can trigger a separate interrupt, exclusive to the Analog Comparator.

    • The user can select Interrupt triggering on comparator output rise, fall or toggle.


    Analog comparator2
    Analog Comparator... the amount of “flash” storage space in the chip

    • Bit 3 (ACME: Analog Comparator Multiplexer Enable bit) of Special Function IO Register (SFIOR)

      • When this bit is written logic one and the ADC is switched off (ADEN in ADCSRA is zero), the ADC multiplexer selects the negative input to the Analog Comparator.

      • When this bit is written logic zero, AIN1 is applied to the negative input of the Analog Comparator.


    Analog comparator3
    Analog Comparator... the amount of “flash” storage space in the chip

    • It is possible to select any of the ADC7..0 pins to replace the negative input to the Analog Comparator.

    • The ADC multiplexer is used to select this input, and consequently, the ADC must be switched off to utilize this feature.

    • If the Analog Comparator Multiplexer Enable bit (ACME in SFIOR) is set and the ADC is switched off (ADEN in ADCSRA is zero), MUX2..0 bits in ADMUX select the input pin to replace the negative input to the Analog Comparator

    • If ACME is cleared or ADEN is set, AIN1 is applied to the negative input to the Analog Comparator


    Analog comparator4
    Analog Comparator the amount of “flash” storage space in the chip

    • Analog Comparator Control and Status Register (ACSR)

      • its bits are used to control and see status of comparator


    Usart1
    USART the amount of “flash” storage space in the chip


    Usart main features
    USART the amount of “flash” storage space in the chipmain features

    • Full Duplex Operation

      • Independent Serial Receive and Transmit Registers

    • Asynchronous or Synchronous Operation

    • Master or Slave Clocked Synchronous Operation

    • High Resolution Baud Rate Generator

    • Supports Serial Frames with

      • 5, 6, 7, 8, or 9 Data Bits and

      • 1 or 2 Stop Bits

    • Odd or Even Parity Generation and Parity Check Supported by Hardware


    Usart main features1
    USART the amount of “flash” storage space in the chipmain features...

    • Data OverRun Detection

    • Framing Error Detection

    • Noise Filtering Includes False Start Bit Detection and Digital Low Pass Filter

    • Three Separate Interrupts on TX Complete, TX Data Register Empty, and RX Complete

    • Multi-processor Communication Mode

    • Double Speed Asynchronous Communication Mode


    Usart overview
    USART the amount of “flash” storage space in the chipOverview

    • The main parts of the USART are:

      • Control Registers

      • Clock Generator

      • Transmitter

      • Receiver and


    Usart control registers
    USART Control Registers the amount of “flash” storage space in the chip

    Three groups of registers

    USART Baud Rate Registers

    UBRRH and UBRRL

    USART Control and Status Registers

    UCSRA, UCSRB, UCSRC

    USART Data Registers

    UDR


    Clock generator
    Clock Generator the amount of “flash” storage space in the chip

    • The clock generation logic generates the base clock for the Transmitter and Receiver

    • The USART supports four modes of clock operation:

      • Normal Asynchronous,

      • Double Speed Asynchronous,

      • Master Synchronous and

      • Slave Synchronous mode.


    Clock generator1
    Clock Generator... the amount of “flash” storage space in the chip

    • The UMSEL bit in USART Control and Status Register C (UCSRC) selects between asynchronous and synchronous operation.

    • Double Speed (Asynchronous mode only) is controlled by the U2X found in the UCSRA Register.

    • When using Synchronous mode (UMSEL = 1), the Data Direction Register for the XCK pin (DDR_XCK) controls whether the clock source is internal (Master mode) or external (Slave mode).

    • The XCK pin is only active when using Synchronous mode.


    Baud rate generator
    Baud Rate Generator the amount of “flash” storage space in the chip

    • The USART Baud Rate Register (UBRR) and the down-counter connected to it function as a baud rate generator.

    • The down-counter, running at system clock, is loaded with the UBRR value each time the counter has counted down to zero or when the UBRRL Register is written.

    • A clock is generated each time the counter reaches zero.

    • This clock is the baud rate generator clock output


    Baud rate generator1
    Baud Rate Generator... the amount of “flash” storage space in the chip

    • The Transmitter divides the baud rate generator clock output by 2, 8 or 16 depending on mode.

    • The baud rate generator output is used directly by the receiver's clock and data recovery units.

    • However, the recovery units use a state machine that uses 2, 8 or 16 states depending on mode set by the state of the UMSEL, U2X and DDR_XCK bits.


    Baud rate registers
    Baud Rate Registers the amount of “flash” storage space in the chip


    Baud rate registers1
    Baud Rate Registers... the amount of “flash” storage space in the chip


    Baud rate registers2
    Baud Rate Registers... the amount of “flash” storage space in the chip


    Usart transmitter receiver
    USART the amount of “flash” storage space in the chipTransmitter/Receiver

    • The Transmitter consists of a single write buffer, a serial Shift Register, parity generator and control logic for handling different serial frame formats.

    • The write buffer allows a continuous transfer of data without any delay between frames.

    • The receiver includes recovery units, a parity checker, control logic, a Shift Register and a two level receive buffer (UDR).

    • The receiver supports the same frame formats as the transmitter, and can detect frame error, data overrun and parity errors


    Usart frame formats
    USART the amount of “flash” storage space in the chipFrame Formats

    • A serial frame is defined to be one character of data bits with synchronization bits (start and stop bits), and optionally a parity bit for error checking.

    • The USART accepts all 30 combinations of the following as valid frame formats:

      • 1 start bit

      • 5, 6, 7, 8, or 9 data bits

      • no, even or odd parity bit

      • 1 or 2 stop bits


    Usart frame formats1
    USART the amount of “flash” storage space in the chipFrame Formats...

    • A frame starts with the start bit followed by the least significant data bit.

    • Then the next data bits, up to a total of nine, are succeeding, ending with the most significant bit.

    • If enabled, the parity bit is inserted after the data bits, before the stop bits.

    • When a complete frame is transmitted, it can be directly followed by a new frame, or the communication line can be set to an idle (high) state


    Usart frame formats2
    USART the amount of “flash” storage space in the chipFrame Formats...

    • The frame format used by the USART is set by the UCSZ2:0, UPM1:0, and USBS bits in UCSRB and UCSRC

    • The Receiver and Transmitter use the same setting.

    • Note that changing the setting of any of these bits will corrupt all ongoing communication for both the Receiver and Transmitter.

    • The USART Character Size (UCSZ2:0) bits select the number of data bits in the frame.


    Usart frame formats3
    USART the amount of “flash” storage space in the chipFrame Formats...

    • The USART Parity mode (UPM1:0) bits enable and set the type of parity bit.

    • The selection between one or two stop bits is done by the USART Stop Bit Select (USBS) bit.

    • The receiver ignores the second stop bit.

    • An FE (Frame Error) will therefore only be detected in the cases where the first stop bit is zero.


    Usart control registers1
    USART Control Registers the amount of “flash” storage space in the chip

    Three groups of registers

    USART Baud Rate Registers

    UBRRH and UBRRL

    USART Control and Status Registers

    UCSRA, UCSRB, UCSRC

    USART Data Registers

    UDR


    Usart cont
    USART(Cont.) the amount of “flash” storage space in the chip


    Usart cont1
    USART(Cont.) the amount of “flash” storage space in the chip


    Usart cont2
    USART(Cont.) the amount of “flash” storage space in the chip


    Usart initialization
    USART Initialization the amount of “flash” storage space in the chip

    • The USART has to be initialized before any communication can take place.

    • The initialization process normally consists of setting the baud rate, setting frame format and enabling the Transmitter or the Receiver depending on the usage.

    • For interrupt driven USART operation, the Global Interrupt Flag should be cleared (and interrupts globally disabled) when doing the initialization.


    Usart initialization1
    USART Initialization... the amount of “flash” storage space in the chip

    • Before doing a re-initialization with changed baud rate or frame format, be sure that there are no ongoing transmissions during the period the registers are changed.

    • The TXC Flag can be used to check that the Transmitter has completed all transfers, and the RXC Flag can be used to check that there are no unread data in the receive buffer.

    • Note that the TXC Flag must be cleared before each transmission (before UDR is written) if it is used for this purpose.


    Serial usart main tasks
    Serial USART ─ Main tasks the amount of “flash” storage space in the chip

    Initialising the serial port.

    Sending a character.

    Receiving a character.

    Sending/receiving formatted strings.


    ad