Control AVR 8 bit Timer-Counter0 using AVR-GCC

Timers are very important part of embedded systems. Timers can make your life much easier if used properly. Basically one good thing is that timers runs independently to main program flow. You just set up the timer and let it do its job while your main program may run and do its job. Timers are really easy to use. I will go through Atmega8 timers. Other AVR microcontroller models have similar timers. In this thread we will go through AVR 8 bit timers.

Atmega8 has two 8 bit timers/counters and one 16-bit timer counter with various abilities.

8-bit Timer/Counter0 features are:

  • Single channel counter;

  • Frequency generator;

  • External Event counter;

  • 10 – bit clock prescaler.

 

TCNT0 is 8 bit timer counter register. Timer counter can be clocked by an external clock through pin T0 or from internal via pre-scaller. Timer counter is incrementing (MAX value is 0xFF) and then restarts from BOTTOM=0x00. After timer Overflows, the flag TOV0 is written to 1 and if TOIE0 bit in SREG is set, then timer overflow interrupt is executed. TOV0 acts as a ninth bit of counter register that is only set but not cleared. When this bis is combined with timer overflow interrupt – then it is automatically cleared. If you need to start not form BOTTOM, then you can write any value to TCNT0 register at any time.

Prescaler may have values 1, 8, 64, 256, 1024. Like in picture you may see prescaling at 8 clock cycles.

 

Timer counter prescaler is controlled by writing values to TCCR0 register refer to datasheet for values.

Prescaler counter may be reset by setting bit0 (PSR10) to one in to SFIOR register. This clears prescaler counter value and this affects both 8 bit timers. PSR10 is cleared automatically after timer reset is complete.

As this timer is simplest of all timers of AVR working with it is very simple:

#include <avr\io.h>

#include <avr\iom8.h>

#include <avr\interrupt.h>

#define outp(a, b) b = a

uint8_t led;

/* signal handler for timer interrupt TOV0 */

ISR(TIMER0_OVF_vect) {

led++;

}

int main(void) {

/* use PortB for output (LED) */

outp(0xFF, DDRB);

/*enable timer overflow interrupt*/

outp((1<<TOV0), TIMSK);

/*set timer counter initial value*/

TCNT0=0x00;

/*start timer without presscaler*/

outp((1<<CS00), TCCR0);

/* enable interrupts */

sei();

/*Initial led value*/

led = 0x00;

for (;;) {

/* loop forever timer interupts will

change the led value*/

outp(led, PORTB);

}

}

 

 

Comments

need d code for 25khz timer.
or the values of TCCR1,GIFR etc..

Give more details: what is you system frequency. You can always use timer calculator: http://frank.circleofcurrent.com/cache/avrtimercalc.htm

Somebody will take a flyer on him, hoping that his legs have some freshness and that the layoff will have humbled him enough to stay quiet and contribute|Somebody will take a flyer on him, hoping that his legs have some freshness and < Women's Champ Bailey Jersey Men's Ray Lewis Jersey Elite > a href="http://www.nfl49ersteamstore.com/la michael-james-jersey/" http://ww|;