Atmega8 Uc: Timer0 !!!
Atmega8 Uc: Timer0 !!!
Timer0 !!!
integrated in
most of the
Atmega8
Timer 0
Timer 1
Timer 2
Timer 0
Timer 1
Timer 2
8-bit
16-bit
8-bit
Registers of Timers/Counters
There are four registers to control the operation & data storage TCNTx= Timer Counter Register TCCRx= Timer/Counter Control Register TIMSK= Timer Interrupt Mask Register TIFR= Timer Interrupt Flag Register
TIMER/COUNTER 0 REGISTER
TCNTx
This magical 8 bit register increases its own value with a fix rate. This register dont require CPU to increase its value. TCNTx keeps the count value from 0-255, it must be initialized in program code. When the count value reaches maximum, it raises a interrupt bit in another register and reset to 0 on overflow
The timer control register controls Timer Frequency, this is achieved by setting the prescalar value
CS00-CS02 are used to select PRESCALER value and hence determines the frequency of the timer
PRESCALAR
Bit 0 or TOIE0 bit of TIMSK is used to enable/disable overflow interrupt of TIMER0 Storing 1 at this place will Enable the overflow interrupt of TIMER0 Storing 0 at this place will Disable the overflow interrupt of TIMER0 Other bits are used for other timers
OVERFLOW MODE
Once the timer reaches it highest counts and if it not resets it is said to be in Overflow Mode
For this mode we have to set 3 registers TCNT0 (To initialize counter) TCCR0 (To set Prescalar) TIMSK (To enable overflow interrupt)
Contd
count=0; sei(); //Enable Global Interrupt while(1); } //main close ISR(TIMER0_OVF_vect) // interrupt service routine { if(count==61) // if count reaches 1 sec { second++; sprintf(seconds:%d, second); lcd_puts(buffer); // for printing seconds on LCD count=0; // reset the count variable } else count++; } //ISR function close
Description of program
if(count==61) // if count reaches 1 sec 61 is decided by clock frequency and the prescalar. Formula for calculating number of interrupts for 1 second time duration is : Timer frequency = CPU clock frequency/Prescalar No of overflow interrupts = Timer frequency/256 In our case: Timer frequency = 1000000/64 = 15625 No of interrupts = 15625/256 = 61.035 61
THANK YOU!!!