You are on page 1of 20

Timers

LECTURE# 12
MICROPROCESSOR SYSTEMS AND INTERFACING

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 1


Last Lecture
Signed number in Assembly Language
Overflow in signed numbers
◦ Conditional branching with signed numbers

Logical and Compare instructions


Rotate and Shift Instructions
◦ ROR, ROL, LSL, LSR, ASR instructions

SWAP instruction
BCD and ASCII conversion using assembly
Timers
◦ Timer0 in Normal mode

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 2


Introduction of Timers
Timer is a register gets incremented with every tick of clock
The clock can be internal or external
Timers are used to generate time delays
Oscillator
◦ Using internal or fixed clock. Counter register
◦ Number of ticks define the time passed External Source

Flag
Also called counter Timer/Counter
◦ To count occurrence of an event
◦ Using an external source for timer/counter tick

AVR ATMEGA32/16 and ATmega328/164 has three timers


◦ Timer0/Timer2 is 8-bits wide, while Timer1 is 16-bits wide.

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 3


Timers in AVR ATmega32/16
Timer0
Timer0/Timer2 are 8-bit & Timer1 is 16-bit TOV0 TCNT0 OCR0

Each timer has following registers TCCR0 =


◦ Timer/Counter Register (TCNTn), a counter (comparator)

◦ Overflow rolls over to zero and sets TOVn flag OCF0


◦ Timer/Counter Control Register TCCRn
◦ For Mode of operation (WGM00-01) Timer1 TOV0
OCR1B TCNT1 OCR1B
◦ Selecting clock source (CS00:02)
◦ Force Compare Match (FOC0)
TCCR1A = =
◦ Compare output mode (COM00-01) (comparator) (comparator)
TCCR1B
◦ Output Compare Register (OCRn) OCF1B OCF1B
◦ compare with the contents of TCCTn
◦ When equal sets OCFn flag Timer2
TOV2 TCNT2 OCR2

A TIFR (Timer/Counter Interrupt Flag Register) =


TCCR2
◦ contains TOVn and OCFn for each timer (comparator)

OCF2
OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 4


Programming Timer0
WGM01 WGM00

0
ClkI/O
1 Control Unit
Clk/8
2
Clk/64
Pre 3 MUX
Clk/256
scalar 4
Clk/1024 count up/down clear
5 OCR0
Falling TCNT0
Edge 6
Rising 7 8 8
detector
TOV0 Comparator
CS02 CS00 =
T0 CS01
OCF0

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 5


Programming Timer0
8-bit timer TCNT0
TCCR0(Timer/Counter Control Register)
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00

Also TOV0 and OCF0 flag in TIFR CS02 CS01 CS00 Clock Source
WGM00 WGM01 Mode 0 0 0 No clock source (Timer/counter stopped)
0 0 Normal 0 0 1 clk (no prescaling)
0 1 CTC (Clear Timer on Compare Match) 0 1 0 clk /8
1 0 PWM, phase correct 0 1 1 clk /64
1 1 Fast PWM 1 0 0 clk /256
1 0 1 clk /1024
1 1 0 External clock on T0 pin (falling edge)**
1 1 1 External clock on T1 pin (rising edge)**
*COM00 COM01 (Compare output mode)
**Will behave as counter

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 6


Timer0 Normal Mode
Timer/Counter increments on every tick of clock
◦ Until it reaches the maximum value of 255 (0xFF)
◦ Upon next tick, it rolls over to zero and set TOV0 (timer overflow) flag
Steps TCNT0
◦ Load the TCNT0 with initial value 0xFF
◦ Load TCCR0, to
1. Select clock source and prescalar
0
2. And WGM00:01 = 00 time
3. Poll TOV0 flag, when it is raised
◦ Get out of the loop
4. Stop the timer by loading CS02:CS00 = 000, this may also be skipped
5. Clear TOV0 flag
6. Go back to step 1

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 7


Timer0 Normal Mode (cont.)
Create a square wave of 50% duty cycle on PORTB.5 pin.
Calculate the delay generate by the timer in above program
Analyze the above program to find frequency of the square wave
How to generate a custom delay
◦ Finding values to be loaded into timer
◦ Using hex to decimal to hex converter
◦ Using negative values in assembly

