Lecture - 10 (Part-I) - Timer - Counter
Lecture - 10 (Part-I) - Timer - Counter
Microprocessor
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
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
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