Microcontroller:
Microcontroller: Microcontroller can be termed as a single on chip computer whichMicrocontroller can be termed as a single on chip computer which includes number of peripherals like RAM, EEPROM, Timers etc., required to perform includes number of peripherals like RAM, EEPROM, Timers etc., required to perform some predefined task.
some predefined task.
Architecture of AVR Microcontroller Architecture of AVR Microcontroller Does this mean that the
Does this mean that the microcontroller microcontroller is another name for a computer…? The answer is another name for a computer…? The answer is NO!
is NO!
The computer on one hand is designed to perform all the general purpose tasks on a The computer on one hand is designed to perform all the general purpose tasks on a single machine like you can use a computer to run a software to perform calculations or single machine like you can use a computer to run a software to perform calculations or you can use a computer to store some multimedia file or to access internet through the you can use a computer to store some multimedia file or to access internet through the browser, whereas the microcontrollers are meant to perform only the specific tasks, for browser, whereas the microcontrollers are meant to perform only the specific tasks, for e.g
e.g., ., swiswitchitching ng the AC the AC off off autautomatomaticalically ly whewhen n rooroom m temptemperaerature drops to ture drops to a a cercertaintain defined limit and again turning it ON when temperature rises above the defined limit. defined limit and again turning it ON when temperature rises above the defined limit.
There are number of popular families of microcontrollers which are used in different There are number of popular families of microcontrollers which are used in different applications as per their capability and feasibility to perform the desired task, most applications as per their capability and feasibility to perform the desired task, most
co
commommon n of of ththesese e araree 80518051,, AVRAVR andand PICPIC micrmicrocoocontrontrollersllers. . In In this article we this article we willwill introduce you with
introduce you with AVRAVR family of microcontrollers.family of microcontrollers.
History of AVR
History of AVR
AVRAVR was developed in the year 1996 by Atmel Corporation. The architecture of was developed in the year 1996 by Atmel Corporation. The architecture of AVRAVR was
was devdeveloeloped ped by by Alf-Alf-EgiEgil l BogBogen en and and VegVegard ard WollWollan. an. AVR deriveAVR derives s its its name from name from itsits develo
developers and pers and stands for stands for Alf-Egil Bogen Vegard Wollan RISC microcontroller Alf-Egil Bogen Vegard Wollan RISC microcontroller , also, also known as
known as AAdvanceddvanced VVirtualirtual RRISC. The AT90S8515 was the first microcontroller whichISC. The AT90S8515 was the first microcontroller which was based on
was based on AVR architectureAVR architecture howevehowever the r the first microcontrofirst microcontroller to ller to hit the hit the commerciacommerciall market was AT90S1200 in the year 1997.
market was AT90S1200 in the year 1997.
AVR microcontrollers
AVR microcontrollers are available in three categories:are available in three categories: 1.
1. TinyAVRTinyAVR – Less memory, small size, suitable only for simpler applications– Less memory, small size, suitable only for simpler applications 2.
2. MegaAVRMegaAVR – These are the most popular ones having good amount of memory– These are the most popular ones having good amount of memory (up
(upto to 25256 6 KBKB), ), highigheher r nunumbember r of of inbinbuiluilt t peperipripheherarals ls anand d susuitaitable ble fofor r modmodereratate e toto complex applications.
complex applications. 3.
3. XmegaAVRXmegaAVR – Used commercially for complex applications, which require large– Used commercially for complex applications, which require large program memory and high speed.
program memory and high speed.
The following table compares the above mentioned AVR series of microcontrollers: The following table compares the above mentioned AVR series of microcontrollers:
S
Seerriiees s NNaammee PPiinnss FFllaassh h MMeemmoorryy SSppeecciiaal l FFeeaattuurree TinyAVR
TinyAVR 66--3322 00..55--8 8 KKBB SSmmaalll l iin n ssiizzee MegaAVR
MegaAVR 2288--110000 44--225566KKBB EExxtteennddeed d ppeerriipphheerraallss XmegaAVR
XmegaAVR 4444--110000 1166--338844KKBB DDMMA A , , EEvveennt t SSyysstteemm included
What’s special about AVR?
What’s special about AVR?
ThThey ey arare e fafast:st: AVR AVR microcmicrocontroller ontroller exexececuteutes s momost st of of the the insinstrutructictionons s in in sinsinglglee execution cycle. AVRs are about 4 times faster than PICs, they consume less power execution cycle. AVRs are about 4 times faster than PICs, they consume less power and can be operated in different power saving modes. Let’s do the comparison between and can be operated in different power saving modes. Let’s do the comparison between the three most commonly used families of microcontrollers.
the three most commonly used families of microcontrollers.
8
8005511 PPIICC AAVVRR SPEED
SPEED SSllooww MMooddeerraattee FFaasstt MEMORY
MEMORY SSmmaallll LLaarrggee LLaarrggee ARCHITECTURE
ARCHITECTURE CCIISSCC RRIISSCC RRIISSCC ADC
ADC NNoot t PPrreesseenntt IInnbbuuiilltt IInnbbuuiilltt Timers
Timers IInnbbuuiilltt IInnbbuuiilltt IInnbbuuiilltt PWM Channels
PWM Channels NNoot t PPrreesseenntt IInnbbuuiilltt IInnbbuuiilltt
AVR is
AVR is an an 8-b8-bit it micrmicrocoocontrontroller ller belobelonginnging g to to the the famifamily ly of of RedReduceuced d InstInstructruction ion SetSet Computer (
Computer (RISCRISC). In RISC architecture the instruction set of the computer are not only). In RISC architecture the instruction set of the computer are not only fe
fewewer r in in nunumbmber er bubut t alalso so sisimpmpleler r aand nd fafaststeer r in in opopereratatioion. n. ThThe e oothther er tytype pe oof f categorization is CISC (Complex Instruction Set Computers). We will explore more on categorization is CISC (Complex Instruction Set Computers). We will explore more on this when we will learn about the architecture of AVR microcontrollers in following this when we will learn about the architecture of AVR microcontrollers in following section.
section.
Let’s see what all this means. What is 8-bit? This means that the microcontroller is Let’s see what all this means. What is 8-bit? This means that the microcontroller is capable of transmitting and receiving 8-bit data.
capable of transmitting and receiving 8-bit data. The input/outpThe input/output registers available areut registers available are of 8-bits. The AVR family controllers have register based architecture which means that of 8-bits. The AVR family controllers have register based architecture which means that bo
both th ththe e opoperaerandnds s fofor r an an opoperaeratiotion n arare e stostorered d in in a a reregisgisteter r anand d the resuthe result lt of of thethe op
performing OR operation between two input registers and storing the value in Output performing OR operation between two input registers and storing the value in Output Register.
Register.
The CPU takes values from two input registers INPUT-1 and INPUT-2, performs the The CPU takes values from two input registers INPUT-1 and INPUT-2, performs the logical operation and stores the value into the OUTPUT register. All this happens in 1 logical operation and stores the value into the OUTPUT register. All this happens in 1 execution cycle.
execution cycle.
In our journey with the AVR we will be
In our journey with the AVR we will be working onworking on Atmega16 Atmega16 microcontroller, which is amicrocontroller, which is a 40-pin IC and belongs to the megaAVR category of AVR family. Some of the features of 40-pin IC and belongs to the megaAVR category of AVR family. Some of the features of Atmega16 are:
Atmega16 are: ·
· 16KB 16KB of of Flash Flash memorymemory ·· 1KB of SRAM1KB of SRAM
·· 512 Bytes of EEPROM512 Bytes of EEPROM ·· Available in 40-Pin DIP Available in 40-Pin DIP
·· 8-Channel 10-bit ADC8-Channel 10-bit ADC ·· Two 8-bit Timers/CountersTwo 8-bit Timers/Counters ·· One 16-bit Timer/Counter One 16-bit Timer/Counter ·· 4 PWM Channels4 PWM Channels
·· In System Programmer (ISP)In System Programmer (ISP) ·· Serial USARTSerial USART
·· SPI InterfaceSPI Interface
·· Digital to Analog Comparator.Digital to Analog Comparator.
Architecture of AVR
Architecture of AVR
The AVR microcontrolleThe AVR microcontrollers are rs are based on the advanced RISC architecture and consist of based on the advanced RISC architecture and consist of 32 x 8-bit general purpose working registers. Within one single clock cycle, AVR can 32 x 8-bit general purpose working registers. Within one single clock cycle, AVR can take inputs from two general purpose registers and put them to ALU for carrying out the take inputs from two general purpose registers and put them to ALU for carrying out the requested operation, and transfer back the result to an arbitrary register. The ALU can requested operation, and transfer back the result to an arbitrary register. The ALU can perform arithmetic as well as logical operations
perform arithmetic as well as logical operations over the inputs from
over the inputs from the register or between the register and a the register or between the register and a constanconstant. Single register t. Single register operations like taking a complement can also be executed in ALU. We can see that operations like taking a complement can also be executed in ALU. We can see that AVR does not have any register like accumulator as in 8051 family of microcontrollers; AVR does not have any register like accumulator as in 8051 family of microcontrollers; the operations can be performed between any of the registers and can be stored in the operations can be performed between any of the registers and can be stored in either of them.
either of them.
AVR follow
AVR follows s HarHarvarvard d ArchArchitecitecture format ture format in in whicwhich h the the procprocessessor or is is equiequippepped d withwith separate memories and buses for Program and the Data information. Here while an separate memories and buses for Program and the Data information. Here while an inst
instructruction ion is is beinbeing g exeexecutecuted, d, the the next next instinstructruction ion is is prepre-fet-fetcheched d from the from the proprogramgram memory.
Since AVR can perform single cycle execution, it means that AVR can execute 1 million Since AVR can perform single cycle execution, it means that AVR can execute 1 million ins
instrutructictionons s peper r sesecocond nd if if cycycle cle frfreqequeuency ncy is is 1MH1MHz. z. ThThe e highigheher r is is the the opopereratiatingng frequency of the controller, the higher will be its processing speed. We need to optimize frequency of the controller, the higher will be its processing speed. We need to optimize the power consumption with processing speed and hence need to select the operating the power consumption with processing speed and hence need to select the operating frequency accordingly.
frequency accordingly.
There are two flavors for Atmega16 microcontroller: There are two flavors for Atmega16 microcontroller: 1.
1. Atmega16Atmega16:- Operating frequency range is 0 – 16 MHz.:- Operating frequency range is 0 – 16 MHz. 2.
2. Atmega16LAtmega16L:- Operating frequency range is 0 – 8 MHz.:- Operating frequency range is 0 – 8 MHz. If we are using a crystal of 8 MHz = 8 x 10
If we are using a crystal of 8 MHz = 8 x 1066 Hertz = 8 Million cycles, then AVR canHertz = 8 Million cycles, then AVR can
execute 8 million instructions. execute 8 million instructions.
Naming Convention.!
Naming Convention.!
TheThe ATAT refers to Atmel the manufacturer,refers to Atmel the manufacturer, MegaMega means that the microcontroller belongmeans that the microcontroller belong to MegaAVR category,
Architecture Diagram: Atmega16
Architecture Diagram: Atmega16
Following points explain the building blocks of
Following points explain the building blocks of Atmega16 architectureAtmega16 architecture::
·· I/O Ports:I/O Ports: Atmega16 has four (PORTA, PORTB, PORTC and PORTD)Atmega16 has four (PORTA, PORTB, PORTC and PORTD) 8-bit8-bit input- input-output ports.
output ports.
·· Internal Calibrated Oscillator:Internal Calibrated Oscillator: Atmega16 is equipped with an internal oscillator for Atmega16 is equipped with an internal oscillator for driving its clock. By default Atmega16 is set to
driving its clock. By default Atmega16 is set to operate at internal calibratoperate at internal calibrated oscillator of ed oscillator of 1 MHz.
1 MHz. The maximum frequency of The maximum frequency of internainternal l oscillatooscillator r is 8Mhz. is 8Mhz. AlternatAlternatively, ATmega16ively, ATmega16 can be
can be opeoperaterated d usinusing g an an extexternaernal l cryscrystal oscillatal oscillator tor with a with a maximaximum mum freqfrequencuency y of of 16MHz. In this case you need to modify the fuse bits. (Fuse Bits will be explained in a 16MHz. In this case you need to modify the fuse bits. (Fuse Bits will be explained in a separate tutorial).
Archit Archit ecture of ATmega16 - AVR Microcontrollers
ecture of ATmega16 - AVR Microcontrollers
·· ADC Interface:ADC Interface: Atmega16 is equipped with an 8 channelAtmega16 is equipped with an 8 channel ADC ADC ((Analog to DigitalAnalog to Digital Converter
Converter ) with a resolution of 10-bits. ADC reads the analog input for e.g., a sensor ) with a resolution of 10-bits. ADC reads the analog input for e.g., a sensor in
inpuput t anand d coconvnvererts ts it it ininto to didigigitatal l ininfoformrmatatioion n whwhicich h is is unundedersrstatandndabable le by by ththee microcontroller.
microcontroller.
·· Timers/Counters:Timers/Counters: Atmega16 consists of two 8-bit and one 16-bit timer/counter. TimersAtmega16 consists of two 8-bit and one 16-bit timer/counter. Timers are useful for generating precision actions for e.g., creating time delays between two are useful for generating precision actions for e.g., creating time delays between two operations.
operations.
·· WatchdoWatchdog g Timer:Timer: Watchdog timer Watchdog timer is present with internal oscillator. Watchdog timer is present with internal oscillator. Watchdog timer continuously monitors and resets the controller if the code gets stuck at any execution continuously monitors and resets the controller if the code gets stuck at any execution action for more than a defined time interval.
·· InterruptsInterrupts:: Atmega16 consists of 21 interrupt sources out of which four are external.Atmega16 consists of 21 interrupt sources out of which four are external. The remaining are internal interrupts which support the peripherals like USART, ADC, The remaining are internal interrupts which support the peripherals like USART, ADC, Timers etc.
Timers etc.
·· USARTUSART:: UniversUniversal al SynchroSynchronous and nous and AsynchAsynchronouronous s ReceiveReceiver r and Transmitter and Transmitter inte
interfarface ce is is avaavailabilable le for for inteinterfacrfacing ing with with extexternaernal l devdevice ice capacapable ble of of commcommunicunicatinatingg serially (data transmission bit by bit).
serially (data transmission bit by bit).
General Purpose Registers:
General Purpose Registers: Atmega16 is equipped with 32 general purpose registersAtmega16 is equipped with 32 general purpose registers which are coupled directly with the Arithmetic Logical Unit (ALU) of CPU.
which are coupled directly with the Arithmetic Logical Unit (ALU) of CPU.
·· Memory:Memory: Atmega16 consist of three different memory sections:Atmega16 consist of three different memory sections:
1.
1. Flash EEPROMFlash EEPROM: Flash EEPROM or simple flash memory is used to : Flash EEPROM or simple flash memory is used to store the programstore the program du
dumpmped ed or or buburnrnt t by by ththe e ususer er on on to to ththe e mimicrcrococonontrtrololleler. r. It It cacan n be be eaeasisily ly ererasaseded electrically as a single unit. Flash memory is non-volatile i.e., it retains the program even 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 if the power is cut-off. Atmega16 is available with 16KB of in system programmable Flash EEPROM.
Flash EEPROM.
2.
2. Byte Addressable EEPROMByte Addressable EEPROM: This is : This is also a nonvolatile memory used to store data likealso a nonvolatile memory used to store data like values of certain variables. Atmega16 has 512 bytes of EEPROM, this memory can be values of certain variables. Atmega16 has 512 bytes of EEPROM, this memory can be useful for storing the lock code if we are designing an application like electronic door useful for storing the lock code if we are designing an application like electronic door lock.
3.
3. SRAMSRAM: Static Random Access Memory, this is the volatile memory of microcontroller : Static Random Access Memory, this is the volatile memory of microcontroller i.e., data is lost as soon as power is turned off. Atmega16 is equipped with 1KB of i.e., data is lost as soon as power is turned off. Atmega16 is equipped with 1KB of internal SRAM. A small portion of SRAM is set aside for general purpose registers used internal SRAM. A small portion of SRAM is set aside for general purpose registers used by CPU and some for the peripheral subsystems of the microcontroller.
by CPU and some for the peripheral subsystems of the microcontroller.
·· ISP:ISP: AVR family of controllers haveAVR family of controllers have In System ProgrammableIn System Programmable Flash Memory whichFlash Memory which can be programmed without removing the IC from the circuit, ISP allows to reprogram can be programmed without removing the IC from the circuit, ISP allows to reprogram the controller while it is in the application circuit.
the controller while it is in the application circuit.
·· SPI:SPI: Serial Peripheral InterfaceSerial Peripheral Interface, SPI port is used for serial communication between, SPI port is used for serial communication between two devices on a
two devices on a common clock sourcecommon clock source. The . The data transmissidata transmission rate of on rate of SPI is more thanSPI is more than that of USART.
that of USART.
·· TWI: Two Wire InterfaceTWI: Two Wire Interface (TWI) can be used to set up a network of devices, many(TWI) can be used to set up a network of devices, many dev
devices can ices can be be conconnecnected over ted over TWI interfTWI interface forming a ace forming a netnetworwork, k, the devicethe devices s cancan simultaneously transmit and receive and have their own unique address.
simultaneously transmit and receive and have their own unique address.
·· DAC:DAC: Atmega16 is also equipped with aAtmega16 is also equipped with a Digital to Digital to Analog Converter Analog Converter (DAC) interface(DAC) interface which can be used for reverse action performed by ADC. DAC can be used when
which can be used for reverse action performed by ADC. DAC can be used when therethere is a need of converting a digital signal to analog signal.
various microcontroller of MegaAVR series:
various microcontroller of MegaAVR series:
ATmega8 andATmega8 and Atmega32 Atmega32 are other members of MegaAVR series controllers. They areare other members of MegaAVR series controllers. They are quite similar to
quite similar to ATmega16 ATmega16 in architecture. Low in architecture. Low power version MegaAVR controllers arepower version MegaAVR controllers are also available in markets. The following table shows the comparison between different also available in markets. The following table shows the comparison between different members of MegaAVR family:
members of MegaAVR family:
Part Part Name Name
R
ROOMM RRAAMM EEEEPRPROOMIMI//00 Pins Pins
Tim
Timerer IntInterruerruptpt s s Operatio Operatio n n VoltageVoltage Operatin Operatin g g frequenc frequenc y y Packagin Packagin g g ATmega8 ATmega8 88KKBB 11KKBB 551122BB 2233 33 1199 44..55--55..5 5 VV 00--116 6 MMHHz 2z 288 ATmega8 ATmega8 L L 8 8KKBB 11KKBB 551122BB 2233 33 1199 22..77--55..5 5 VV 00--8 8 MMHHzz 2288 ATmega1 ATmega1 6 6 1 166KKB 1B 1KKBB 551122BB 3232 33 2211 44..55--55..5 5 VV 00--116 6 MMHHz 4z 400 ATmega1 ATmega1 6L 6L 1 166KKB 1B 1KKBB 551122BB 3232 33 2211 22..77--55..5 5 VV 00--8 8 MMHHzz 4400 ATmega3 ATmega3 2 2 3 322KKB 2B 2KKBB 11KKBB 3322 33 2121 44..55--55..5 5 VV 00--116 6 MMHHz 4z 400 ATmega3 ATmega3 2L 2L 3 322KKB 2B 2KKBB 11KKBB 3322 33 2121 22..77--55..5 5 VV 00--8 8 MMHHzz 4400