Timer/counter: The AVR Microcontroller and Embedded Systems Using Assembly and C
Timer/counter: The AVR Microcontroller and Embedded Systems Using Assembly and C
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A counter register
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A simple design (counting people)
First design
Comparator
=
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A simple design (counting people)
Second design
$FC
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A simple design (making delay)
$FC
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A generic timer/counter
Delay generating
Counting
Wave-form generating
Input Capturing (to calculate the freq of an input
signal)
Oscillator 0
Counter register
COUT
External 1
source
Flag
Counter/Timer
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timers in AVR
1 to 6 timers
3 timers in ATmega32
8-bit and 16-bit timers
two 8-bit timers and one 16-bit timer in ATmega32
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer in AVR
Oscillator
TCNTn (Timer/Counter register) External
Counter register
source
TOVn (Timer Overflow flag) Counter/Timer
Flag
TOVn
= OCFn
Comment:
Comment:
All
Allofofthe
thetimer
timerregisters
registersare
are
byte-addressable
byte-addressableI/O I/Oregisters
registers OCRn
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer 0 (an 8-bit timer)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer 0
TCCR0
TCNT0
TOV0
= OCF0
OCR0
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0
Clock Mode
Timer Selector
(WGM)
(CS)
clk/64
clk/8
clk/256
clk/1024
1 0 1 clk / 1024
1 1 0 External clock source on T0 pin. Clock on falling edge T0
1 1 1 External clock source on T0 pin.
0 Clock on rising edge
CS00 0 1 2 3 4 5 6 7
CS01
CS02
Timer/Counter0 clock
source
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Normal mode
1. Load the TCNT with 256-N:
TCNT0 = 256-N TCNT0
2. Start the timer – by loading a
value into TCCR : TCCR0 = X
0xFF
3. Monitor the TOV flag
TOV TOV TOV
1. Once the TOV becomes high, it time
indicates the timer has finished 0
FF
FE TOV0: 0
1
2
1 TOV0 = 1
0
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1: Write a program that waits 14 machine cycles in
Normal mode to generate a square wave.
$100 8
256-14 =242
-$0E $F2
242,243,244,….,255, 0
$F2
0 0 0 0 1
PSR10
0 0 Normal
clk/64
clk/8
clk/256
clk/1024
0 1 CTC T0
0
1 0 PWM, phase correct
1 1 Fast PWM CS00 0 1 2 3 4 5 6 7
CS01
CS02
Timer/Counter0 clock
source
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Square wave on pin 5 of port B
14 cycles 14 cycles
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1: write a program that waits 14 machine cycles in
Normal mode.
$100 FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0
-$0E OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0 TIFR
$F2
.INCLUDE "M32DEF.INC"
0.5 ms
0.5 ms
Generate a square wave of 1 ms time period. CLK = 10 MHz. Use 8 bit timer.
Td = 1 ms /2 = 0.5 ms
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Write a program using a timer to generate a square wave of 200 kHz.
CLK = 10 MHz, Tclk = 0.1 uS
T = 1/200k = 5 us
Ton = 2.5 us, Toff = 2.5 us
N = 2.5 us/0.1 us = 25
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
In example 1 calculate the delay. Imagine XTAL = 10 MHz.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Finding values to be loaded into the timer
1. Calculate the period of clock source.
Period = 1 / Frequency
E.g. For XTAL = 8 MHz T = 1/8MHz = 125 ns
2. Divide the desired time delay by period of
clock, N = Td/Tclk
3. If N > 255, use a pre-scaled clock
4. Perform 256 - N, where N is the decimal
value we got in Step 2.
5. Set TCNT0 = 256 - N
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 2: Assuming that XTAL = 10 MHz, write a program to generate a
square wave with a period of 10 us (= 100 kHz) on pin PORTB.3.
For a square wave with T = 10 µs we must have a time delay of 5 µs. Because
XTAL = 10 MHz, the counter counts up every 0.1 µs. This means that we need 5
µs / 0.1 µs = 50 clocks. 256 - 50 = 206.
.INCLUDE "M32DEF.INC" DDRB = 1<<3;
PORTB &= ~ (1<<3);
LDI R16,0x08
SBI DDRB,3 ;PB3 as an output while (1)
LDI R17,0
{
OUT PORTB,R17
BEGIN: LDI R20,206 TCNT0 = 206;
OUT TCNT0,R20 ;load timer0
LDI R20,0x01 TCCR0 = 0x01;
OUT TCCR0,R20 ;Timer0,Normal mode,int clk while((TIFR&0x01) == 0);
AGAIN: IN R20,TIFR ;read TIFR
SBRS R20,TOV0 ;if TOV0 is set skip next TCCR0 = 0;
RJMP AGAIN TIFR = 1<<TOV0;
LDI R20,0x0
OUT TCCR0,R20 ;stop Timer0 PORTB = PORTB ^ (1<<3);
LDI R20,0x01 }
OUT TIFR,R20 ;clear TOV0 flag
EOR R17,R16 ;toggle D3 of R17
OUT ;toggle PB3
RJMP BEPORTB,R17GIN
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 3: Modify TCNT0 in Example 2 to get the largest time delay possible with no prescaler.
Find the delay in µs. In your calculation, do not include the overhead due to instructions.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
For an 8-bit timer, what is the maximum delay that can be generated for
CLK = 10MHz?
Tclk = 0.1 us
0.1 x 1024 = 102.4 us for one clock cycle using a pre-scaled clock
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Generating Large Delays
Using loop
Pre-scaled clock
Bigger counters
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Prescaler and generating a large time delay
PSR10
Clear
clkIO 10-bit T/C Prescaler
clk/8
clk/64
clk/256
clk/1024
T0
0
CS00 0 1 2 3 4 5 6 7
CS01
CS02
Timer/Counter0 clock
source
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
CTC (Clear Timer on Compare match) mode
TCNT0
0xFF
OCR0
OCR0 = 99 0
xx TOV0:
2 0
1
OCF0:
1 TOV0 = no change
0
OCF0 = 1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer in Normal Mode Timer in CTC Mode
1.Load the TCNT with 256-N: 1.Load the OCR with N-1: OCR0=N-
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Rewrite
Example example
4: Rewrite 2 using
example CTC
2 using CTC
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0
TCCR0 TCCR2
TCNT0 TCNT2
TOV0 TOV2
= OCF0 = OCF2
OCR0 OCR2
= OCF1B
TOV1 = OCF1A
OCR1AH OCR1AL
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
COM1A1 COM1A0 COM1B1 COM1B0 FOC1A FOC1B WGM11 WGM10 TCCR1A
clk/8
clk/64
clk/256
clk/1024
0 1 1 clk / 64
1 0 0 clk / 256 T1
1 0 1 clk / 1024 0
1 1 0 External clock source on T0 pin. Clock on falling edge
1 1 1 External clock source 0on 1T0 pin. Clock on5 rising edge
CS10 2 3 4 6 7
CS11
CS12
Timer/Counter1 clock
source
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Assuming XTAL = 10 MHz write a program that toggles
PB5 once per millisecond, using Normal mode.
XTALLDI = 10 MHz 1/10
.INCLUDE "M32DEF.INC"
R16,HIGH(RAMEND) MHzpointer
;init stack = 0.1 µs
Num.LDI of
OUT
machine cycles = 1 ms / 0.1 µs = 10,000
SPH,R16
R16,LOW(RAMEND)
TCNT1
OUT
SBI = 65,536
SPL,R16
DDRB,5 – an10,000
;PB5 as output = 55,536 = $D8F0
BEGIN:SBI PORTB,5 ;PB5 = 1
RCALL DELAY_1ms
CBI PORTB,5 ;PB5 = 0
RCALL DELAY_1ms TCNT1H TCNT1L
RJMP BEGIN
DELAY_1ms:
LDI
LDI R20,0xD8
R20,HIGH(-10000) ; load -N
OUT
OUT TCNT1H,R20
TCNT1H,R20 ;TEMP = 0xD8
LDI
LDI R20,0xF0
R20,LOW(-10000)
OUT
OUT TCNT1L,R20
TCNT1L,R20 ;TCNT1L
;Timer1 =overflows
0xF0, TCNT1H
after= 10000
TEMP machine cycles
LDI R20,0x00
OUT TCCR1A,R20 ;WGM11:10=00
LDI R20,0x01
OUT TCCR1B,R20 ;WGM13:12=00,CS=CLK
AGAIN:IN R20,TIFR ;read TIFR
SBRS R20,TOV1 ;if OCF1A is set skip next instruction
RJMP AGAIN
LDI R20,1<<TOV1
OUT TIFR,R20 ;clear TOV1 flag
LDI R19,0
OUT TCCR1B,R19 ;stop timer
OUT TCCR1A,R19 ;
RET
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
LDI R16,LOW(-10000)
OUT TCNT1L,R16
LDI R16,HIGH(-10000)
OUT TCNT1H,R16
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
TCNT0 = 256 –N
TCNT0 = -N
TCNT1L = LOW(-N)
TCNT1H = HIGH(-N)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
TEMP register
Data bus (8-bit)
TEMP (8-bit)
LDI R20,0xF3
IN R20,TCNT1L
a = TCNT1L;
TCNT1H = 0xF3;
OUT TCNT1H,R20
IN R21,TCNT1H
b = TCNT1H;
TCNT1L = 0x53;
LDI R20,0x53
OUT TCNT1L,R20
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Assuming XTAL = 10 MHz write a program that toggles
PB5 once per millisecond, using CTC mode.
.INCLUDE "M32DEF.INC"
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
SBI DDRB,5 ;PB5 as an output
BEGIN:SBI PORTB,5 ;PB5 = 1
RCALL DELAY_1ms
CBI PORTB,5 ;PB5 = 0
RCALL DELAY_1ms
RJMP BEGIN
DELAY_1ms:
LDI R20,0x00
OUT TCNT1H,R20 ;TEMP = 0
OUT TCNT1L,R20 ;TCNT1L = 0, TCNT1H = TEMP
LDI R20,0x00
OUT TCCR1A,R20 ;WGM11:10=00
LDI R20,0x09
OUT TCCR1B,R20 ;WGM13:12=01,CS=CLK
AGAIN:
IN R20,TIFR ;read TIFR
SBRS R20,OCF1A ;if OCF1A is set skip next instruction
RJMP AGAIN
LDI R20,1<<OCF1A
OUT TIFR,R20 ;clear OCF1A flag
LDI R19,0
OUT TCCR1B,R19 ;stop timer
OUT TCCR1A,R19 ;
RET
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Counting
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Counting
PSR10
Clear
clkIO 10-bit T/C Prescaler
clk/8
clk/64
clk/1024
clk/256
T0
0
CS00 0 1 2 3 4 5 6 7
CS01 T0
CS02
Timer/Counter0 clock
source
7
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example Assuming that clock pulses are fed into pin T0, write a program for counter 0
in normal mode to count the pulses on falling edge and display the state of the TCNT0
count on PORTC.
.INCLUDE "M32DEF.INC"
CBI DDRB,0 ;make T0 (PB0) input PSR10
clk/64
clk/256
clk/8
clk/1024
LDI R20,0x06
T0
OUT TCCR0,R20 ;counter, falling edge
0
AGAIN:
IN R20,TCNT0
CS00 0 1 2 3 4 5 6 7
OUT PORTC,R20 ;PORTC = TCNT0 CS01
IN R16,TIFR CS02
SBRS R16,TOV0
RJMP AGAIN ;keep doing it Timer/Counter0 clock
source
LDI R16,1<<TOV0
OUT TIFR, R16
RJMP AGAIN ;keep doing it
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Assuming that clock pulses are fed into pin T1. Write a program for counter 1 in CTC
mode to make PORTC.0 high every 100 pulses.
.INCLUDE "M32DEF.INC"
LDI R20,0x0
OUT TCCR1A,R20
LDI R20,0x0E ; WGM = 0100 for CTC mode, clock = 110
OUT TCCR1B,R20 ;CTC, counter, falling edge
AGAIN:
LDI R20,0
OUT OCR1AH,R20 ;TEMP = 0
LDI R20,99
OUT OCR1AL,R20 ;ORC1L = R20, OCR1H = TEMP
L1: IN R20,TIFR
SBRS R20,OCF1A
RJMP L1 ;keep doing it
LDI R20,1<<OCF1A ;clear OCF1A flag
OUT TIFR, R20
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights