FESD Unit2 AVR PDF
FESD Unit2 AVR PDF
FESD Unit2 AVR PDF
Gaurav Verma
Assistant Professor
Department of Electronics and Communication Engineering
Jaypee Institute of Information and Technology
Sector-62, Noida, Uttar Pradesh, India
1 copyright@gauravverma
History of AVR Series Microcontrollers
Designed by Alf-Egil Bogen and Vegard Wollan at
Norwegin Institute of Technology(NTH).
Developed in the year 1996 by Atmel Corporation.
AdvancedVirtual RISC.
copyright@gauravverma
What’s special about AVR?
copyright@gauravverma
Features of Atmega16
8 bit Controller
Has 40 pins
• 32 pins for I/O
• 8 pins reserved
Clock Frequency: 16Mhz
I/O pins divided into 4 groups
• Named as Ports.
• Four ports A,B,C and D.
16Kb Flash memory
512Bytes EEPROM
1Kb Internal SRAM
copyright@gauravverma
Features of ATmega16 cont.…..
Three inbuilt timers
• Two 8-bit
• One 16-bit
Operating Voltages
• 2.7V - 5.5V (ATmega16L)
• 4.5V - 5.5V (ATmega16)
Maximum programmable Cycles:
• 10,000 for Flash memory
• 100,000 for EEPROM
copyright@gauravverma
Detailed Pin Diagram of ATmega16
copyright@gauravverma
PIN DIAGRAM of ATmega16
copyright@gauravverma
Architecture of AVR
Advanced RISC CPU architecture
Consist of 32 x 8-bit general purpose working
registers.
AVR does not have any register like accumulator
as in 8051 family of microcontrollers
AVR follows Harvard Architecture for Memory
Access.
copyright@gauravverma
Memory Architecture
Memories
RAM ROM
EEPROM Flash
copyright@gauravverma
RAM(Random Access Memory)
copyright@gauravverma
GPRs and ALU
copyright@gauravverma
PC(Program Counter)
copyright@gauravverma
Harvard Architecture in AVR
copyright@gauravverma
Abstract Architecture of Atmega16
copyright@gauravverma
Registers?
Registers are the links between our code and the
hardware (pins).
They actually are the memory locations inside the μC
whose names and sizes are predefined.
I/O Pins of a Microcontroller are divided generally into
groups of 8 pins called PORT.
Each PORT contains some registers associated with it.
copyright@gauravverma
Registers for I/O Ports
Registers DDRx
PINx
PORTx
copyright@gauravverma
DDR(Data Direction Register)
It is 8-bit register which sets the pins either input or
output. Each bit of the register corresponds to a
pin.
0 - Sets the corresponding pin INPUT
1 - Sets the corresponding pin OUTPUT
If the pin is input, then the voltage at that pin is
undecided until an external voltage is applied.
If the pin is output, then the voltage at that pin is fixed to
a particular value (5V or 0).
PORT Register
PORT is also an 8 bit register. The bits on the PORT
register correspond to the pins of the associated port in
the same manner as in the case of the DDR register.
copyright@gauravverma
Pull Up register concept
copyright@gauravverma
Pin Register
PIN is a register whose value can be read, but cannot be
changed inside the program.
It gives the value of the actual voltage at a particular pin. 5V
corresponds to 1, and 0 corresponds to 0.
copyright@gauravverma
Summary
copyright@gauravverma
Interrupts
Gaurav Verma
Assistant Professor
Department of Electronics and Communication Engineering
Jaypee Institute of Information and Technology
Sector-62, Noida, Uttar Pradesh, India
25 copyright@gauravverma
INTERRUPTS
An interrupt is an external or internal event that interrupts
the microcontroller to inform it that a device needs its
service
26 copyright@gauravverma
Interrupt Vs Polling
1. Interrupts
Whenever any device needs its service, the device notifies the
microcontroller by sending it an interrupt signal.
Upon receiving an interrupt signal, the microcontroller interrupts
whatever it is doing and serves the device.
The program which is associated with the interrupt is called the
interrupt service routine (ISR) or interrupt handler.
2. Polling
The microcontroller continuously monitors the status of a given
device.
When the conditions met, it performs the service.
After that, it moves on to monitor the next device until every one is
serviced.
27 copyright@gauravverma
Interrupts in ATmega16
1. Reset – power-up reset.
29
copyright@gauravverma
ATmega16 Interrupt related
Registers
The various registers associated with the use of external
interrupts are:
31 copyright@gauravverma
General Interrupt Control register
(GICR)
32 copyright@gauravverma
MCU Control register (MCUCR)
33 copyright@gauravverma
MCU Control & Status register
(MCUCSR)
34 copyright@gauravverma
General Interrupt Flag Register
(GIFR)
35 copyright@gauravverma
Interrupt Priority
If two interrupts are activated at the same time, the interrupt with the higher
priority is served first.
The priority of each interrupt is related to the address of the interrupt
defined in the interrupt vector table.
36 copyright@gauravverma
Interrupt Vector Table
37
copyright@gauravverma
Interrupt Programming in C
Interrupt include file: #include<avr\interrupt.h>
Cli() and sei() macros to clear and set the D7 bit of SREG register.
Defining ISR:
ISR (interrupt vector name)
{
//our interrupt handler program
}
38 copyright@gauravverma
Interrupt Vector Name defined in WinAVR
39
copyright@gauravverma
Interrupt Interface using INT0
copyright@gauravverma
40
Toggle the LED connected on PORTC using
external interrupt INT0 (PORTD 2)
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
int main(void)
{
DDRC=(1<<7); /* Make PORTC as output PORT*/
PORTC=0;
DDRD=(0<<2); /* PORTD as input */
PORTD=0xFF; /* Make pull up high */
GICR = 1<<INT0; /* Enable INT0*/
MCUCR = (1<<ISC01 | 1<<ISC00); /* Trigger INT0 on rising edge */
sei(); /* Enable Global Interrupt */
while(1);
}
ISR(INT0_vect)
{
PORTC=~PORTC; /* Toggle PORTC */
_delay_ms(50); /* Software debouncing control delay */
copyright@gauravverma
41}
Timers/Counter
Gaurav Verma
Assistant Professor
Department of Electronics and Communication Engineering
Jaypee Institute of Information and Technology
Sector-62, Noida, Uttar Pradesh, India
42 copyright@gauravverma
Timers
Timer is special register that can
be 8-16 bit so capable of holding
from 0 to 255-65536
copyright@gauravverma
copyright@gauravverma
One of basic condition when Timer overflow(
it means timer counted up to its maximum
value and return back to zero).
copyright@gauravverma
Resolution:
is the smallest period taken by timer to take one
count we can calculate it through formula
Resolution = (1/Frequency)
copyright@gauravverma
In case you want to increase the time needed for
specific operation we use timer prescalar
Prescalar divide the clock frequency and
produce timer clock
Prescalar modes are
No prescalar (Timer Clock).
Clock/8.
Clock/64. Note
Clock/256. CPU Clock doesn’t
Clock/512. affected by reduction
Clock/1024. using prescalar.
prescalar.
NO clock. It remain the same
copyright@gauravverma
Example for using prescalar :
If you using 1 MHZ for CPU Clock without using
prescalar timer takes to count up till it overflow 256
µs
But when you use prescalar 1024, timer takes 0.26 sec
to count up till it overflow
It’s easy to know the time timer need to overflow if
you know Resolution.
Resolution = (1/Frequency)
So if microcontroller is clocked with 1MHz source,
then 8 bit timer without prescaler will run with
resolution:
Resolution=1/1MHz=1µs
copyright@gauravverma
So if timer take 256 counts to overflow then it
takes :
copyright@gauravverma
Timers in ATmega16
ATmega16/32 have 3 Timer/counter we have
three Timers/Counters each one with Separate
Prescaler.
Timer 0 - 8 bit timer.
Timer 1 -16 bit timer.
Timer 2 - 8 bit timer.
Timer can also be operated in 3 modes :
Normal mode (Overflow).
Clear Timer Compare mode(CTC ).
Pulse Width Modulation mode(PWM).
Timer in Atmega16
copyright@gauravverma
53 Copyright to gaurav verma
Timer 0 Register
Timer/Counter Control Register – TCCR0
TOV0:
The bit TOV0 is set (one) when an overflow occurs in Timer/Counter0.
TOV0 is cleared by hardware when executing the corresponding
interrupt handling vector. When the SREG I-bit, TOIE0
(Timer/Counter0 Overflow Interrupt Enable), and TOV0 are set (one),
the Timer/Counter0 Overflow interrupt is executed.
OCF0
The OCF0 bit is set (one) when a compare match occurs between the
Timer/Counter0 and the data in OCR0 – Output Compare Register0.
OCF0 is cleared by hardware when executing the corresponding
interrupt handling vector. When the I-bit in SREG, OCIE0
(Timer/Counter0 Compare Match Interrupt Enable), and OCF0 are set
(one), the Timer/Counter0 Compare Match Interrupt is executed.
Timer 0 Modes
Normal Mode (Overflow)
A timer overflow means that the counter(TCTNx)
has counted up to its maximum value and is reset to
zero in the next timer clock cycle.
The resolution of the timer determines the
maximum value of that timer.
The timer overflow event causes the Timer Overflow
Flag (TOVx) to be set in the Timer Interrupt Flag
Register (TIFR).
Timer 0 Modes
Timer compare mode :
In compare mode we load a register called Output Compare
Register OCRx with a value of our choice and the timer will
compare the current value of timer TCTNx with that of Output
Compare Register continuously and when they match the
following things can be configured to happen:
Gaurav Verma
Assistant Professor
Department of Electronics and Communication Engineering
Jaypee Institute of Information and Technology
Sector-62, Noida, Uttar Pradesh, India
67 copyright@gauravverma
Motor
DC Supply
DC Supply
DC Supply
50% Duty Cycle
25% Duty Cycle
75% Duty Cycle
PWM Generation
Modes of PWM
Inverted Mode(A)
Non-Inverted Mode(B)
Toggle Mode
PWM
PWM Modes of Operation
Fast PWM
Phase Correct PWM
Frequency and Phase Correct PWM
Fast PWM
Phase Correct PWM
TCCR0
Gaurav Verma
Assistant Professor
Department of Electronics and Communication Engineering
Jaypee Institute of Information and Technology
Sector-62, Noida, Uttar Pradesh, India
Types of Communication
Computers transfer data in two ways:
Parallel: Often 8 or more lines (wired conductors) are used to transfer
data to a device that is only a few feet away. Example: PCI, ARM Bus.
87 copyright@gauravverma
How data sent serially
Basics of Serial Communication
Serial data communication uses two methods
Synchronous method transfers a block of data at a time
89 copyright@gauravverma
Asynchronous – Start & Stop Bit
Asynchronous serial data communication is widely used for
character-oriented transmissions
Each character is placed in between start and stop bits, this is called
framing.
Block-oriented data transfers use the synchronous method.
The start bit is always one bit, but the stop bit can be one or
two bits
90 copyright@gauravverma
Asynchronous – Start & Stop Bit
91 copyright@gauravverma
Data Transfer Rate
The rate of data transfer in serial data communication is stated in bps
(bits per second).
92 copyright@gauravverma
Communication Modes
Serial Communication Protocols
SPI – Serial Peripheral Interface
Three wire interface
Slave Select line for communication of multiple ICs
I2C – Inter-Integrated Circuit
Invented by Philips
Advanced USART
Two wire interface
FireWire
Developed by Apple Computers
High speed busses capable of Audio/Video Transmission
FireWire
Ethernet:
Used mostly in LAN connections
bus consists of 8 lines, or 4 Tx/Rx pairs
Universal serial bus (USB):
RS-232 :
Recommended Standard 232
typically connected using a DB9
Total 9 pins (3 output+5 input + GND)
Approved by the Electronic Industries Association
Interfacing IC
98 copyright@gauravverma
Interfacing to PC through RS232
99 copyright@gauravverma
How to program on chip USART
Initialize USART
Set baud rate value : 9600
Register Used: UBRR (USART Baud Rate Register)
Enable Receive & Transmit mode
Register Used: UCSRB (USART Control & Status Register)
Set the 8 bit frame format
Register Used: UCSRC (USART Control & Status Register)
Transmit Data
Put data in UDR Register and check UDRE bit in UCSRA register
until all the data gets transmitted.
Receive Data
Check RXC bit in UCSRA register until all the data get received in
UDR register.
100 copyright@gauravverma
UBBR(USART Baud Rate Register)
UCSRB: USART Control and Status Register B
Gaurav Verma
Assistant Professor
Department of Electronics and Communication Engineering
Jaypee Institute of Information and Technology
Sector-62, Noida, Uttar Pradesh, India