0% found this document useful (0 votes)
32 views22 pages

Lecture - 10 (Part-I) - Timer - Counter

Uploaded by

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

Lecture - 10 (Part-I) - Timer - Counter

Uploaded by

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

EEE/ECE 365

Microprocessor

Lecture #10 (Part-I)


Timers and Counters
Md Rakibul Hasan
Lecturer, Dept. of EEE, BRAC University

Slide Courtesy of Imtiaz Ahmed, Lecturer, Dept. of EEE, BRAC University


Timer/Counter: Basic Concept
• There are counter registers in microcontrollers for counting
events or measuring time.
• When we want to count an event, we connect the external
event source to the clock pin of the counter register.
• Then when an event occurs externally, the content of the
counter increments; in this way the content of the counter
represents how many times an event has occurred.

2
Timer/Counter: Basic Concept
• When we want to measure time, we connect the oscillator to
the clock pin of the counter
• So, when the oscillator ticks, the content of the counter
increases and
• Since we know the time period of the clock, therefore, from
the content of the counter we can measure how much time
has been elapsed.

3
Block Diagram Representation
Clock of the
Counter
Oscillator Register
Counter Register

External
Source Registers involved
Counter/ TCNTx
Timer Flag
OCRx
TIMSK
TIFR
4
NORMAL MODE : Value Increases in TCNT0 Register at Every Pulse
Value increases and Resets When it Overflows
in TCNT0 Register TOV0 flag is
set
Maxm value
255
Your Set
Value in
OCR0

Pulses
OCF0 flag is 255
Both Flags
set
set at 3
immediate WGM00:WGM01 = 00 2
next clock COM01:COM00 = 00 1
0 0 5
To measure time
• One of the ways to generate a time delay is to first clear the
counter register and wait until the counter reaches a certain
number.
• For example, the oscillator frequency of a microcontroller is
1 MHz. It means that every clock is of 1 uSec width.
• In other words, the content of the counter register
increments at every 1 uSec.
• If we want to measure 100 uSec then we have to clear the
counter register and make the timer ON and wait until
counting of 100 clocks is completed.
6
Another method of time measurement
• In microcontrollers, there is a flag for each of the counters.
• The flag is set when the register overflows and it is cleared by software.
• The second method of generating time delay is to counter register with
an appropriate value and wait until the counter register overflows and
the flag is set.
• For example, if we want to make 3uS delay with a 1 MHz microcontroller
we have to load the register with 253.
• With first tick, it will become 254, with the second tick, 255 and with the
third tick, the register will overflow and the flag will be set.
7
Timers in ATmega32
• Different microcontrollers have different numbers of timers.
• In ATmega32, there are three timers, namely Timer0, Timer1
and Timer2
• Timer0 and Timer2 are 8-bit timers while Timer1 is a 16-bit
timer.

8
Programming the timer
• In AVR, for each of the Timers, there is a counter register
called TCNTn (Timer/CouNTer). That is, in ATmega32, there
are TCNT0, TCNT1 and TCNT2 registers.
• Upon reset, TCNTn registers contains zero.
• It counts up with each pulse (external or internal)
• You can write values to TCNTn registers or you can also read
them.

9
Programming the Timers
• Each timer/counter has a Timer OVerflow Flag called TOVn.
• When a Timer/Counter overflows, TOVn flag is set.
• Each Timer/Counter has also Timer/Counter Control Register
called TCCRn for setting modes of operation.
• For example, you can specify Timer0 to work as TIMER or
COUNTER by loading proper values in TCCR0 register.

10
Programming the Timer
• Each timer has also OCRn (Output Compare Register) and OCFn
(Output Compare Flag) in TCCRn register.
• The content of TCNTn register is compared with OCRn register,
when they are equal OCF flag is set at immediate next clock.
– Let us say TCNT0 register is cleared (set zero). And OCR0 register
is loaded with 99.
– If the timer is started, TCNT0 register will start increasing.
– When TCNT0 register becomes equal to OCR0 register, OCF0 flag
is set at immediate next clock.
– In this case, 99+1=100 clock counting is performed.

11
Timer0 Programming
TCCR0 Register
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00

 FOC0 – Bit 7 – Force Compare Match. This is a write only bit,


writing 1 to it causes to act as if a compare match has occurred.

 WGM00, WGM01 – Bit 6 and 3 – Mode Selector


