C9 Timers2
C9 Timers2
Outline
References
1. Mazidi, The AVR Microcontroller and Embedded Systems Using Assembly and C,
chapter 10
1
1 Timers and Counters
ä Timers are essential components in microcontrollers that allow the manipulation of time
intervals and the generation of precise timing events.
ä In AVR microcontrollers, timers play a critical role in various applications, including gener-
ating delays, PWM signals, and interfacing with external devices.
ä Many timing applications require the generation of accurate time delay between certain
events or counting of external events outside microprocessor, such as as counting number
of pulses.
ä There are two approaches to generate a time delay: software (loop), and hardware (timers)
ä The subroutine that generates a time delay do nothing for a specified time period. It keeps
the microcontroller busy during delay time. Also, it is difficult to generate precise time
delay.
2 AVR Timers
2
ä Architecture of 16 bit AVR timer
ä Timer components: clock source, counter register, compare register, overflow flag, match
compare flag, output compare pin, and interrupt flag.
3
2.2 Timer 0 Registers
Timer 0 Registers:
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
TIFR0 – – – – – OCF0B OCF0A TOV0
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
ä TCNT0 -
ä Time Interrupt Flag Register (TIFR0) - TIFR contains the flags of different timers.
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIFR0 – – – – – OCF0B OCF0A TOV0
4
TOV0 Timer0 Overflow Flag bit
1 if TCNT0 changes from 0xFF to 0x00
OCF0A Timer0 Output Compare flag bit
1 if TCNT0 = OCR0A
OCF0B Timer0 Output Compare B match flag
1 if TCNT0 = OCR0B
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
ã The clock source can be internal or external (system clock, external clock, external
signal)
ã Prescalar divides the cpu-clock before it reaches the timer.
ã AVR timers have different clock sources and prescaling as shown below:
ã The clock source and prescaling are selected by setting CS2-CS0 in TCCR0B register.
CS2 - CS0 are used to set prescalar.
Example 1 - The following instructions set the prescaler values in the TCCR0B to a clock
division factor of 1024.
LDI R16, (1<<CS02)|(1<<CS00) ; This equivalent to LDI R16, 0x05
OUT TCCR0B, R16 ; Timer clock = system clock/1024
If we do not need to change the content of other bits in TCCR0B, use the following code:
IN R16, TCCR0B ; R16 = TCCR0B
SBR R16, 0x05 ; Set b2 and b0 in R16
OUT TCCR0B, R16 ; Timer clock = system clock/1024
5
ä Operation modes (WGM02 - WGM00) -
6
ä Control OC0X (COM0A1-COM0A0 , COM0B1-COM0B0) -
These bit are used to change the OC0A/OC0B pin in non-PWM mode.
7
Standard I/O registers (00 - 1F)- IN, OUT, SBI, CBI, SBIS, SBIC
PINB, DDRB, PORTB, PINC, DDRC, PORTC, PIND, DDRD, PORTD, TIFR0, TIFR1, TIFR2
TIMSK0, TIMSK1, TIMSK2, TCCR1A, TCCR1B, TCCR1C, TCNT1L, TCNT1H, ICR1L, ICR1H, OCR1AL,
OCR1AH, OCR1BL, OCR1BH, TCCR2A, TCCR2B, TCNT2, OCR2A, OCR2B, ASSR,
8
ä I/O Registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI
and CBI instructions. In these registers, the value of single bits can be checked by using
the SBIS and SBIC instructions.
ä Some of the Status Flags are cleared by writing a logical one to them.
ä The CBI and SBI instructions work with registers 0x00 to 0x1F only.
ä When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must
be used. When addressing I/O Registers as data space using LD and ST instructions, 0x20
must be added to these addresses.
ä For the Extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD
instructions can be used.
3 Operation Modes
ä WGM02:0 = 0
ä The TOV0 = 1 exactly when the TCNT is changed from 0xFF to 0x00.
Example 2 - list the available methods to monitor timer events and respond to them.
1. Polling - the main program can frequently check the status of these flags (TOV0, OCF0A,
OCF0B) to see if one of these events occurred. This requires some program overhead,
which will cost additional processing time. The advantage of this solution is the very short
response time when tight loops are used.
9
2. Interrupt - the AVR can be configured to execute interrupts if an interrupt flag (TOV0,
OCF0A, OCF0B) is set. Normal program execution will be interrupted (almost) immediately
and the processor will execute the code of the ISR. The advantage compared to polling of
interrupt flags is zero overhead in the main loop. This saves processing time.
3. Changing the level of output signal automatically by the hardware - the output
pin (OC0A or OC0B) can be configured to be set, cleared, or toggled automatically on a
compare match based on the content of COM0A1:0. In contrast to the two other solutions
this happens in parallel to normal code execution and requires no processing time.
LDI R16, 32
OUT OCR0A, R16 ; OCR0A = 32
LDI R16, (1<<COM0A0)|(1<<WGM01)|(1<<WGM00)
OUT TCCR0A, R16 ; fast PWM, toggle OC0A when TCNT0 = OCR0A
LDI R16, 1<<CS00
STS TCCR0B, R16 ; prescaler = 1, start timer 0
....
To enable OCOA pin toggling, the PD6 = OC0A has to be set to make it an output pin.
Example 3 - Find the value for TCCR0A and TCCR0B if we want to program Timer0 in Normal
mode, no prescaler.
CLR R20
OUT TCCR0A, R20
LDI R20, 0x01
OUT TCCR0B, R20
We use OUT instruction because the TCCR0A and TCCR0B reside at standard I/O mem-
ory addresses.
Example 4 - Assume the system clock is 1 MHz, and it is divided by a prescaler = 1024, Write
an ALP to blink the LED. What is the blinking frequency.
Reg. D7 D6 D5 D4 D3 D2 D1 D0
10
TIFR0 – – – – – OCF0B OCF0A TOV0
0 0 0 0 0 0 0 ?
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
0 0 0 0 0 0 0 1
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
0 0 0 0 0 0 0 0
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
0 0 0 1 0 1
Example 5 - Use Timer 0 in normal mode to create a simple blinking LED program. Calculate
the delay.
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIFR0 – – – – – OCF0B OCF0A TOV0
0 0 0 0 0 0 0 ?
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
0 0 0 0 0 0 0 1
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
0 0 0 0 0 0 0 0
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
0 0 0 1 0 1
11
.ORG 0x0000 ; location for reset
RJMP MAIN
.ORG 0x0020 ; ISR location for Timer0 overflow
JMP ISRT0
MAIN: LDI R20, HIGH(RAMEND)
OUT SPH, R20
LDI R20, LOW(RAMEND)
OUT SPL, R20 ; set up stack
SBI DDRB, 5 ; PB5 = output
LDI R16, (1 << TOV0)
OUT TIFR0, R16 ; Clear all flags in TIFR0
LDI R20, (1 << TOIE0) ; or use LDI R20, 0x01
STS TIMSK0, R20 ; enable Timer0 overflow interrupt
SEI ; set I (enable interrupts globally)
LDI R20, 0x05
OUT TCCR0B, R20 ; start Timer0, normal mode, prescaler = 1024
HERE: JMP HERE ; keeping CPU busy waiting for interrupt
ä WGM02:0 = 2
ä The OCR0A defines the TOP value for the counter (TOP = OCR0A)
Count range = 0x00 – OCR0A
ä An interrupt can be generated when TCNT0 = OCR0A using the OCF0A flag.
LDI R16, 1<< OCIE0A ; or use LDI R16, 0x02
STS TIMSK0, R16 ; enable OCF0A interrupt, TIMSK0.OCIE0A = 1
ä The CTC mode is used to generate a precise time delay or a digital waveform. It allows
greater control of the output frequency.
12
Example 6 - write an ALP using Timer0 in CTC mode to toggle PB5 every 40 µs.
fs 106
ft = = = 1 µs
P 1
1 1
Tt = = = 1 µs
ft 106
delay = counts * Tt = 40 µs
delay 40 µs
count = = = 40
Tt 1 µs
OCR0A = count - 1 = 39
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
0 0 0 0 0 0 0 0
TIFR0 – – – – – OCF0B OCF0A TOV0
0 0 0 0 0 0 ? 0
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
0 0 0 0 0 0 1 0
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
0 0 0 0 0 1
.ORG 0x0000
SBI DDRB, 5 ; PB5 is output
LDI R20, 0x27
OUT OCR0A, R20 ; OCR0A = 39
LDI R20, 0x02
OUT TCCR0A, R20 ; CTC mode
LDI R20, 0x01
OUT TCCR0B, R20 ; start Timer0, CTC mode, prescaler = 001 = CLK/1
LOOP: SBIS TIFR0, OCF0A ; skip next instruction if OCF0A = 1
RJMP HERE
SBI TIFR0, OCF0A ; Clear OCF0A
13
SBI PINB, 5 ; toggle PB5
RJMP LOOP ; go to HERE to check flag
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
0 0 0 0 0 0 1 0
TIFR0 – – – – – OCF0B OCF0A TOV0
0 0 0 0 0 0 ? 0
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
0 0 0 0 0 0 1 0
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
0 0 0 0 0 1
Example 7 - Write an ALP using Timer0 in CTC mode to toggle PB5 every 100 µs (XTAL = 16
MHz)
14
P=8
fs 16 ∗ 106
ft = = = 2 MHz
P 8
1 1
Tt = = = 0.5 µs
ft 2 ∗ 106
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
0 0 0 0 0 0 0 0
TIFR0 – – – – – OCF0B OCF0A TOV0
0 0 0 0 0 0 0 ?
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
0 0 0 0 0 0 1 0
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
0 0 0 0 0 0 1 0
.ORG 0x0000
SBI DDRB, 5 ; PB5 is output
LDI R20, 0xC7
OUT OCR0A, R20 ; OCR0A = 39
LDI R20, 1 << WGM01 ; or LDI R20, 0x02
OUT TCCR0A, R20 ; WGM = 010, CTC mode
LDI R20, 1 << CS01
OUT TCCR0B, R2O ; TCCR0B = 0x02, start Timer0, prescaler = 010 = CLK/8
HERE: SBIS TIFR0, OCF0A ; skip next instruction if OCF0A = 1
RJMP HERE
SBI TIFR0, OCF0A ; Clear OCF0A
SBI PINB, 5 ; toggle PB5
RJMP HERE
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
0 0 0 0 0 0 1 0
TIFR0 – – – – – OCF0B OCF0A TOV0
0 0 0 0 0 0 ? 0
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
0 0 0 0 0 0 1 0
15
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
0 0 0 0 0 0 1 0
Example 8 - Write a subroutine to generate 16 ms delay. Use Timer0, CTC mode. Assuming
XTAL = 16 MHz.
fs 16 ∗ 106
ft = = = 15625
P 1024
counts counts
Time delay = = 16 ms = counts = 250
ft 15625
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
0 0 0 0 0 0 0 0
TIFR0 – – – – – OCF0B OCF0A TOV0
0 0 0 0 0 0 0 0
16
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
0 0 0 0 0 0 1 0
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
0 0 0 0 0 1 0 1
Example 9 - determine the frequency and duty cycle of output signal generated by the fol-
lowing ALP. Assume the system clock = 16 MHz.
fs 16 ∗ 106
ft = = = 15625 Hz
P 1024
OCR0A + 1 96
Td = = = 6.144 ms
ft 15625
1
fo = = 81.38 Hz
2 ∗ Td
17
Or
ft 15625
fo = = = 81.38 Hz
2 ∗ (1 + OCR0A) 192
ä WGM02:0 = 3, 7
ä The TCNT0 register counts from 0x00 to 0xFF and back down to 0x00 continually (single-
slop operation).
ä Whenever the TCNT0 = OCR0A register, the OC0A is changed according to the values of
COM0A1-COM0A0 bits.
fast-PWM
COM0A1:0 WGM002 OC0A
00 X disconnected
01 0 disconnected
1 toggle
10 X clear on match
set on bottom
11 X set on match
clear on bottom
ä In non-inverting mode, OC0A is cleared when TCNT0 = OCR0A, and set at when TCNT0 =
BOTTOM. In inverting mode, OC0A is set on compare match and cleared at BOTTOM.
non-inverting inverting
OC0A OC0A
TCNT = OCR0A 0 1
TCNT0 = 0x00 1 0
ä The OC0A value will only be visible on the port pin if the pin is defined as output.
SBI DDRD, 6 ; OC0A is multiplexed with PD6 in UNO board
18
ä The frequency of the output signal is calculated by:
fs
fo = P ∈ {1, 8, 64, 256, or 1024}
P ∗ 256
ä The extreme values for the OCR0A register represents special cases:
OCR0A Description
0x00 output will be a narrow spike for each MAX+1 timer clock cycle
0xFF constantly high or low output (set by the COM0A1:0)
ä The toggle option can be used to generate an output signal with fixed frequency and variable
duty cycle. This option is not available for the OC0B pin.
A frequency (with 50% duty cycle) waveform output in fast PWM mode can be achieved by
setting OC0x to toggle mode (COM001:0 = 01).
ä TOV0 = 1 when TCNT0 = TOP. If the interrupt is enabled, the ISR can be used to update
OCR0A.
ä This mode provides higher frequency PWM waveform than phase correct PWM. It is called
the Fast PWM because its maximum frequency is twice that of the Phase Correct PWM mode.
Example 10 - Assuming XTAL = 16 MHz, calculate the frequency of the wave generated by
the following program:
19
OUT TCCR0B, R22 ; start Timer 0, prescaler = 101 = CLK/1024
HERE: RJMP HERE
Reg. D7 D6 D5 D4 D3 D2 D1 D0
TIMSK0 – – – – – OCIE0B OCIE0A TOIE0
0 0 0 0 0 0 0 0
TIFR0 – – – – – OCF0B OCF0A TOV0
0 0 0 0 0 0 0 0
TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
1 0 0 0 0 0 1 1
TCCR0B FOC0A FOC0B – – WGM02 CS02 CS01 CS00
0 0 0 0 0 1 0 1
fs 16000000
fo = = = 61 Hz
P ∗ 256 1024 ∗ 256
Th = 201
Tl = 55
Th 201
duty cycle = = = 0.785
T 256
OCR0A + 1 201
or simply it is = = 0.785
T OP 256
If the OC0A is connected to a LED, nothing is observed due to high frequency output.
Homework - In the previous example, calculate the output frequency in each of the following
cases:
20