SSMPTC,TIRUR Electronics 2021-22 1
INTRODUCTION
➢ Many Applications need to count an event or generate time delays.
➢ Counter registers in Microcontroller
➢ Count an event & Generate Time delay
➢ Counter – To count an event
• Connect the external event source to the clock pin of the counter.
• When event occurs externally, the content of counter register incremented.
• Content of the counter register represents the number of events occurred.
➢ Timer – To generate delay
• Connect the oscillator the clock pin of the counter.
• When oscillator ticks, the content of counter register incremented.
• Content of the counter register represents how many ticks have occurred.
• As speed of oscillator is known , we can calculate tick period
SSMPTC,TIRUR•Electronics
So time elapsed can be calculated from content of counter
2021-22 3
➢ First Method
• Clear the counter at start time.
• Wait until the counter reaches certain number.
• Example :- MC with Oscillator Frequency 1 MHz
Counter increments once per micro second
If we want 100 µs clear the counter and wait until it reaches 100.
➢ Second Method
• In AVR there is a flag for each of the Counters.
The flag is set when the counter overflows
• Load counter register and wait until the counter overflows and the flag is set.
• Example :- MC with Oscillator Frequency 1 MHz with an 8 bit counter.
For time delay of 3 µs, load the counter register with $FD and wait until the flag is set.
First tick → register increments to $FE
Second tick → register increments to $FF
Third tick → register overflows and the flag is set (ie, increments to $00)
SSMPTC,TIRUR Electronics 2021-22 4
➢ The AVR has one to six timers depending on the family members
➢ They referred to as Timer 0,1,2,3,4 and 5
➢ They can be used as timer to generate delay or counter to count an external event
➢ In AVR some of the counter timer is 8 bit and some are 16 bit.
➢ In ATmega 32, there are three timers
• Timer 0 - 8 bit
• Timer 1 - 16 bit
• Timer 2 - 8 bit
SSMPTC,TIRUR Electronics 2021-22 5
SSMPTC,TIRUR Electronics 2021-22 6
SSMPTC,TIRUR Electronics 2021-22 7
For each timer there are different registers and Flags
➢ TCNTn (timer/counter) register
➢ TCCRn (timer/counter control) register
➢ OCRn (Output Compare Register)
➢ TIFR (Timer/counter Interrupt Flag register)
▪ TOVn (Timer Overflow) flag
▪ OCF (Output Compare Flag)
▪ ICF (Input Capture Flag)
SSMPTC,TIRUR Electronics 2021-22 8
TCNTn (Timer /Counter ) register
➢ For each timer there is a TCNTn (timer/counter) register.
➢ TCNT0,TCNT1 and TCNT2 for Timer0, Timer1 and Timer2 respectively.
➢ TCNTn register is a counter.
➢ Upon reset TCNTn contains zero. It counts up with each pulse
➢ The content of the timer/counter can be accessed using TCNTn
➢ We can load value in to TCNTn and read from it.
SSMPTC,TIRUR Electronics 2021-22 9
TCCRn (Timer /Counter Control Register)
➢ Each timer has TCCRn (timer/counter) register for setting mode of operation.
➢ We can specify a timer to work as a timer or counter by loading proper value to
the TCCRn.
OCRn (Output Compare Register)
➢ Each timer has OCRn (Output Compare Register).
➢ The content of OCRn is compared with the content of TCNTn.
➢ When they are equal OCFn(Output compare Flag) will set.
SSMPTC,TIRUR Electronics 2021-22 10
TIFRn (Timer /Counter Interrupt Flag Register)
➢ TIFR register contains flags of different timers.
SSMPTC,TIRUR Electronics 2021-22 11
TCNT0
➢ It is an 8bit register ,which act as counter.
SSMPTC,TIRUR Electronics 2021-22 12
TCCR0
➢ .
SSMPTC,TIRUR Electronics 2021-22 13
TOV0
➢ The flag is set when the counter overflows,going from $FF to $00.
➢ It remains set until the software clears it.
➢ In order to clear it we need to write a 1to it.
SSMPTC,TIRUR Electronics 2021-22 14
Normal Mode
➢ The content of timer/counter increments with each clock.
➢ It counts up until it reaches its max of 0xFF
➢ When it rolls over from 0xFF to 0x00,it set flag bit TOV0.
➢ This timer flag can be monitored.
SSMPTC,TIRUR Electronics 2021-22 15
SSMPTC,TIRUR Electronics 2021-22 16
SSMPTC,TIRUR Electronics 2021-22 17
SSMPTC,TIRUR Electronics 2021-22 18
SSMPTC,TIRUR Electronics 2021-22 19
SSMPTC,TIRUR Electronics 2021-22 20
SSMPTC,TIRUR Electronics 2021-22 21
Clear Timer0 on Compqre match (CTC) mode
➢ OCR0 register is used with CTC mode
➢ As like normal mode, timer is incremented with clock in CTC mode.
➢ But it counts up until TCNT0 equal to OCR0.(ie, Compare match occurs)
➢ Timer will be cleared and OCF0 flag will set when next clock occurs.
➢ When it rolls over from 0xFF to 0x00,it set flag bit TOV0.
➢ This timer flag can be monitored.
SSMPTC,TIRUR Electronics 2021-22 22
SSMPTC,TIRUR Electronics 2021-22 23
SSMPTC,TIRUR Electronics 2021-22 24
SSMPTC,TIRUR Electronics 2021-22 25
➢ Timer2 is an 8 bit timer. Hence it works same way as Timer0
➢ But there are two differences between them .
SSMPTC,TIRUR Electronics 2021-22 26
TCCR2
SSMPTC,TIRUR Electronics 2021-22 27
SSMPTC,TIRUR Electronics 2021-22 28
SSMPTC,TIRUR Electronics 2021-22 29