Вы находитесь на странице: 1из 30


One of those dull but important lectures

The Timer Unit Hardware PWM Variable pulse width Variable frequency w/ 50% pulse width

Counter-based Interrupts Software PWM Servos

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

something. (Not to mention homework 2)

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.

Timer Output Compare Ports

Clock Sources
Clock I/O 8 MHz External

clock Prescalar

Divide by 8, 64, 256, 1024 Timer 1 has more!

Timer Counting
Timer increments, decrements, or clears every clock

cycle What it does depends on register configuration:

Modes of Operation
Normal Fast PWM CTC

Phase Correct PWM

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

Fast PWM Mode

Useful for motor driving and for buzzers Timer counts until it reaches top, then clears Top can be set to either 0xFF or OCRA

Can set the OCA or OCB pins to toggle when counter

reaches OCRA and OCRB

This is how hardware PWM is done!

Can also set the OCA or OCB pins to a 50% duty

cycle with a varying frequency

Lets us run the buzzer

Fast PWM Mode Timing

Other Modes
CTC Essentially obsolete

Phase Correct Mode

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


Hardware PWM Configuration

Set fast PWM Mode, Top to 0xFF for maximum


These instructions are exact for timer 0, timer 1 requires more reading on your part

Hardware PWM Configuration

Pay close attention to the following table:

Set the Clock Prescalar

Remember to calculate your desired PWM frequency: f = fclk/(256*prescalar)

A few last configuration steps

You need to actually set OCRnA and OCRnB You need to set the direction of the pin to output

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

Creating soft PWM

Time for interrupts! Use normal mode

Enable the Timer overflow, and one or more of the

output compare interrupts (TIMSK)

Simplest Way to do soft PWM

First, configure your clock prescalar so that

frequency is roughly 75-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

timer overflow and output compare.

Timer Overflow Interrupt

ISR(TIM0_OVF_vect) // Or timer 1 { if(firstLEDVal != 0) { turn it on; ditto for all additional values now set OCR0A to smallest non-zero value }

Output Compare Interrupt

ISR(TIM0_COMPA_vect ) { if(firstLEDVal == OCR0A) turn off LED pin ditto for all additional values

set OCRA0A to the next smallest value }

Servos are a bit trickier than LEDs/other soft PWM Range is encoded by a pulse between 600 uS and

2400 uS that arrives ~50 times per second.

Problem of resolution

One Potential Algorithm

Change the prescalar to maximize resolution! In slow mode (prescalar of 1024), we set output

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


You should now have all the knowledge required to

complete all of homework 2.

Next Up: Housings!