Full Version : How to Use Timers for PWM Motor Control
avr >>BEGINNERS & BUTTERFLIES >>How to Use Timers for PWM Motor Control


AVR_Admin- 05-18-2006
How to Use Timers for PWM Motor Control

Timers ROCK - Climber's info on AVR programming

The timers are the facilities built in to the AVR that do Pulse Width Modulation or PWM, counting external events, keeping time, frequency generation and other cool things.

In the simplest terms all timers do is increment or decrement by 1 the value of a register (TCNTx) with each pulse of the clock fed into it. The timers become useful when we program the AVR to do something when the value in TCNTx reaches certain values.

Such values can be MAX (the maximum value the counter can store), certain fixed constants, a value in another register or zero. The doodad inside the AVR that does all this is the compare unit. It continuously compares the counter value with what we ask it to and triggers certain events whenever there's a match; hence the term "compare match."

Each timer is different in it's resolution (16 bits or 8 bits), what things we can use for TOP (described below), what I/O pins are accessible by the counter (not all timers are attached to an I/O pin), what we can use for a clock and what events the timer triggers as it counts.

These events can activate a certain type of interrupt (which is the only way to execute arbitrary code on such events) or set/clear/toggle the value of an I/O pin.

The things that we as the human oppressor can adjust while setting all this up:

how fast it counts up or down by specifying the clock source and the prescale. I call this the input frequency.
what it does when (if) it reaches the value of TOP which can either be MAX, a certain fixed value or the value in either an output compare register (OCRx) or input capture register (ICRx).
what it does when (if) it reaches the value in the OCRx register if it isn't being used as TOP.
It is these events that makes timers so DANG useful.

There are three timers on the mega8, timer0 (8-bit), timer1 16-bit and timer2 (also 8-bit). Timer2 has an access to one I/O pin (oc2) and timer1 (16 bit timer) has access to two I/O pins (OC1A and OC1B). The remaining 8 bit timer, timer0, has no external access on the mega8.

The count for each is stored in the TCNTx register (16 bits on timer1). The counters always count up from zero except when counting down during the second half of "phase correct PWM" that timer1 and 2 can do. Timer0 always counts up.

We can set control bits to adjust how the counters work. The timers have several control bits in common:


CSxy - Clock Select - Usually used to define the pre-scale value. These are the only bits we can twiddle for timer0 other than to enable the interrupt or adjust the clock source that is shared between timer0 and timer1.

WMGxy - Waveform Generation Mode - configures timer to do PWM, frequency generation, etc. by defining what to use as TOP and what happens to TCNTx when it reaches there or MAX. These bits also are partially responsible to define what happens to the OCxy pins in these modes along with the COMxy pins described below.

COMxy - Compare Output Mode - defines how an external pin is affected by the timer. IMPORTANT! You need to set the bits in the appropriate DDRx register to configure those pins as outputs.

All three timers can be programmed to trigger an interrupt when the timer passes MAX. Timer1 and 2 can trigger an interrupt when the counter matches what's in the OCRx register. Timer1 also has an interrupt that can trigger from the input capture.

Link to Tutorial: http://members.shaw.ca/climber/avrtimers.html


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.