wiguna149

Blog For Sharing Knowledge

Timer/Counter on AVR

Timer/Counter is a feature on avr which can count based on the clock source. It is usually used to make application which require periodical event, counter from user, measuring event time or making a PWM(pulse width modulation) for controlling some device.

There are some basic important value while using timer/counter: counter value, top value, compare value and inputcapture value. There are registers for each values.

Counter value is increasing according to the clock source. If you have the operating timer clock source on 1MHz, the counter value will be increasing with the speed 1MHz. The register for counter value is TCNTx(x shows the number of used timer).

Top value give you the maximal value of the counter before it resets to bottom(zero). The top can be set into fixed value or another register, like OCRx or ICRx. When the counter value reach the top,it is called overflow.

Compare value is used to make a flag inside a counter period.The register is(OCRx).

Input capture is a counter value which increments within the clock if there are captured events. The register is ICRx. The captured event is detected from a pin ICPx or analog comparator.

After knowing the basic value, you must know some important setting for using the timer/counter. you must set your clock source and operating mode. The register for setting the clock and modes are the TCCRx.

Clock source can be configured by setting the CS bit. you can use the AVR clock source, using prescaled clock or external clock source. The prescaler is used to set the time resolution on your timer/counter. External clock is usually used for counter application. External clock is inserted from pin Tx on AVR.

There ara 4 operation mode: normal, clear on timer compare match(CTC), fast pwm, phase correct pwm and phase and frequency correct pwm. In normal mode, the counter value is always incrementing and resets to bottom when overflow occurs. In CTC mode, the counter resets when the counter value matches the OCRx register. in fast PWM the counter for generating pwm frequency is only incrementing(one slope). the frequency is

Fpwm=Fosc/(N.(top+1))

Fpwm: PWM frequency

Fosc: AVR oscillator clock frequency

N: prescaler

top= top value

In phase correct and phase and frequency corect, the counter value is incrementing to top and the decrementing from top to bottom. because the countter valueoperation is using two slope(incrementing and decrementing), the frequency will be

Fpwm=Fosc/(2.N.(top+1))

The difference of phase correct mode and phase and frequency correct mode is that the output of phase correct mode is asymmetrical, while phase and frequency correct mode is symmetrical in a period. in phase correct mode, a period is from a top to the next top while phase and frequency correct mode a period is from a bottom to another bottom.

Phase Correct Mode timing diagram

Phase and Frequency Correct mode timing diagram

 

In generating PWM, you can use the top value to change the frequency and compare value to change the duty cycle.

There are some features that is only available in 16 bit timer(timer/counter1). Setting the counter top, input capture and phase and frequemcy correctmode pwm is only available in timer1 of AVR ATMEGA. Timer1 alse has 16 bit register and three timer/counter control register(TCCR1A,TCCR1B,TCCR1C). The detail can be seen in datasheet.

There are some setting that i haven’t mentioned about some bit in TCCRx. It sets the output mode on pin OCx, like the inverting or non inverting PWM output.

With timer/counter, you can make an application which require precision timing or counting measurement or event handler without making it all manually in your main program.

*picture taken from ATMEGA16 Datasheet

Leave a comment