9 TimerCounter v2
9 TimerCounter v2
Sepehr Naimi
www.NicerLand.com
A counter register
2
A simple design (counting people)
First design
Comparator
=
3
A simple design (counting people)
Second design
$FC
4
A simple design (making delay)
$FC
5
A generic timer/counter
Delay generating
Counting
Wave-form generating
Capturing
Oscillator 0
Counter register
COUT
External 1
source
Flag
Counter/Timer
6
Timers in AVR
1 to 6 timers
3 timers in ATmega328
8-bit and 16-bit timers
two 8-bit timers and one 16-bit timer in
ATmega328
7
Timer in AVR
Oscillator
TCNTn (Timer/Counter register) External
Counter register
source
TOVn (Timer Overflow flag) Counter/Timer
Flag
TCCRn (Timer Counter control register)
OCRn (output compare register) TCCRn
OCFn (output compare match flag)
TCNTn OCRn
TOVn
=
Comment:
Comment:
All
Allofofthe
thetimer
timerregisters
registersare
are
byte-addressable OCFn
byte-addressableregisters
registers
8
Timer 0 (an 8-bit timer)
Timer 0
TCCR0A TCCR0B
TOV0
= =
OCF0A OCF0B
10
COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00 TCCR0A
PSR10
Timer Clock
Mode Selector
(WGM) (CS)
Clear
clkIO 10-bit T/C Prescaler
CS02
WGM02CS01 Comment
CS00 WGM00
WGM01 Comment
clk/8
clk/64
clk/256
clk/1024
00 0 0 0 No
0 clock Normal
source (Timer/Counter stopped)
00 0 0 1 clk
1 (No Prescaling)
Phase correct PWM T0
00 1 1 0 clk
0 / 8 CTC (Clear Timer on Compare Match)
0
00 1 1 1 clk
1 / 64 Fast PWM
11 0 0 0 clk
0 / 256 Reserved
11 0 0 1 clk
1 / 1024Phase CS00
correct PWM0 1 2 3 4 5 6 7
11 1 1 0 External
0 clock CS01 on T0 pin. Clock on falling edge
source
Reserved
CS02
11 1 1 1 External
1 clock source
Fast PWM on T0 pin. Clock on rising edge
Timer/Counter0 clock
source
11
Normal mode
TCNT0
0xFF
TOV TOV TOV
0 time
FF
FE TOV0: 0
1
2
1
0 TOV0 = 1
12
Example 1: Write a program that waits 14
machine cycles in Normal mode.
14 = $0E
UP Counter Register Load
$100 COUT
-$0E 8
$F2
$F2
COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00 TCCR0A
0 0 TCCR0A
TCCR0A==0x00;
0x00;
TCCR0B TCCR0B
TCCR0B==0x03;
FOC0A FOC0B - - WGM02 CS02 CS01 CS00
0x03;
0 0 0 1
Timer Mode (WGM) PSR10
0 0 0 Normal
clk/8
clk/64
clk/256
clk/1024
0 0 1 Phase correct PWM T0
0 1 0 CTC 0
1 0 0 Reserved CS01
CS02
1 0 1 Phase correct PWM
1 1 0 Reserved Timer/Counter0 clock
source
1 1 1 Fast PWM
13
Example 1: Write a program that waits 14
machine cycles in Normal mode.
$100
- - - - - OCF0B OCF0A TOV0 TIFR0
-$0E
$F2
LDI R16,(1<<5) ;R16=0x20
DDRB = 1<<5;
SBI DDRB,5 ;PB5 as an output
LDI R17,0 PORTB &= ~(1<<5); //PB5=0
OUT PORTB,R17
while (1)
BEGIN: LDI R20,0xF2
OUT Question:
Question:How
TCNT0,R20 to
;load
How tocalculate
calculatethe
timer0 the{delay
delaygenerated
generated
LDI R20,0x0
OUT
by
bythe
thetimer?
TCCR0A,R20 timer? TCNT0 = 0xF2;
LDI R20,0x01 TCCR0A = 0x00;
OUT Answer:
TCCR0B,R20
Answer:
;Normal mode, inter. clk
TCCR0B = 0x01;
AGAIN: SBIS TIFR0,TOV0 ;if TOV0 is set skip next
RJMP 1)
1)Calculate
AGAIN Calculatehowhowmuchmuchaamachine
machine clock
clocklasts.
lasts.
while((TIFR0&(1<<TOV0))==0)
LDI T=
T=1/f
R20,0x0
1/f {}
OUT TCCR0B,R20 ;stop Timer0
LDI 2)
2)Calculate
Calculatehow
R20,(1<<TOV0) howmany
;R20 0x01machine
=many machineclocks
clocksit=itwaits.
waits.
TCCR0B 0;
OUT 3)
3)Delay
Delay==TT*;clear
TIFR0,R20 *number of
TOV0
number machine
offlag
machine cycles
cycles
TIFR0 = (1<<TOV0);
EOR R17,R16 ;toggle D5 of R17 PORTB ^= (1<<5);
OUT PORTB,R17 ;toggle PB5
RJMP BEGIN }
14
In example 1 calculate the delay.
XTAL = 10 MHz.
Solution 1 (inaccurate):
LDI R16,0x20
1) Calculating T: SBI DDRB,5 ;PB5 as an output
LDI R17,0
T = 1/f = 1/10M = 0.1µs OUT PORTB,R17
BEGIN: LDI R20,0xF2
2) Calculating num of OUT TCNT0,R20 ;load timer0
LDI R20,0x0
machine cycles: OUT TCCR0A,R20
LDI R20,0x01
$100 OUT TCCR0B,R20 ;Normal mode, inter. clk
AGAIN: SBIS TIFR0,TOV0 ;if TOV0 is set skip next
-$F2
RJMP AGAIN
$0E = 14 LDI R20,0x0
OUT TCCR0B,R20 ;stop Timer0
3) Calculating delay LDI R20,(1<<TOV0) ;R20 = 0x01
OUT TIFR0,R20 ;clear TOV0 flag
14 * 0.1µs = 1.4 0µs
EOR R17,R16 ;toggle D5 of R17
OUT PORTB,R17 ;toggle PB5
RJMP BEGIN
15
Accurate calculating
Other than timer, executing the instructions consumes time; so if we
want to calculate the accurate delay a program causes we should add
the delay caused by instructions to the delay caused by the timer
LDI R16,0x20
SBI DDRB,5
LDI R17,0
OUT PORTB,R17
BEGIN: LDI R20,0xF2 1
OUT TCNT0,R20 1
LDI R20,0x00 1
OUT TCCR0A,R20 1
LDI R20,0x01 1
OUT TCCR0B,R20 1
AGAIN: SBIS TIFR0,TOV0 1/2
RJMP AGAIN 2
LDI R20,0x0 1
OUT TCCR0B,R20 1
LDI R20,0x01 1
OUT TIFR0,R20 1
EOR R17,R16 1
OUT PORTB,R17 1
RJMP BEGIN 2
18
Delay caused by timer = 14 * 0.1µs = 1.4 µs Delay caused by instructions = 18 * 0.1µs = 1.8
Total delay = 3.2 µs wave period = 2*3.2 µs = 6.4 µs wave frequency = 156.25 KHz
16
Finding values to be loaded into the
timer
1. Calculate the period of clock source.
Period = 1 / Frequency
E.g. For XTAL = 16 MHz T = 1/16MHz
2. Divide the desired time delay by
period of clock.
3. Perform 256 - n, where n is the
decimal value we got in Step 2.
4. Set TCNT0 = 256 - n
17
Example 2: Assuming that XTAL = 10 MHz, write a program to
generate a square wave with a period of 10 ms 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.
LDI R16,(1<<5) ;R16=0x20 DDRB = 1<<3;
SBI DDRB,5 ;PB5 as an output
LDI R17,0 PORTB &= ~ (1<<3);
OUT PORTB,R17 while (1)
BEGIN: LDI R20,206
OUT TCNT0,R20 ;load timer0 {
TCNT0 = 206;
LDI R20,0x0
OUT TCCR0A,R20 TCCR0A = 0x00;
LDI R20,0x01 TCCR0B = 0x01;
OUT TCCR0B,R20 ;Normal mode, int. clk
AGAIN: SBIS TIFR0,TOV0 ;if TOV0 set skip next while((TIFR0&0x01) == 0)
RJMP AGAIN {}
LDI R20,0x0
OUT TCCR0B,R20 ;stop Timer0 TCCR0B = 0;
LDI R20,(1<<TOV0) ;R20 = 0x01 TIFR0 = 1<<TOV0;
OUT TIFR0,R20 ;clear TOV0 flag
PORTB = PORTB ^ (1<<3);
EOR R17,R16 ;toggle D5 of R17 }
OUT PORTB,R17 ;toggle PB5
RJMP BEGIN
18
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.
To get the largest delay we make TCNT0 zero. This will count
up from 00 to 0xFF and then roll over to zero.
LDI R16,(1<<5) ;R16=0x20
SBI DDRB,5 ;PB5 as an output
DDRB = 1 << 3;
LDI R17,0
OUT PORTB,R17 PORTB &= ~(1<<3);
BEGIN: LDI R20,0
OUT TCNT0,R20 ;load timer0 while (1)
LDI R20,0x0 {
OUT TCCR0A,R20
LDI R20,0x01 TCNT0 = 0x00;
OUT TCCR0B,R20 ;Normal mode, int. clk TCCR0A = 0x00;
AGAIN: SBIS TIFR0,TOV0 ;if TOV0 set skip next
RJMP AGAIN TCCR0B = 0x01;
LDI R20,0x0 Solution
OUT TCCR0B,R20 ;stop Timer0 while((TIFR0&(1<<TOV0))==0)
LDI 1)
R20,(1<<TOV0)Calculating T:
;R20 = 0x01 {}
OUT TIFR0,R20 ;clear TOV0 flag
T = 1/f = 1/10MHz = 0.1µs TCCR0B = 0;
EOR R17,R16 ;toggle D5 of R17 TIFR0 = 0x01;
OUT 2) Calculating
PORTB,R17 ;toggle PB5 delay
RJMP BEGIN PORTB = PORTB^(1<<3);
256 * 0.1µs = 25.6µs
}
19
Generating Large Delays
Using loop
Prescaler
Bigger counters
20
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
21
CTC (Clear Timer on Compare match) mode
TCNT0
0xFF
OCR0
OCR0 0
xx TOV0:
2 0
1
OCF0:
1
TOV0 = no change
0
OCF0 = 1
22
CTC mode (Cont.)
COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00 TCCR0A
1 0
FOC0A FOC0B - - WGM02 CS02 CS01 CS00 TCCR0B
0
Timer Mode (WGM)
WGM02 WGM01 WGM00 Comment
0 0 0 Normal
0 0 1 Phase correct PWM
0 1 0 CTC
0 1 1 Fast PWM
1 0 0 Reserved
1 0 1 Phase correct PWM
1 1 0 Reserved
1 1 1 Fast PWM
23
Rewrite example 2 using CTC
(Assuming XTAL = 10MHz, write a program to generate a square wave with a period of
Example 4: Rewrite example 2 using CTC
10 ms.)
TOV0 TOV2
= = = =
25
The difference between Timer0 and
Timer2
Timer0 Timer2
26
Timer 1
TCCR1A TCCR1B TCCR1C
TOV1
= =
OCF1A OCF1B
27
COM1A1 COM1A0 COM1B1 COM1B0 - - WGM11 WGM10 TCCR1A
clk/8
clk/64
clk/256
clk/1024
1 0 1 clk / 1024 T1
1 1 0 External clock source on T0 pin. Clock on falling edge
1 1 1 External clock source on0T0 pin. Clock on rising edge
CS10 0 1 2 3 4 5 6 7
CS11
CS12
Timer/Counter1 clock
source
28
toggles PB5 once per millisecond, using Normal
mode.
XTALOUT = 10
LDI
MHz 1/10
R16,HIGH(RAMEND)
SPH,R16 MHz = 0.1 µs
;init stack pointer
Num.LDI
OUT of machine cycles = 1 ms / 0.1 µs = 10,000
R16,LOW(RAMEND)
SPL,R16
TCNT1
SBI
BEGIN:SBI = 65,536
DDRB,5
PORTB,5 – 10,000
;PB5 = 1 = 55,536 = $D8F0
;PB5 as an output
RCALL DELAY_1ms
CBI PORTB,5 ;PB5 = 0
RCALL DELAY_1ms
RJMP BEGIN TCNT1H TCNT1L
DELAY_1ms:
LDI R20,HIGH(-10000)
R20,0xD8
STS TCNT1H,R20
TCNT1H,R20 ;TEMP = 0xD8
LDI R20,R20,0xF0
,LOW(-10000)
STS TCNT1L,R20
TCNT1L,R20 ;Timer1 overflows
;TCNT1L after
= 0xF0, 10000
TCNT1H machine cycles
= TEMP
LDI R20,0x0
STS TCCR1A,R20 ;WGM11:10=00
LDI R20,0x1
STS TCCR1B,R20 ;WGM13:12=00,CS=CLK
AGAIN:SBIS TIFR1,TOV1 ;if TOV1 is set skip next instruction
RJMP AGAIN
LDI R20,1<<TOV1
OUT TIFR1,R20 ;clear TOV1 flag
LDI R19,0
STS TCCR1B,R19 ;stop timer
STS TCCR1A,R19 ;
RET
29
TEMP register
Data bus (8-bit)
TEMP (8-bit)
LDI
LDS R20,0xF3
R20,TCNT1L
TCNT1H
a = TCNT1L;
= 0xF3;
STS
LDS TCNT1H,R20
R21,TCNT1H
TCNT1L
b = TCNT1H;
= 0x53;
LDI R20,0x53
STS TCNT1L,R20
30
toggles PB5 once per millisecond, using CTC
mode.
LDI R16,HIGH(RAMEND)
#include <avr/io.h>
OUT SPH,R16
LDI R16,LOW(RAMEND) void delay1ms();
OUT SPL,R16
int main(){
SBI DDRB,5 ;PB5 as an output
BEGIN:SBI PORTB,5 ;PB5 = 1 DDRB |= 1<<5;
RCALL DELAY_1ms
CBI PORTB,5 ;PB5 = 0 while (1) {
RCALL DELAY_1ms
RJMP BEGIN
delay1ms();
PORTB ^= (1<<5); //toggle PB5
DELAY_1ms:
LDI R20,0x00 }
STS TCNT1H,R20 ;TEMP = 0
STS TCNT1L,R20 ;TCNT1L = 0, TCNT1H = TEMP }
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 28 pin
(PCINT14/RESET) PC6 1 28 PC5 (ADC5/SCL/PCINT13)
(PCINT16/RXD) PD0 2 27 PC4 (ADC4/SDA/PCINT12)
Timer/Counter0 clock
(PCINT17/TXD) PD1 3 26 PC3 (ADC3/PCINT11)
source
(PCINT18/INT0) PD2 4 MEGA328 25 PC2 (ADC2/PCINT10)
(PCINT19/OC2B/INT1) PD3 5 24 PC1 (ADC1/PCINT9)
T0 PD4
(PCINT20/XCK/T0) 6 23 PC0 (ADC0/PCINT8)
VCC 7 22 GND
GND 8 21 AREF
(PCINT6/XTAL1/TOSC1) PB6 9 20 AVCC
6 (PCINT7/XTAL2/TOSC2) PB7 10 19 PB5 (SCK/PCINT5)
(PCINT21/OC0B) PD5 11 18 PB4 (MISO/PCINT4)
(PCINT22/OC0A/AIN0) PD6 12 17 PB3 (MOSI/OC2A/PCINT3)
(PCINT23/AIN1) PD7 13 16 PB2 (SS/OC1B/PCINT2)
7 (PCINT0/CLKO/ICP1) PB0 14 15 PB1 (OC1A/PCINT1)
33
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 <avr/io.h>
CBI DDRD,4 ;make T0 (PD4) input int main()
LDI R20,0xFF
{
OUT DDRC,R20 ;make PORTC output
LDI R20,0x00 DDRC = 0xFF;
OUT TCCR0A,R20 TCCR0A = 0; //WGM=0000 (Normal)
LDI R20,0x06
TCCR0B = 0x06; //CS=6 (Count on
OUT TCCR0B,R20 ;counter, falling fall)
edge
AGAIN: while (1)
IN R20,TCNT0 {
OUT PORTC,R20 ;PORTC = TCNT0
PORTC = TCNT0; //read TCNT0
RJMP AGAIN ;keep doing it
}
PSR10
}
Clear
clkIO 10-bit T/C Prescaler
clk/256
clk/1024
clk/8
clk/64
CS00 0 1 2 3 4 5 6 7
CS01
CS02
Timer/Counter0 clock
source
34
Assuming that clock pulses are fed into pin T1. Write a program for Counter1
in CTC mode to make PORTC.0 high every 100 pulses.
CBI DDRD,5 ;make T1 (PD5) input #include <avr/io.h>
SBI DDRC,0 ;PC0 as an output
int main()
LDI R20,0x00 {
OUT TCCR1A,R20 DDRD &= ~(1<<5);
LDI R20,0x0E ;CS=6 (falling edge
counter) DDRC |= 1<<0;
OUT TCCR1B,R20 ;CTC TCCR1A = 0; //WGM=0100 (CTC)
AGAIN:
LDI R20,0 TCCR1B = 0x0E; //CS=6 (Count on
fall)
OUT OCR1AH,R20 ;TEMP = 0
LDI R20,99 OCR1A = 99;
OUT OCR1AL,R20 ;ORC1L = R20, OCR1H = while (1)
TEMP
L1: {
SBRS TIFR1,OCF1A while((TIFR1&(1<<OCF1A)) == 0);
RJMP L1 ;keep doing it
LDI R20,1<<OCF1A ;clear OCF1A flag TIFR1 = (1<<OCF1A);
OUT TIFR1, R20 PORTC |= (1<<0); //PC0 = 1
PORTC &= ~(1<<0); //PC0 = 0
SBI PORTC,0 ;PC0 = 1
CBI PORTC,0 ;PC0 = 0 }
RJMP AGAIN ;keep doing it }
35