Escolar Documentos
Profissional Documentos
Cultura Documentos
Outline
The Timer Unit Hardware PWM Variable pulse width Variable frequency w/ 50% pulse width
Timer Units
Used for generating interrupts with periodicity 8 bit or 16 bit Attiny has two 8-bit timers Clocked by I/O clock OR by external input Can tie to hardware to generate PWM signals Can use for generating software PWM or servo signals Bottom line: Nearly all your gadgets need to use a timer for
Timer Interrupts
Timer Overflow Interrupt (TOVn) Marks when the timer overflows typically when it hits the maximum counter value Output Compare Interrupt (OCRAn, OCRBn, etc) Multiple per interrupt Set output compare to some number, when the timer hits this number, an interrupt is triggered.
Clock Sources
Clock I/O 8 MHz External
clock Prescalar
Timer Counting
Timer increments, decrements, or clears every clock
Modes of Operation
Normal Fast PWM CTC
Normal Mode
Timer just counts up and overflows Timer Overflow Interrupt is set
The Output Compare Unit can be used to generate interrupts at some given time. Using the Output Compare to generate waveforms in Normal mode is not recommended, since this will occupy too much of the CPU time. But, for LEDs and servos, normal mode is just fine
Other Modes
CTC Essentially obsolete
Hardware PWM
Doesnt Require Interrupts Register Configuration is key Sections 11.9 and 12.3 are your friend You need to set bits in registers TCCRnA and
TCCRnB
resolution.
These instructions are exact for timer 0, timer 1 requires more reading on your part
Configuring a Buzzer
A frequency (with 50% duty cycle) waveform output
in fast PWM mode can be achieved by setting OC0x to toggle its logical level on each Compare Match (COM0x1:0 = 1). The waveform generated will have a maximum frequency of fOC0 = fclk_I/O/2 when OCR0A is set to zero. This feature is similar to the OC0A toggle in CTC mode, except the double buffer feature of the Output Compare unit is enabled in the fast PWM mode. - page 76, Attiny85 datasheet
Wait a minute
If that fails
Just use CTC mode
A Note on Timer 1
Read section 12 of datasheet Timer 1s operation is similar but subtly different,
register settings are not identical Also, timer 1 runs on a 64 MHz clock(!!), and has 14 prescalar options
Soft PWM
Tri color LEDs have three pins Itd be cool if they could all fade so that the LED could be any color Servos require complicated pseudo-PWM signals Fortunately, both of these only need signal
frequencies of 50-150 Hz
Start w/ 8 MHz clock 256 count steps per period 31250 Hz is the maximum frequency Divide by 256 to get 122 Hz sounds good!
Create global volatile ints for your LED values Give them values between 0 and 255 0 for off, 255 for fully on Then, you will need to enable two ISR routines
Servos
Servos are a bit trickier than LEDs/other soft PWM Range is encoded by a pulse between 600 uS and
compare to trigger every 20 ms In fast mode (prescalar of 64), we turn our pins on, and turn them off when the appropriate amount of time has gone by (and then we reset the counter and go back to slow mode). Timer 1 has many more prescalar options, so this may work better with timer 1 16 bit timers made this much easier
Questions?