0% found this document useful (0 votes)
11 views16 pages

MPS W6-L2L3 Programming AVR Timers II

Uploaded by

hussainpolyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

MPS W6-L2L3 Programming AVR Timers II

Uploaded by

hussainpolyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

EE-222 Microprocessor Systems

Programming AVR Timers II


Week6-Lecture 2/3

Dr. Sara Shakil Qureshi


2

Outline
• Timers
• Timer as a counter
• Timer Modes
▫ Normal mode
▫ CTC mode
Timer as a counter
Timer as Counter
• So far, we have used timers to generate time
delays

• The AVR timer can also be used to count, detect


and measure time of events happening outside of
AVR.
Timer as Counter
• When the timer is used as a timer, the source of the
frequency is the AVR’s internal crystal.

• When the timer is used as a counter, the pulse outside the


AVR increments the TCNT register.
Timer as Counter
• When the timer is used as a timer, the source of the
frequency is the AVR’s internal crystal.

• When the timer is used as a counter, the pulse outside the


AVR increments the TCNT register.
Timer as Counter: Example
• Find the value for TCCR0 if we want to program Timer0 as
a Normal mode counter. Use an external clock for the
clock source and increment on the positive edge.
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0

Clock Selector (CS)

CS02 CS01 CS00 Comment


0 0 0 No clock source (Timer/Counter stopped)
0 0 1 clk (No Prescaling)
0 1 0 clk / 8
0 1 1 clk / 64
1 0 0 clk / 256
1 0 1 clk / 1024
1 1 0 External clock source on T0 pin. Clock on falling edge
1 1 1 External clock source on T0 pin. Clock on rising edge

Sol: TCCR0 = 0000 0111 Normal, external clock source, no


prescalar
Timer as Counter: Example
• Assuming that a 1 Hz clock pulse is fed into pin
T0 (PB0),
▫ Write a program for Counter0 in normal mode
to count the pulses on falling edge and display
the state of the TCNT0 count on PORTC.
Timer as Counter: Example
CTC Mode Programming
Recall: OCR0 Flag
• OCR0 [Output Compare] Flag: [in CTC
Mode]
▫ The content of the OCR0 is compared with
the contents of the TCNT0, when both are
equal:
 OCR0 flag is set AND
 Timer is cleared
TCCR0: Mode Selector
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0

Timer Mode (WGM)

WGM00 WGM01 Comment


0 0 Normal
0 1 CTC (Clear Timer on Compare Match)
1 0 Phase correct PWM
1 1 Fast PWM
OCR0 Example
15

Conclusion
• Timers as a counter

• Timer modes
▫ Normal mode
▫ CTC mode
16

Reading Material
• Textbook:
▫ Chapter 9, Section 9.1 & 9.2
17

Questions?

You might also like