Maximum delay possible by the timer0 with 𝑓𝑜𝑠𝑐 = 10 MHz?


Maximum delay using Prescalar?

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 8


Timer0 Normal Mode Example
.INCLUDE "M32DEF, INC"
Create a square wave ;Stack Initialize
of 50% duty cycle on
LDI R16, 1<<5 ;R16 = 0b0010 0000
PORTB.5. Use Timer0 SBI DDRB, 5 ;PB5 as an output
in Normal mode LDI R17, 0
OUT PORTB, R17 ;clear PORTB
BEGIN: RCALL DELAY ;call timer delay
Calculate the Delay EOR R17, R16 ;toggle D5 of R1
generated by the OUT PORTB, R17 ;toggle PB5
RJMP BEGIN
program? ;-------------------------------Timer0 delay
DELAY: LDI R20, 0xF2 ;R20 = OxF2
Frequency of the wave OUT TCNT0, R20 ;load timer0
LDI R20, 0x01
generated? OUT TCCR0, R20 ;Tim0 Normal mode,int clk
AGAIN: IN R20, TIFR ;read TIFR
Try negative values SBRS R20, TOV0 ;if TOV0=1,skip nxt inst.
RJMP AGAIN
LDI R20, 0x0
OUT TCCR0, R20 ;stop Timer0
LDI R20, (1<<TOV0)
OUT TIFR, R20 ;clear TOV0 flag
RET

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 9


Timers in AVR ATmega328
Timer0/2 – 8-bit
Timer/Counter Register
◦ TCNTn
◦ Overflow rolls over to zero and sets TOVn flag

Timer/Counter Control Register


◦ TCCRnA, TCCRnB
◦ For Mode of operation (WGM00-02)
◦ Selecting clock source (CS00:02)
◦ Force Compare Match (FOC0)
◦ Compare output mode (COM00-01)

Output Compare Register


◦ OCRnA and OCRnB
◦ compare with the contents of TCCTn
◦ When equal sets OCFn flag

Timer/Counter Interrupt Flag Register)


◦ TIFRn
◦ contains TOVn, OCFnA & OCFnB
◦ for each timer

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 10


Timers in AVR ATmega328
Timer0 Registers

COM0x
Description
[1:0]
Normal, OC0x
00
disconnected
Toggle OC0x on
01
compare match
Clear OC0x on
10
compare match
Set OC0x on
11
compare match

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 11


ATmega328
Timer0 Normal Mode Example
LDI R16, 1<<5 ;R16 = 0b0010 0000 LDI R16, 1<<5 ;R16 = 0b0010 0000
SBI DDRB, 5 ;PB5 as an output SBI DDRB, 5 ;PB5 as an output
LDI R17, 0 ATmega32 LDI R17, 0 ATmega328P
OUT PORTB, R17 ;clear PORTB OUT PORTB, R17 ;clear PORTB
BEGIN: RCALL DELAY ;call timer delay BEGIN:RCALL DELAY ;call timer delay
EOR R17, R16 ;toggle D5 of R1 EOR R17, R16 ;toggle D5 of R1
OUT PORTB, R17 ;toggle PB5 OUT PORTB, R17 ;toggle PB5
RJMP BEGIN RJMP BEGIN
;-------------------------------Timer0 delay ;-------------------------------Timer0 delay
DELAY: LDI R20, 0xF2 ;R20 = OxF2 DELAY:LDI R20, 0xF2 ;R20 = 0xF2
OUT TCNT0, R20 ;load timer0 OUT TCNT0, R20 ;load timer0
LDI R20, 0x01 LDI R20, 0x00
OUT TCCR0, R20 ;Tim0 Normal mode,int clk OUT TCCR0A, R20 ;Tim0 Normal mode
AGAIN: IN R20, TIFR ;read TIFR LDI R20, 0x01
SBRS R20, TOV0 ;if TOV0=1,skip nxt inst. OUT TCCR0B, R20 ;Tim0 Normal mode, int clk
RJMP AGAIN AGAIN:IN R20, TIFR0 ;read TIFR
LDI R20, 0x0 SBRS R20, TOV0 ;if TOV0=1,skip nxt inst.
OUT TCCR0, R20 ;stop Timer0 RJMP AGAIN
LDI R20, (1<<TOV0) LDI R20, 0x0
OUT TIFR, R20 ;clear TOV0 flag OUT TCCR0B, R20 ;stop Timer0
RET LDI R20, (1<<TOV0)
OUT TIFR0, R20 ;clear TOV0 flag
RET

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 12


