R and A Unit-3
R and A Unit-3
AVR MICROCONTROLLER
Contents
The AVR RISC microcontroller architecture:
• Introduction
• AVR family architecture
• register file
• Pin diagram of AVR
• memory organization
• I/O ports
• timers
• USART
• Interrupt structure.
AVR MICROCONTROLLER
INTRODUCTION
• The architecture of AVR was developed by Alf-Egil Bogen and Vegard Wollan. AVR derives its
name from its developers and stands for Alf-Egil Bogen Vegard Wollan RISC microcontroller, also
known as Advanced Virtual RISC.
• The AT90S8515 was the first microcontroller which was based on AVR architecture.
• However the first microcontroller to hit the commercial market was AT90S1200 in the year 1997.
TinyAVR – Less memory, small size, suitable only for simpler applications
MegaAVR – These are the most popular ones having good amount of memory (upto 256 KB),
higher number of inbuilt peripherals and suitable for moderate to complex applications.
XmegaAVR – Used commercially for complex applications, which require large program
memory and high speed.
AVR MICROCONTROLLER
INTRODUCTION
• The following table compares the above mentioned AVR series of
microcontrollers:
AVR MICROCONTROLLER
INTRODUCTION
• What’s special about AVR?
AVR MICROCONTROLLER
INTRODUCTION
• AVR is an 8-bit microcontroller belonging to the family of Reduced Instruction Set Computer
(RISC). In RISC architecture the instruction set of the computer are not only fewer in number
but also simpler and faster in operation.
• This microcontroller is capable of transmitting and receiving 8-bit data. The input/output
registers available are of 8-bits. The AVR family controllers have register based architecture
which means that both the operands for an operation are stored in a register and the result of
the operation is also stored in a register.
• The specific aspect of RISC is that almost all instructions execute in one machine cycle.
• The next RISC principle adopted by AVR is the load-store architecture which means that the
only instructions that access memory are load and store.
• Load instruction loads data from memory into the register where as store moves the data from
register into the memory.
• For an ALU instruction to be executed the operands are brought into the registers using LOAD
and the result is stored back in the memory after execution.
• Another aspect of RISC architecture is larger register file which is taken care by the AVR design.
AVR MICROCONTROLLER
INTRODUCTION
Atmega16 microcontroller, which is a 40-pin IC and belongs to the megaAVR category of AVR family. Some of the
features of Atmega16 are:
16KB of ISP Flash memory
1KB of SRAM
512 Bytes of EEPROM
Available in 40-Pin DIP
8-Channel 10-bit ADC
Two 8-bit Timers/Counters
One 16-bit Timer/Counter
4 PWM Channels
Serial USART
SPI Interface
Digital to Analog Converter.
AVR MICROCONTROLLER
Architecture
• The Atmel®AVR®ATmega32 is a low-power CMOS 8-bit microcontroller based on the AVR
enhanced RISC architecture. By executing powerful instructions in a single clock cycle,
the ATmega32 achieves throughputs approaching 1 MIPS per MHz
Atmega32 Features:
• 2 Kilo bytes of internal Static RAM
• 32 X 8bit general working purpose registers
• 32 Kilo bytes of in system self programmable flash program
memory.
• 1024 bytes EEPROM
• Programmable serial USART
• 8 Channel, 10 bit ADC
• One 16-bit timer/counter with separate prescaler, compare mode
and capture mode.
AVR MICROCONTROLLER
Architecture
• Two 8-bit timers/counters with separate prescalers and compare modes
• Available in 40 pin DIP, 44-pad QFN(Quad Flat no lead)/MLF(Micro lead
frame) and 44-lead QTFP(thin quad flat package)
• 32 programmable I/O lines
• In system programming by on-chip boot program
• Master/slave SPI serial interface
• 4 PWM channels
• Programmable watch dog timer with separate on-chip oscillator
• a JTAG interface for Boundary scan, On-chip Debugging support and programming
• a serial programmable USART
• A Two Wire Interface(I2C) for serial communication
• six software selectable power saving modes(Idle mode, Power-down mode, Power-save
mode, ADC Noise Reduction mode, Standby mode, Extended Standby mode )
AVR MICROCONTROLLER
Architecture
• The Idle mode stops the CPU while allowing the USART, Two-wire interface, A/D Converter, SRAM,
Timer/ Counters, SPI port, and interrupt system to continue functioning.
• The Power-down mode saves the register contents but freezes the Oscillator, disabling all other
chip functions until the next External Interrupt or Hardware Reset.
• In Power-save mode, the Asynchronous Timer continues to run allowing the user to maintain a
timer base while the rest of the device is sleeping.
Note: The Asynchronous Timer clock allows the Asynchronous Timer/Counter to be clocked directly from an external 32kHz
clock crystal. The dedicated clock domain allows using this Timer/Counter as a real-time counter even when the device is in sleep
mode.
• The ADC Noise Reduction mode stops the CPU and all I/O modules except Asynchronous Timer and
ADC, to minimize switching noise during ADC conversions.
• In Standby mode, the external crystal/resonator clock remains running while the rest of the device
is sleeping.
• In Extended Standby mode, both the external crystal/resonator clock and the Asynchronous Timer
continue to run.
Architecture of AVR
• The architecture of AVR is divided into two parts:
1)The core: Computing Engine of the microcontroller.
2)Peripherals: No. and type of peripherals vary between different microcontrollers.
Function of the core is to ensure correct program execution.
It has an 8-bit data BUS and the memory is based on Harvard Architecture(Data memory
and program memory and associate BUSES are separate).
Instructions in the program memory are executed with single level pipelining i.e while one
instruction is being executed the next instruction is prefetched from the program memory.
Fig: AVR MCU ARCHITECTURE
Architecture of AVR
Atmel's ATmega32 is an 8-bit RISC processor, based on Harvard architecture.
On-chip Memory: Atmega32 consist of three different memory sections
1)Flash EEPROM: Flash EEPROM or simple flash memory is used to store the program dumped or burnt by
the user on to the microcontroller. It can be easily erased in blocks. Flash memory is non-volatile i.e., it
retains the program even if the power is cut-off. Atmega32 is available with 32KB of in system programmable
Flash EEPROM. Program Flash memory space is divided in two sections, the Boot program section and the
Application Program section. On-chip Boot program running on the AVR core can use any interface to
download the application
program in the Application Flash memory.
2) EEPROM: This is also a nonvolatile memory used to store data which is more or less a constant once the
design is finalized. Atmega32 has 1KB of EEPROM.
3)
The Size of SRAM in Atmega32 is 2KB.During interrupts and subroutine calls, the return address Program
Counter (PC) is stored on the Stack. The Stack is effectively allocated in the general data SRAM
Architecture of AVR
Program Counter:
For Each Member of the AVR series ,the bit size of the program counter corresponds to how much of program memory is
available. For Atmega32 chip ,there is 32KB of ROM organized as 16K x 16-bits.For address numbering to 16K(24 x 210 =214 ),14
bits are needed and hence this microcontroller has 14-bit PC.
Register File:
Atmega32 is equipped with 32 general purpose registers of 8-bit size, which are coupled directly with the Arithmetic Logical
Unit (ALU) of CPU. These registers are used as operand registers for computation . Of these 32 registers, 6 registers can be
used as address registers to act as data address pointers.(Two registers each from X, Y, Z registers)
• The I/O memory space contains 64bytes of space for CPU peripheral functions such as Control Registers, SPI, and other I/O
functions. The I/O Memory can be accessed directly, or as the Data Space locations following those of the Register File, $20
- $5F.
Note: The registers have names as well as addresses which correspond to the locations in RAM space.
Architecture
of AVR
• 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
Architecture of AVR
• Status and Control Register: After an arithmetic operation, the Status Register is updated
to reflect information about the result of the operation. 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.
Watchdog Timer: The Watchdog Timer is clocked from a separate On-chip Oscillator which runs at
1MHz.Watchdog timer continuously monitors and resets the controller if the code gets stuck at any
execution point for more than a defined time interval.
Architecture of AVR
Interrupts: Atmega32 consists of 21 interrupt sources out of which three are
external(INT0,INT1,INT2).Upon activation of these interrupts the Atmega32 controller gets
interrupted in whatever task it is doing and jumps to perform Interrupt Service Routine. The
remaining are internal interrupts which support various operations in the microcontroller like USART,
ADC, Timers etc.
USART: Universal Synchronous and Asynchronous Receiver and Transmitter interface is available
for interfacing with external device capable of communicating serially by using TXD and RXD pins.
SPI: Serial Peripheral Interface, 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.
TWI(I2C): Two Wire Interface (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.
MCU Timing and Control: It is used for the generation of timing and control signals.
Architecture of AVR
• 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.
Register File
• The register file is optimized for the AVR enhanced RISC instruction set. Most of the
instructions that operate on the register file have direct access to all the registers.
• Most of these instructions are just single instruction that run in one clock cycle . Since there
are 32 of these registers combined with direct accessibility by the ALU, programmers can
have more registers for storage of values for faster processing.
• Figure in the next slide shows the structure of the 32 general purpose working registers in
the CPU:
Register File
Register File
• 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.
• 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 figure in the next
slide:
• The registers have names as well as addresses which correspond to their location in RAM
space.
• The registers R27 and R26 are concatenated to form the X-register ,R29 and R28 to form
the Y register ,R31 and R30 to form the Z Register
AVR general purpose Registers
Register File
• Status and Control Register: After an arithmetic operation, the Status Register is updated
to reflect information about the result of the operation. 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.
for that port and then bring in the data present at the pins. When DDR contains 0’S the port gets data.
• Upon reset ,all ports have the value 0x00 in their DDR registers . This means all ports are configured
as inputs.
I/O Ports
• PINx and PORTx:
• TCCR0 Register:
TCCR0 is an 8-bit register and used for the control of timer0.
Timers
• Timer/Counter Interrupt flag register(TIFR):This register contains the
flags of different timers.
Timers
• TOV0 Flag: The TOV0 flag is set when the timer rolls over from $FF to 00 and it remains set until the software clears it.
-itored.
CTC Mode(Clear Timer0 on Compare Match):The OCR0 register is used with CTC mode.In the CTC mode the timer is
incremented with the clock. But it counts up until the contents of TCNT0 register becomes equal with the contents of OCR0
register. Then the timer will be cleared and the OCF0 flag will be set.
Timer 2:
• Like timer0 , Timer2 also has the above set of registers and hence the working of timer0 and timer 2 are almost
similar with few differences.
• These are referred to as TCNT1L (Timer1 lower byte) and TCNT1H(Timer1 higher Byte)
1:256, 1:1024.
• There are two OCR registers each of 16-bit in timer1:OCR1A and OCR1B.There are two separate flags for
each of the OCR registers which act independently of each other.
• When TCNT1 equals OCR1A, THE OCF1A flag will be set. When the TCNT1 equal OCR1B the OCF1B flag will
be set.
• There is also an auxiliary register named ICR1(Input Capture Register) which is used in operations such as
capturing.ICR1 is a 16-bit register made of ICRH and ICRL.
Timer1 High and low Registers
• UCSRA Register:
FIG:UBRR REGISTER
USART
Interrupt Structure
• A single microcontroller can serve several devices. There are two methods by
which devices receive service from the microcontroller.
1. Interrupts
2. Polling
In the interrupt method whenever any device needs microcontroller’s service ,
the device notifies it by sending an interrupt signal. Upon receiving the
interrupt signal the microcontroller stops whatever it is doing and serves the
device.
The program associated with the interrupt is called Interrupt service Routine or
Interrupt Handler.
The group of memory locations set aside to hold the addresses of ISRs is called
the interrupt vector table.
Interrupt Structure
• INT0:When this bit is ‘1’ and global interrupt bit in SREG is ‘1’ the external interrupt INT0 is
enabled. The ISC01 and ISC00 of MCUCR register control when the interrupt is to be
activated(Rising or falling edge or level triggered)
• INT1:When this bit is ‘1’ and global interrupt bit in SREG is ‘1’ the external interrupt INT1 is
enabled. The ISC11 and ISC10 of MCUCR register control when the interrupt is to be
activated(Rising or falling edge or level triggered)
• INT2:When this bit is ‘1’ and global interrupt bit in SREG is ‘1’ the external interrupt INT2 is
enabled. The ISC2 of MCUCSR register control when the interrupt is to be activated(Rising or
falling edge)
Interrupt Structure
• INT0 and INT1 can be edge triggered or level triggered but INT2 can only be edge
triggered.
• The MCUCR and MCUCSR registers decide the triggering options of the external hardware
interrupts INT0,INT1 and INT2.
Bit 1 – IVSEL: Interrupt Vector Select
• When the IVSEL bit is cleared (zero), the Interrupt Vectors are placed at the start of the
Flash memory. When this bit is set (one), the interrupt vectors are moved to the beginning
of the Boot Loader section of the Flash.
Bit 0 – IVCE: Interrupt Vector Change Enable
• The IVCE bit must be written to logic one to enable change of the IVSEL bit
Interrupt Structure
• MCUCR(MCU Control Register):The MCU Control Register contains control bits for
interrupt sense control and general MCU functions.
• ISC01 and ISC00(Interrupt Sense Control Bits):These bits define whether the signal should
be level or edge triggered at INT0 pin.
Interrupt Structure
• ISC11 and ISC10: These bits define whether the signal should be level or edge triggered at
INT1 pin.
The ATMega16 microcontroller has a flexible interrupt system that allows the user to
handle interrupt requests from various peripherals and external devices.
The interrupt structure of the ATMega16 consists of the following components:
1.Interrupt Vector Table: This is a table of memory addresses that correspond to each of
the interrupt sources. When an interrupt occurs, the microcontroller jumps to the
corresponding interrupt vector to handle the interrupt.
2.Interrupt Request (IRQ) Pin: The microcontroller has several interrupt request (IRQ) pins
that can be used to generate interrupt requests from external devices.
3.Interrupt Mask Register (IMR): This register allows the user to enable or disable specific
interrupt sources.
Interrupt Structure(CHAT GPT)
4.Interrupt Flag Register (IFR): This register holds the interrupt flags,
which indicate the status of the interrupt sources.
5.Interrupt Service Routine (ISR): This is a function that handles the
interrupt request. The ISR is executed when an interrupt occurs, and it
performs the necessary operations to process the interrupt.
To use the interrupt system of the ATMega16, the user must configure
the interrupt sources by setting the appropriate bits in the IMR and IFR
registers. The user must also define the ISR functions that correspond
to each interrupt source. When an interrupt occurs, the microcontroller
jumps to the corresponding ISR to handle the interrupt. After the ISR is
executed, the microcontroller returns to the main program.
Architecture(Chat GPT)
• The architecture of an AVR microcontroller is based on the Harvard architecture, which separates the data
memory and the program memory. The AVR architecture consists of the following components:
1. Central Processing Unit (CPU): The CPU is responsible for executing instructions, performing arithmetic and
logic operations, and managing the flow of the program.
2. Program Memory: The program memory stores the executable instructions that the CPU executes. It is usually
flash memory, which can be reprogrammed multiple times.
3. Data Memory: The data memory stores the variables and data used by the program. It includes both static and
dynamic RAM.
4. Input/Output (I/O) Ports: The AVR microcontroller has a number of I/O ports that can be used to interface with
external devices, such as sensors and actuators.
5. Timers/Counters: The AVR microcontroller has several timers and counters that can be used for timing and
counting purposes.
6. Analog-to-Digital Converter (ADC): The ADC converts analog signals to digital signals, which can be processed
by the CPU.
7. Serial Communication Interfaces: The AVR microcontroller has several serial communication interfaces, such as
USART, SPI, and I2C, that can be used for communication with other devices.
8. Interrupt Controller: The interrupt controller handles interrupt requests from external devices and peripherals,
allowing the CPU to respond to events in a timely manner.
9. Clock Generator: The clock generator generates the clock signal used by the microcontroller to synchronize its
operations.
• Overall, the architecture of an AVR microcontroller is designed to be simple, flexible, and efficient, making it a
popular choice for a wide range of applications.
PIN DESCRIPTION OF ATMEGA16
The ATMega16 is a popular AVR microcontroller that has the following pin description:
1.Power Pins: The ATMega16 has two power pins: VCC (positive power supply, typically 5V) and
GND (ground).
2.I/O Pins: The ATMega16 has up to 31 I/O pins that can be used as inputs or outputs. These
pins are labeled as PORT A, PORT B, PORT C, and PORT D.
3.Reset Pin: The reset pin (labeled RESET) is used to reset the microcontroller and is typically
connected to a push-button switch.
4.Oscillator Pins: The ATMega16 has two oscillator pins (XTAL1 and XTAL2) that are used to
connect an external crystal oscillator. This oscillator is used to generate the clock signal that
drives the microcontroller.
PIN DESCRIPTION OF ATMEGA16
5.Analog Input Pins: The ATMega16 has up to 6 analog input pins (labeled ADC0
to ADC5) that can be used with the built-in analog-to-digital converter (ADC).
6.Serial Communication Pins: The ATMega16 has several serial communication
pins that can be used for communication with other devices, including USART
(Universal Synchronous/Asynchronous Receiver/Transmitter), SPI (Serial
Peripheral Interface), and I2C (Inter-Integrated Circuit).
7.Interrupt Pins: The ATMega16 has several interrupt pins that can be used to
handle interrupt requests from external devices and peripherals.
8.Other Pins: The ATMega16 has several other pins, including a programming pin
(labeled MISO), a clock pin (labeled SCK), and a data pin (labeled MOSI), which
are used for programming the microcontroller using an in-system programmer.