Control AVR 8 bit Timer-Counter0 using AVR-GCC

Timers are an essential part of embedded systems. They make your life much more comfortable if used properly. Timers run independently to the main program flow so that they can do the job individually without disturbing precious calculations done by the CPU. You just set up the timer and let it run while your main program performs other tasks. Timers are easy to set up. In this post, an example is made for Atmega8 timers. Other AVR microcontroller models and even different brand chips have similar timers. Atmega8 has two 8-bit timers/counters and one 16-bit timer counter with many abilities. Standard 8-bit Timer/Counter0 features are: TCNT0 is an 8-bit timer counter register that keeps the current count number. The timer counter can be clocked by an external clock through pin T0 or from internal via pre-scaler. The timer counter increments (MAX value is 0xFF) and then restarts from BOTTOM=0x00. After the 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 bit is combined with timer overflow interrupt –…