Avr Timers
Avr Timers
Avr Timers
Timer Basics
The ATMega 328 has three timers available to it (Arduino Mega has 6)
max frequency of each is 16 MHz, on Arduino TIMER0 is an 8-bit timer, with 1, 8, 64, 256, 1024 prescaler options TIMER1 is a 16-bit timer, with 1, 8, 64, 256, 1024 prescaler options TIMER2 is an 8-bit timer with 1, 8, 32, 64, 128, 256, 1024 prescaler options
These timers, recall, are used for PWM pins 5&6, 9&10, 3&11, respectively
we saw that we could change the PWM frequency by messing with the frequency prescaler values but PWM frequency is not the same as clock frequency
Lecture 11 2
Prescaling& Frequency
The Arduino boards run the ATMega chip at 16 MHz
so a prescaler of 1 results in a 16 MHz clock a prescaler of 1024 results in 15.625 kHz
Register
TCCR0B TCCR1B TCCR2B
scaler values
1, 2, 3, 4, 5 1, 2, 3, 4, 5 1, 2, 3, 4, 5, 6, 7
frequencies (Hz)
62500, 7812,977, 244, 61.0 31250, 3906, 488, 122, 30.5 31250, 3906, 977, 488, 244, 122, 30.5
the top frequency is not 16 MHz, off by 256 and 512 this is because PWM is (presumably) counting a certain number of clock cycles (256 or 512) between pulses
Lecture 11
Lecture 11
Wrap Times
TIMER0 is 8-bit (0255)
when prescaler = 1, reaches full count in 16 ms when prescaler = 1024, full count in 16.384 ms
Timed Interrupts
Really handy to have timed action, despite whatever loop() is doing
could check for serial input on a regular basis could read analog signal for regular sampling could produce custom signal at specific frequency
Lecture 11
CAUTION
Messing with timer configurations can compromise other timer-based functions like
PWM outputs: analogWrite() (diff. pins diff. timers) delay() (uses timer0, depends on counter wrap) millis() and micros() (uses timer0, dep. on wrap) Servo library (uses timer1) tone() (uses timer2) but delayMicroseconds()is okay (not timer-based) others?
TIMER1 as Example
Relevant registers for setting up timer:
TCCR1A: Timer/Counter1Control Register A
sets up mode of operation
TCNT1: actual 16-bit count TCNT1 and OCR1A break into, e.g., TCNT1H and TCNT1L high and low bytes (registers) to accommodate 16 bits
Lecture 11 9
Timer 1 Registers
Lecture 11
10
TCCR1A
Upper bits are Compare Output Mode
sets behavior of Compare Match condition can toggle, clear or set OCR bits on Compare Match condition
Lecture 11
11
TCCR1B
Lecture 11
12
This is the value against which TCNT1 (L & H) is compared (also a OCR1B for alternate value)
Finally, TIFR1
Lecture 11
14
Set up registers:
TCCR1A to 0 TCCR1B: set WGM12 (for CTC), CS12, CS10 OCR1A to 23437 (OCR1AH = 91, OCR1AL to 141) TIMSK1: set OCIE1A
// set up LED for OUTPUT // clear ctrl register A // clear ctrl register B // set bit for CTC mode // set bit 2 of prescaler for 1024x // set bit 0 of prescaler for 1024x // set L & H bytes to 23437 (1.5 sec) // enable interrupt on OCR1A // reset counter to zero
ISR(TIMER1_COMPA_vect){ // results in interrupt vector in asm code state += 1; state %= 2; // toggle state 1 --> 0; 0 --> 1 digitalWrite(LED,state); // export value to pin } Lecture 11
16
Comments on Code
The bit values WGM12, CS10, etc. are defined in, e.g., iom328p.h
in hardware/tools/avr/avr/include/avr/ for example:
#define CS10 0 #define CS11 1 #define CS12 2 #define WGM12 3 #define WGM13 4 #define ICES1 6 #define ICNC1 7 #define OCR1A _SFR_MEM16(0x88) #define OCR1AL _SFR_MEM8(0x88) #define OCR1AH _SFR_MEM8(0x89) #define TIMER1_COMPA_vect _VECTOR(11) // Timer1 Compare Match A
Lecture 11
17
vector 11 is specially defined in ATMega 328 to correspond to a comparison match to OCR1A on timer 1 when this particular sort of interrupt is encountered, itll jump to program location 0x100, where:
various working registers are PUSHed onto the STACK
so the service function can use those registers for itself
the interrupt service functions are performed the STACK contents are POPped back into registers the program counter is reloaded with the pre-interruption value
A Custom PWM
ISR(TIMER1_COMPA_vect) { if (state) OCR1A = 31248; else OCR1A = 15624; state += 1; state %= 2; digitalWrite(LED,state); } // two seconds for OFF // one second for ON
Nested Interrupts
Imagine you want to respond to an external interrupt, and perform some follow-up action 2 seconds later
external interrupt arranged via attachInterrupt() within service function, set up TIMER1 counter for timed interrupt in timer ISR, reset TIMER1 to normal mode
disable interrupt condition, or youll keep coming back
Lecture 11
20
Announcements
Will review proposals over weekend Offer feedback, redirect, order parts (some) early in week New Lab times:
M 36; T 26; W 36; Th 3:003:45 and 5:156:00; F 36 will have someone there, typically 2 out of the three of us
Light tracker demo/code/paragraphs due 2/12 or 2/13 Exam on Friday 2/15 in class time
Lecture 11 21