0 0 – Normal
0 1 – CTC (Clear Timer on Compare Match)
1 0 – PWM, Phase Correct
1 1 – Fast PWM

12
Timer0 Programming (Contd.)
TCCR0 Register
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00
• COM 01:00 – Bit 5 and 4
Compare output mode This mode controls the waveform
generator (will be discussed in another lecture)
• CS02:00 – Bit 2, 1 and 0 – Timer clock selector
(see the next slide)

13
Block Diagram for Timer0
CLKI/O STOP
0
CLK
1 WGM01 WGM00
CLK/8
2
CLK/64
3 MUX
PRESCALER CLK/256 CONTROL UNIT
4
CLK/1024
5
Falling Edge
EDGE DETECTOR 6 UP/ CLEAR
Rising Edge COUNT
7 2 0 DOWN OCR0
1 TCNT0

TOV0 COMPARATOR =
T0
CS01 CS00
CS02
OCF0
14
T0/T1 Pins
For Timer0
For Timer1

15
Timer0 Clock Selector (CS02 : 00)
D2 D1 D0
0 0 0 - No clock source (Timer/ Counter Stopped)
0 0 1 - Clock (No Prescaling)
0 1 0 - Clock/8
0 1 1 - Clock/64
1 0 0 - Clock/256
1 0 1- Clock/1024
1 1 0 - Ext. Clk on T0 pin, Clk on Falling Edge
1 1 1 - Ext. Clk on T0 pin, Clk on Rising Edge

16
TIMSK (Timer/Counter) Mask Register
TIMSK Register [Interrupt Enable]
OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0

Timer 2 Timer 1 Timer 0


TOIE0 Timer0 overflow ENABLE
OCIE0 Timer0 output compare match ENABLE
TOIE1 Timer1 overflow ENABLE
OCIE1B Timer1 output compare B match ENABLE
OCIE1A Timer1 output compare A match ENABLE
TICIE1 Input Capture ENABLE
TOIE2 Timer2 overflow ENABLE
OCIE2 Timer2 output compare match ENABLE
17
TIFR (Timer/Counter) Interrupt Flag Register
TIFR Register
OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0

Timer 2 Timer 1 Timer 0


TOV0 Timer0 flag bit; 0- did not overflow, 1- has overflowed (FF->00)
OCF0 Timer0 output compare flag bit; 0- did not match, 1- matched
TOV1 Timer1 flag
OCF1B Timer1 output compare B match flag
OCF1A Timer1 output compare A match flag
ICF1 Input Capture flag
TOV2 Timer2 flag
OCF2 Timer2 output compare flag
18
An Example of Using Counter0
• Let us take an example of a counting external pulses.
• A switch is connected to the input timer pin, T0.
• At every press and release a pulse will be generated at the pin.
• For every 10 external pulses, a pulse of 200 mSec duration will be made
output to a pin of the uC.
• Let say at every falling edge of the pulse we want to count.
• Since pulse will be generated at every switch press and the switch is
connected at T0 pin, so at every pulse, the Timer/Counter0 register
TCNT0 will be incremented.
• If we enable the Timer Overflow Enable bit of TIMSK, TOIE0, the ISR
written for Timer0 Overflow will be executed.
• In the ISR we have to write a code to generated a 200 mSec pulse.
19
Interfacing Circuit

20
#include <mega32.h> TCNT0=initial;
#include <delay.h> OCR0=0x00;
#include <alcd.h> TIMSK=(0<<OCIE2) | (0<<TOIE2) |
#include <stdio.h> (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) |
int initial=246; (0<<TOIE1) | (0<<OCIE0) | (1<<TOIE0);
The Code
char disp[16]; lcd_init(16);
int count=0; #asm("sei")
interrupt [TIM0_OVF] void timer0_ovf_isr(void) PORTB.1=0;
{ lcd_clear();
PORTB.1=1; while (1)
delay_ms(200); {
PORTB.1=0; lcd_gotoxy(0,0);
TCNT0=initial; count=TCNT0-initial;
} sprintf(disp,"Sw Pressed : %d",count);
void main(void) lcd_gotoxy(0,0);
{ lcd_puts(disp);
}
DDRB.1=1; }
TCCR0=(0<<WGM00) | (0<<COM01) |
(0<<COM00) | (0<<WGM01) | (1<<CS02) | 21
(1<<CS01) | (0<<CS00);
Thanks
22

You might also like