Timer0 CTC mode
CTC (Clear Timer0 on Compare Match)
◦ Increments on clock tick as normal mode
◦ When the TCNT0 value matches OCR0 value
◦ TCNT0 is reset to zero in the next cycle and OCF0 flag is set

Suitable for recurring delay unlike Normal mode


◦ Does not have the re-initialize TCNT0 value
◦ The delay value is directly loaded in to OCR0 register only once without
subtraction or negative sign
TCNT0
0xFF
OCR0

0
time

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 13


Timer0 CTC mode example
.INCLUDE "M32DEF, INC"
Same as previous example LDI R16, 0x08
on Normal mode, except SBI DDRB, 3 ;PB3 as an output
LDI R17, 0
using CTC mode here BEGIN: OUT PORTB, R17 ;PORTB = R17
RCALL DELAY
Calculate the Delay EOR R17, R16 ;toggle D3 of R17
RJMP BEGIN
generated by the program? ; --------------- Timer0 Delay
DELAY: LDI R20, 0
Frequency of the wave OUT TCNT0, R20
LDI R20, 9
generated? OUT OCR0, R20 ;load OCR0
LDI R20, 0x09
Overhead due to OUT TCCR0, R20 ;Timer0, CTC mode,int clk
AGAIN: IN R20, TIFR ;read TIFR
instructions SBRS R20, 0CF0 ;if OCF0 is set skip next inst.
RJMP AGAIN
LDI R20, 0x0
OUT TCCR0, R20 ;stop Timer0
LDI R20, 1<<OCF0
TIFR
OUT TIFR, R20 ;clear OCF0 flag
OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0 RET

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 14


ATmega328
Timer0 CTC mode example
LDI R16, 0x08
Same as previous example SBI DDRB, 3 ;PB3 as an output
on Normal mode, except LDI R17, 0
BEGIN: OUT PORTB, R17 ;PORTB = R17
using CTC mode here RCALLDELAY
EOR R17, R16 ;toggle D3 of R17
Calculate the Delay RJMPBEGIN
;-------------------------------- Timer0 Delay
generated by the program? DELAY: LDI R20, 0
OUT TCNT0, R20
Frequency of the wave LDI R20, 9
OUT OCR0A, R20 ;load OCR0
generated? LDI R20, 0x02
OUT TCCR0A, R20 ;Timer0, CTC mode
Overhead due to LDI R20, 0x01
OUT TCCR0B, R20 ;Timer0, CTC mode,int clk
instructions AGAIN: IN R20, TIFR0 ;read TIFR
SBRS R20, OCF0A ;if OCF0 is set skip next inst.
RJMP AGAIN
LDI R20, 0x0
OUT TCCR0B, R20 ;stop Timer0
TIFR
LDI R20, 1<<OCF0A
OCF0B OCF0A TOV0 OUT TIFR0, R20 ;clear OCF0 flag
HERE: RJMP HERE

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 15


ATmega328
Timer 2 Block Diagram
Timer 2 is another 8-bit timer
◦ similar to Timer 0

Dedicated External oscillator


◦ Can be connected
◦ E.g. 32.768 KHz for RTC

More prescalar options

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 16


ATmega328
Timer 2 Registers
COM2x
[1:0]
a Description

Normal, OC0x
00
disconnected
Toggle OC0x on
01
compare match
Clear OC0x on
10
compare match
Set OC0x on
11
compare match

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 17


ATmega328
Timer 1 – 16 bit timer
16-bit, split into two bytes
◦ TCNT1H and TCNT1L

Two control Registers, wide


number of modes
◦ TCCT1A and TCCR1B

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 18


ATmega328
Timer 1 – 16 bit timer

Following 160bit register are not shown here


TCNT1 – TCNT1H/TCNT1L
OCR1A – OCR1AH/OCR1AL
OCR1B – OCR1BH/OCR1BL
ICR1 – OCR1H/OCR1L

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 19


Counter Programming
Timers can be used to count occurrence of an event
◦ Like clock input source is connected to a door
◦ Find frequency of a wave

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 27

You might also like