0% found this document useful (0 votes)
2 views

MPS W6-L1 Programming AVR Timers I (1)

Uploaded by

hussainpolyu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MPS W6-L1 Programming AVR Timers I (1)

Uploaded by

hussainpolyu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

EE-222 Microprocessor Systems

Programming AVR Timers I


Week6-Lecture 1

Dr. Sara Shakil Qureshi


2

Outline
• Timers
▫ Registers
▫ Timer flags
▫ Timer programming
AVR Timers
Timers
Timers: Why do we need
them?
• Provide accurately timed delays or actions
independent of code execution time
• How are Timers used?
▫ Accurate delay
 Read the timer, store value as K. Loop until timer
reaches K+100.
▫ Schedule important events
 Setup an Output Compare to trigger an interrupt at a
precise time
▫ Measure time between events
 When event#1 happens, store timer value as K
 When event#2 happens, read timer value and subtract K
 The difference is the time elapsed between the two
6

AVR Timers
• Generally, we use a timer/counter to generate time
delays, waveforms, or to count events.
▫ In AVR ATmega16 / ATmega32, there are three
timers:
 Timer0: 8-bit timer
 Timer1: 16-bit timer
 Timer2: 8-bit timer
7

AVR Timers
• We can load a count value in TCNT0 and start the
timer from a specific count.
• Another interesting feature is that a value can be
set in the Output Compare Register (OCR0), and
▫ whenever TCNT0 reaches that value, the Output
Compare Flag (OCF0) flag is Set.
8

Timer Registers Located in I/O


memory
• TCNTn (Timer Counter Register n)
Use IN and OUT

• TCCRn (Timer Counter Control Register n)

• OCRn (Output Compare Register n)


▫ Sets OCF if OCR(value) ==TCNT(value)
• TIFR (Timer/Counter Interrupt Flag Register)
9

Timer Registers Located in I/O memory


Use IN and O
T0 Programming
11

Timer0 Programming
12

TCCR0

• Clock frequency • Timer mode

External clock source is used.


13

TIFR0
14

TOV0 – Normal Mode


• Sets when a time overflows
15

OCR0 – Normal Mode


• Sets when OCR0 is equal to TCNT0
16

Timer 0 – Hardware
Organization
17

Example
• Find the value of TCCR0 if we want to program
Timer0 in:
▫ Normal Mode
▫ No prescaler
▫ Use AVR’s Crystal Oscillator for the clock source

• Sol:
Normal Mode
FF TCNT0

FE
0xFF

2 TOV TOV TOV


0 time
1
0 TOV0 =
1

TOV0: 0
1
19

Steps to Program Timer0 in


Normal Mode
1. Load the TCNT0 with the initial count value.
2. Configure timer/counter mode through TCCR0
register.
3. Keep monitoring the timer overflow flag (TOV0):
▫ Get out of the loop when TOV0 becomes high
4. Stop the timer by disconnecting the clock source:
▫ LDI R20, 0x00
▫ TCCR0,R20
5. Clear the TOV0 flag for the next round.
6. Go back to Step 1 to load TCNT0 again.
20

Timer 0 Demo
1. Load the TCNT0 LDI R20, 0xF2
OUT TCNT0, R20
2. Configure TCCR0 register
3. Monitor TOV0 LDI R20, 0x01
OUT TCCR0, R20
4. Stop the timer
5. Clear the TOV0 AGAIN: IN R20,TIFR
SBRS R20,TOV0
RJMP AGAIN

LDI R20,0x0
OUT TCCR0,R20

LDI R20,0x01
OUT TIFR, R20
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
machine cycles: LDI R20,0x0
OUT TCCR0A,R20
$100 LDI R20,0x01
OUT TCCR0B,R20 ;Normal mode, inter. clk
-$F2 AGAIN: SBIS TIFR0,TOV0 ;if TOV0 is set skip next
RJMP AGAIN
$0E = 14 LDI R20,0x0
OUT TCCR0B,R20 ;stop Timer0
3) Calculating delay
LDI R20,(1<<TOV0) ;R20 = 0x01
14 * 0.1µs = 1.4 0µs OUT TIFR0,R20 ;clear TOV0 flag

EOR R17,R16 ;toggle D5 of R17


OUT PORTB,R17 ;toggle PB5
RJMP BEGIN
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
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
Example
• Assuming XTAL = 8 Mhz, write a program to
generate a square wave with a period of 12.5 us
on PIN PORTB.3.
Solution
Prescalar and Generating a Large
Time Delay
• Time delay depends on:
▫ Crystal Frequency
▫ Timer’s 8-bit register

• Both are fixed

• How to generate large time delay?


▫ Use prescalar to increase the delay by
reducing the clock time period
Prescalar and Generating a Large
Time Delay
Example
Example
30

Conclusion
• Timers as delay
▫ Registers
▫ Timer flags
31

Reading Material
• Textbook:
▫ Chapter 9, Section 9.1
32

Questions?

You might also like