5c AVR Timer (16 Bit)
5c AVR Timer (16 Bit)
5c AVR Timer (16 Bit)
Lecture 5C
16-bit Timer/Counter
Following registers associated with the timer/counter1 are
all 16 bit
Timer/Counter1 Register – TCNT1 (TCNT1H & TCNT1L)
Output Compare Registers A – OCR1A (OCR1AH & OCR1AL)
Output Compare Registers B – OCR1B (OCR1BH & OCR1BL)
Input Capture Register – ICR1 (ICR1H & ICR1L)
The TCNT1, OCR1A, OCR1B, and ICR1 are 16-bit
registers that can be accessed by the AVR CPU via the
8-bit data bus
2 / 78
16-bit Timer/Counter
Unlike 8-bit timers/counters the 16-bit timer/counter1
has two 8-bit timer/counter control registers
Timer/Counter1 Control Register A – TCCR1A
Timer/Counter1 Control Register B – TCCR1B
Just like 8-bit timers, 16-bit timer is also started by
selecting the clock source
The bit which select the clock source for the timer are
situated in TCCR1B
3 / 78
Accessing 16-bit Registers
WRITE OPERATION
16-bit timer has a single 8-bit register for temporary
storing of the high byte of the 16-bit access
The same temporary register is shared between all 16-
bit registers within 16-bit timer
First high byte is written, which is stored in the
temporary register
Writing the low byte, triggers the 16-bit write operation
to the actual register
When the low byte of a 16-bit register is written by the
CPU, the high byte stored in the temporary register and
the low byte written are both copied into the 16-bit
register in the same clock cycle
4 / 78
Accessing 16-bit Registers (Contd.)
READ OPERATION
When the low byte of a 16-bit register is read by the
CPU, the high byte of the 16-bit register is copied into
the temporary register in the same clock cycle as the
low byte is read
And then the high byte is read from the temporary
register
5 / 78
Accessing 16-bit Registers
6 / 78
16-bit Register Read/Write Summary
To write a 16-bit register
The high byte must be written before the low
byte
To read a 16-bit register
The low byte must be read before the high
byte
Note:- While using “C”, the compiler handles
the 16-bit access, but if you want to
write/read Low and high byte separately
then above rules have to be followed
7 / 78
16-bit Register Read/Write - Assembly
.include "M16def.inc"
ldi r16,0x80
out tcnt1h, r16 ;Write TH
ldi r17,0xc1
out tcnt1l, r17 ; Write TL
8 / 78
16-bit Register Read/Write - C
int main()
{ unsigned int x;
unsigned char high=0, low=0;
TCNT1=0xC180;
x= TCNT1;
10 / 78
Registers Associated with Timers
Timer/Counter1 Control Register A – TCCR1A
11 / 78
Registers Associated with Timers
Timer/Counter1 – TCNT1H and TCNT1L
12 / 78
Registers Associated with Timers
Output Compare Register 1 A – OCR1AH and OCR1AL
13 / 78
Registers Associated with Timers
Timer/Counter Interrupt Flag Register – TIFR
14 / 78
PWM PINs of Timer 1
15 / 78
Timers/Counters in Atmega16
16 / 78
Timers/Counters in Atmega16
17 / 78
Selecting the Required Mode
Timer/Counter1 Control Register A – TCCR1A
18 / 78
Selecting the Required Mode
19 / 78
Normal Mode
TCCR1A and TCCR1B(Timer/counter1 Control Registers)
WGM12=0
WGM11=0
WGM10=0
20 / 78
Normal Mode
21 / 78
Selecting the Required Mode
22 / 78
Pre-Scaler
23 / 78
Normal Mode
Normal Mode(WGM13:WGM10=0000)
The simplest mode of operation is the normal mode
In this mode the counting direction is always up
(incrementing)
The counter simply overruns when it passes its
maximum 16-bit value (TOP = 0xFFFF) and then restarts
from the bottom (0x00)
In normal operation the Timer/Counter Overflow Flag
(TOV1) will be set in the same timer clock cycle as the
TCNT1 becomes zero
This mode can be used to create delays or measure
time between two events.
24 / 78
Normal Mode
An interrupt can be generated each time the counter value
overflows by using the TOV1 flag
If the interrupt is enabled, the interrupt handler routine can
be used for updating (reloading) the TCNT1 value
25 / 78
Calc No for TCNT1 Register
Procedure
Nc = Time of delay (us) x Crystal freq (MHz)
If Nc < 65536 use Timer1 without prescaler else use prescaler.
Nr = 65536 – Nc (value to be loaded in TCNT1)
In case of Prescaler
Np = Nc / prescaler
Nr = 65536- Np
26 / 78
Example
Use Timer1 in normal mode to create a delay of
5ms. Controller frequency is 8mhz.
Solution
Nc = 5000 x 8 = 40000
Nc<65536, hence no prescaler is required.
Nr = 65536-40000 = 25536
TCNT1=Nr=25536;
27 / 78
Example
Generate a delay of 0.5s using Timer1. Controller
frequency is 8Mhz. Find Prescaler and TCNT1 value
Solution
Nc = Time of delay (us) x Crystal freq (MHz)
Nc = 500,000x8 =40,00,000
NC > 65536 use Timer1 with prescaler
Np = Nr / prescaler
Np = 40,00,000 / 8=500,000
Since, Np > 65536 we choose the next prescaler value
Np = 40,00,000 / 64=62500
Nr = 65536-62500 = 3036
28 / 78
Example
Generate a delay of 5ms using Timer0. Controller frequency is
8Mhz.
Procedure
We assume that the timer is loaded with zero to generate the delay. From
this assumption we then calculate the approximate timer period
Approx Timer period = delay/256 = T
Approx Timer Frequency = 1/T =F
Approx Prescaler = 8M/F =N
From the table we then choose the nearest value of prescaler greater than
“N”
From this value of N we redo the calculations to calculate the value to be
loaded in the Timer.
29 / 78
Example
Solution
Approximate timer period = 5m/256 = 19.53us
Approximate Timer Frequency = 1/19.53u = 51.2kHz
Approximate Prescaler = 8M/51.2k = 156.25
Hence presclaer of 256 or 1024 can be chosen. We choose 256.
Timer Frequency = 8m/256 = 31.25k
Timer Period = 1/31.25k = 32us
Timer counts Req. = 5m/32u = 156
TCNT0 = 256-156 = 100
30 / 78
Example
31 / 78
Example(using interrupt)
#include<avr/io.h>
#include<avr/interrupt.h>
int main()
{
DDRB=0xFF;
TCNT1=0xC180;
TIMSK=0x04;
SREG|=0x80;
TCCR1A=0x00;
TCCR1B=0x01;
while(1)
{
}
}
ISR(TIMER1_OVF_vect)
{ PORTB=PORTB^0x10;
TCNT1=0xC180;
} AVR_16bit_normal_Int
32 / 78
Example
Program Timer 1 to generate a square wave
of frequency 3khz on pin PB4, using normal
mode. Clk freq= 8Mhz.
33 / 78
Solution
Calculate value of TCNT1
Tperiod = 1/f = 1/3k = 1/3 ms
Hence required Delay :
d=(1/3)/2 = 1/6 ms
34 / 78
Code
#include<avr/io.h>
#include<avr/interrupt.h>
int main()
{
DDRB=0xFF;
TCNT1=64203;
TIMSK=0x04;
TIFR|=0x04;
SREG|=0x80;
TCCR1A=0x00;
TCCR1B=0x01;
while(1)
{
}
}
ISR(TIMER1_OVF_vect)
{ PORTB=PORTB^0x10;
TCNT1=64203;
}
35 / 78
Clear Timer on Compare Match
TCCR1A and TCCR1B(Timer/counter1 Control Registers)
36 / 78
Clear Timer on Compare Match
WGM13:WGM10=0100
WGM13:WGM10=1100
37 / 78
Selecting the Required Mode
38 / 78
Clear Timer on Compare Match
39 / 78
Clear Timer on Compare Match
In Clear Timer on Compare or CTC mode the OCR1A/ICR1
Register is used to manipulate the TIMER/COUNTER
resolution
In CTC mode the counter is cleared to zero when the counter
value (TCNT1) matches the OCR1A or ICR1
The OCR1A/ICR1 defines the top value for the counter, hence
also its resolution
If a frequency other than those available using the prescaler
options is required, this mode can be used.
The frequency is calculated by
40 / 78
Clear Timer on Compare Match(CTC)
An interrupt can be generated each time the counter value
reaches the TOP value by using the OCF1A/ICF1 flag
If the interrupt is enabled, the interrupt handler routine can be
used for updating the TOP value
Changing TOP to a value close to BOTTOM when the counter
is running with none or a low prescaler value must be done
with care since the CTC mode does not have the double
buffering feature. If the new value written to OCR1A/ICR1 is
lower than the current value of TCNT1, the counter will miss
the compare match. The counter will then have to count to its
maximum value (0xFFFF) and wrap around starting at 0x0000
before the compare match can occur
41 / 78
Example
Write a C program to toggle only the PORTB.4 bit
continuously every 2ms. Use Timer1, CTC mode and no
Prescaler to create the delay. Assume XTAL= 8 MHz.
42 / 78
Example(using interrupt)
#include<avr/io.h> #include<avr/io.h>
#include<avr/interrupt.h> #include<avr/interrupt.h>
int main() int main()
{ {
DDRB=0xFF; DDRB=0xFF;
TCCR1A=0x00; TCCR1A=0x00;
TCCR1B=0x09; TCCR1B=0x01;
OCR1A=0x3E80; TCNT1=0xC180;
TIMSK=0x10; TIMSK=0x04;
SREG|=0x80; SREG|=0x80;
while(1) while(1)
{ {
} }
} }
ISR(TIMER1_COMPA_vect) ISR(TIMER1_OVF_vect)
{ PORTB=PORTB^0x10; { PORTB=PORTB^0x10;
} TCNT1=0xC180;
}
43 / 78
Example
Program Timer 1 to generate a square wave
of frequency 20khz on pin PB4, using CTC
mode. Clk freq= 8Mhz.
Calc OCR1A = ?
Use N=1 (No prescaler)
44 / 78
Solution
Using
OCR1A= 8000/(2*20) - 1
OCR1A = 199
45 / 78
Code
#include<avr/io.h>
#include<avr/interrupt.h>
int main()
{
DDRB=0xFF;
TCCR1A=0;
TCCR1B=0x09;
OCR1A=199;
TIMSK=0x10;
SREG|=0x80;
while(1)
{
}
}
ISR(TIMER1_COMPA_vect)
{ PORTB=PORTB^0x10;
46 / 78
Counter Mode
The above two modes are used to drive the timer from
an external clock source.
By using these options the timer can be used as a
counter to count external events.
47 / 78
Example
Count the number of cars passing on the
road. A sensor is placed across the road, that
sends a pulse when it senses a car, attached
to pin PB1. Configure the timer so that it
responds to the rising edge. Output the
number of cars on PORTA and PORTC.
Solution
For this we configure the timer to count on the
rising edge of an external source. Hence,
CS12=1,CS11=1,CS10=1.
The number of cars that pass through the road will
be equal to the value of TCNT1.
48 / 78
Example
CODE
#include<avr/io.h>
#include<avr/interrupt.h>
int main()
{
DDRA=0xFF;
DDRC=0xFF;
DDRB=0x00;
TCCR1A=0;
TCCR1B=0x07;
while(1)
{
PORTC=TCNT1L;
PORTA=TCNT1H;
}
} AVR_16Bit_Counter
Timer1 16 bit counter.isis
49 / 78
Phase Correct PWM Mode
TCCR1A and TCCR1B(Timer/counter1 Control Registers)
50 / 78
Selecting the Required Mode
51 / 78
Phase Correct PWM Mode
The phase correct PWM mode is based on a dual
slope operation
The counter counts repeatedly from BOTTOM to TOP and
then from TOP to BOTTOM
52 / 78
Phase Correct PWM Mode
In non-inverting Compare Output mode (COM01=1&COM00=0)
The Output Compare (OC1x) is cleared on the compare match
between TCNT1 and OCR1x while upcounting, and set on the
compare match while downcounting.
53 / 78
Phase Correct PWM Mode
In inverting Output Compare mode (COM01=1&COM00=1)
The operation is inverted
54 / 78
Phase Correct PWM Mode
The Timer/Counter Overflow Flag (TOV1) is set each time the
counter reaches BOTTOM
If the interrupt is enabled, the interrupt handler routine can be
used for updating the compare value
The PWM resolution for the phase correct PWM mode can be
fixed to 8-, 9-, or 10-bit or defined by OCR1A/ICR1
The maximum resolution is 16-bit (OCR1A/ICR1 set to MAX)
The PWM resolution in bits can be calculated by using the
following equation
55 / 78
Example
Generate a PWM waveform in the phase correct pwm
mode. Frequency of the waveform should be
approximately 2Khz. Assume controller frequency to be
8Mhz. Use 8-bit resolution.
Calculate the value of prescaler and the value to be loaded in
TCCR1A and TCCR1B.
For the above calculated prescaler, find the output frequency if 9-
bit resolution is used.
Write a C code generating the waveform with Duty cycle of 75% if
PIN 2 of PORTC is set otherwise duty cycle should be 50%.
56 / 78
Registers Associated with Timers
Timer/Counter1 Control Register A – TCCR1A
57 / 78
Solution
Calc value of Prescaler and TCCR1A and TCCR1B
N= PreScaler
We are given fclk-i/o = 8MHz, fpwm = 2KHz.
TOP= 8bit=255
Hence the value of N is found to be =7.84
From the table we choose the nearest value of 8.
Hence, CS12=0, CS11=1 and CS10=0.
TCCR1A=0xA1;
TCCR1B=0x02;
Value of Prescaler =8
while(1)
{
OCR1A=PINA;
OCR1B=PINC;
}
}
AVR_PWM_16bit_2pwms
60 / 78
Phase and Frequency Correct PWM Mode
TCCR1A and TCCR1B(Timer/counter1 Control Registers)
62 / 78
Selecting the Required Mode
64 / 78
Selecting the Required Mode
65 / 78
Fast PWM Mode
The fast Pulse Width Modulation or fast PWM mode
provides a high frequency PWM waveform
generation option
The counter counts from BOTTOM to TOP then
restarts from BOTTOM. It is a single slope operation
66 / 78
Fast PWM Mode
In non-inverting Compare Output mode
(COM01x1=1&COM1x0=0)
The Output Compare (OC1x) is cleared on the compare
match between TCNT1 and OCR1x and set at the TOP.
67 / 78
Fast PWM Mode
In inverting Output Compare mode
(COM01x1=1&COM1x0=1)
The Output Compare (OC1) is set on the compare match
between TCNT1 and OCR1x and cleared at the TOP.
68 / 78
Fast PWM Mode
The Timer/Counter Overflow Flag (TOV1) is set each time the
counter reaches TOP
If the interrupt is enabled, the interrupt handler routine can be
used for updating the compare value
The minimum resolution allowed is 2-bit (OCR1A set to
0x0003), and the maximum resolution is 16-bit (OCR1A set to
MAX)
The PWM resolution in bits can be calculated by using the
following equation
69 / 78
Example
Generate a PWM waveform in the Fast PWM mode.
Frequency of the waveform should be approximately
4Khz. Assume controller frequency to be 8Mhz. Use 8-
bit resolution.
Calculate the value of prescaler and the value to be loaded in
TCCR1A and TCCR1B.
For the above calculated prescaler, find the output frequency if
10-bit resolution is used.
Write a C code generating the waveform with Duty cycle of 75% if
PIN 3 of PORTC is set otherwise duty cycle should be 50%.
70 / 78
Solution
Value of Prescaler
TCCR1A=0xA1;
TCCR1B=0x0A;
Value of Prescaler = 8
10-bit Resolution
Top=10-bit=1023
Focnx=1khz.
71 / 78
Solution
C code for generating the waveform
#include<avr/io.h>
unsigned char x=0;
void main(void)
{ DDRD|=0x30; //configure port D PIN 5 and PIN 6 (OC1A &OC1B) as output
DDRC&=0xfb; //Configures port C PIN 3 as input
TCCR1A=0b10100001; //PWM in phase correct mode 8-BIT
TCCR1B=0b00001010; //Prescalar=8
while(1)
{
x=PINC & 0x04;
if(x==0x04)
{
OCR1A=191;
OCR1B=191;
}
else
{
OCR1A=127;
OCR1B=127;
}
}
} 72 / 78
Waveform Generation
The AVR microcontroller can generate a waveform
using the timer on the I/O pin associated with that
timer.
The microcontroller compares the OCR1x and the TCNT1
register constantly. When they match the OCF1x (compare
match Flag) is set.
The user can program the microcontroller to generate a
waveform in response to this compare match .
The COM1x1 and COM1x0 bits in the TCCR0 register decide
whether to generate a waveform or not and also its form.
73 / 78
Waveform Generation
74 / 78
Waveform Generation
Normal or CTC mode (non-PWM)
75 / 78
Waveform generation Normal Mode
76 / 78
Waveform Generation CTC Mode
77 / 78
Waveform Generation
Fast PWM mode
78 / 78