Date post: | 06-Nov-2015 |
Category: |
Documents |
Upload: | dangkhuyenmai |
View: | 229 times |
Download: | 2 times |
AVR Timer/Counter
Prof Prabhat RanjanDA-IICT, Gandhinagar
8-bit Timer/Counter0with PWM
Single Compare Unit Counter Clear Timer on Compare Match (Auto Reload) Glitch-free, Phase Correct Pulse Width Modulator
(PWM) Frequency Generator External Event Counter 10-bit Clock Prescaler Overflow and Compare Match Interrupt Sources
(TOV0 and OCF0)
16-bit Timer/Counter
The 16-bit Timer/Counter unit allows accurate program execution timing (event management), wave generation, and signal timing measurement. The main features are:
True 16-bit Design (i.e., Allows 16-bit PWM) Two Independent Output Compare Units Double Buffered Output Compare Registers One Input Capture Unit Input Capture Noise Canceler Clear Timer on Compare Match (Auto Reload)
16-bit Timer/Counter
Glitch-free, Phase Correct Pulse Width Modulator (PWM)
Variable PWM Period Frequency Generator External Event Counter Four Independent Interrupt Sources (TOV1, OCF1A,
OCF1B, and ICF1)
8-bit Timer/Counter2
General purpose, single compare unit, 8-bit Timer/Counter module
Single Compare unit Counter Clear Timer on Compare Match (Auto Reload Glitch-free, Phase Correct Pulse Width Modulator
(PWM) Frequency Generator 10-bit Clock Prescaler Overflow and Compare Match Interrupt Sources
(TOV2 and OCF2) Allows clocking from External 32 kHz Watch
Crystal Independent of the I/O Clock
Based on
Newbie's Guide to AVR Timers - Dean Camera
Introduction
AVR timers are very versatile but at the same time complex
Asynchronous to man AVR core Separate circuits on the AVR chip which can run
independent of the main program, interacting via the control and count registers, and the timer interrupts
Timers can be configured to produce outputs directly to pre-determined pins, reducing the processing load on the AVR core
Like all digital systems, the timer requires a clock in order to function
As each clock pulse increments the timer's counter by one, the timer measures intervals in periods of one on the input frequency:
Code: Timer Resolution = (1 / Input Frequency) Smallest amount of time - one period of the
incoming clock signal. If we supply a 100Hz signal : Code: Timer Resolution = (1 / Input Frequency) Timer Resolution = (1 / 100) Timer Resolution = .01 seconds
ATMega16@1 MHz
Assume the target to be a MEGA16, running at at 1MHz clock.
Modern AVRs come running off their internal ~1MHz RC oscillator by default (will be slightly incorrect due to the RC frequency tolerance).
In the sections dealing with toggling a LED, it is assumed to be connected to PORTB
Simple Example(Flash LED every 1/20 s)
int main (void) {
DDRB |= (1
Timer/Counter1 Control Register B TCCR1B
Bit 2:0 CS12:0: Clock Select Bit 4:3 WGM13:2: Waveform Generation Mode Bit 5 Reserved Bit Bit 6 ICES1: Input Capture Edge Select Bit 7 ICNC1: Input Capture Noise Canceler
Clock Select Bit (CSxx)
Prescaler Value | Resolution @ 1MHz 1 | 1uS 8 | 8uS 64 | 64uS 256 | 256uS 1024 | 1024uS
Value to count
1/20 s = 0.05 s Clock minimum interval = 1 s = 1.e-06 s Count = 0.05 s/ (1.e-06 s) = 50000 -1 = 49999
Pre-scaled Timer
Need to measure longer interval, you can prescale clock before bring fed to timer
If you want to create exact 1 secondPrescaler Value | Target Timer Count
1 | 999999 8 | 125000 64 | 15624 256 | 3905.25 1024 | 975.5625
int main (void) { DDRB |= (1
CTC Timer Mode
CTC Clear on Timer Compare Compares in hardware the current timer value
against the wanted value, and when the wanted value is reached a flag in a status register is set and the timer's value reset
Handy - comparing is done in hardware checking status register much faster than comparing bytes
Pseudo Code
Set up LED hardware Set up timer in CTC mode Set timer compare value to one second WHILE forever
IF CTC flag IS EQUAL TO 1 THEN Toggle LED Clear CTC flag
END IF END WHILE
DDRB |= (1
for (;;) { if (TIFR & (1
CTC Mode using Interrupts
AVR timers can have several different Interrupts Overflow Compare Capture
Consider only the Compare interrupt, which occurs in CTC mode when the compare value is reached
Pseudocode
Set up LED hardware
Set up timer in CTC mode
Enable CTC interrupt
Enable global interrupts
Set timer compare value to one second
WHILE forever
END WHILE
ISR Timer Compare
Toggle LED
END ISR
Setting Registers
DDRB |= (1
ISR
ISR(TIMER1_COMPA_vect) { PORTB ^= (1
PURE HARDWARE CTC Look at COM1A1/COM1A0 and COM1B1/COM1B0 Allow us to control the hardware behaviour when a compare occurs Configured to set, clear or toggle the OCxy (where "x" - timer
number, "y" - channel letter for timers with more than one channel) hardware pins when a compare occurs.
Output Compare Pins
OC0 -> PB3 OC1B -> PD4 OC1A -> PD5 OC2 -> PD7
Code Fragment DDRD |= (1
Pulse Width Modulation
Ref : http://extremeelectronics.co.in/avr-tutorials/
PWM Waveform Terms
12.5% Duty Cycle
AVR Timer/Counter Sequence for fast PWM
Output Compare Register - OCR0
PWM Generation