This note briefly describes how to set up PWM on an ATmega32. In this instance, Timer/Counter 1 (16-bits wide) is used to set up a Fast PWM-mode PWM wave to drive a typical R/C servo. The PWM period is 20 ms (50 Hz), and the pulse width varies from 0.9 ms to 2.1 ms.
With a 16 MHz clock, the full 16-bit count wraps around in about 780 µs. Since we need a PWM period of 20 ms, this is clearly too fast. Prescaling Timer/Counter 1 by dividing the sytem clock by 64 gives us a full-count period of 50 ms. By setting the upper limit of the count using ICR1 to 12500, this results in the desired 20 ms period.
Update: I’ve gotten some math wrong in the preceding paragraph, and I'm not sure what I was thinking. It seems that a full 16-bit count would take 4.096 ms at 16 MHz. Also, the comments in the code regarding prescaling values don’t all agree. Sorry about that. I hope you can still find this useful.
To get the appropriate servo pulse width between 0.9 ms and 2.1 ms, the OCR1A register is loaded with values between 225 and 525.
Circuit Set-up
Please excuse the lack of a schematic; I simply don’t have time to create one right now.
The ATmega32 is set up to run with a 16-MHz crystal oscillator. On the PDIP 40 package, the following pins are connected:
Pin |
Description |
Connections & Use |
6 |
PB5/MOSI |
ISSP |
7 |
PB6/MISO |
ISSP |
8 |
PB6/SCK |
ISSP |
9 |
RESET |
Through 10 kΩ resistor to Vcc, N.O. switch to GND, ISSP |
10 |
Vcc |
Power, 0.1 µF ceramic cap to GND, ISSP |
11 |
GND |
Ground, ISSP |
12 |
XTAL1 |
To 16 MHz crytsal, 10 pF ceramic cap to GND |
13 |
XTAL2 |
To 16 MHz crytsal, 10 pF ceramic cap to GND |
19 |
PD5/OC1A |
To Servo signal line |
30 |
AVcc |
Power, 0.1 µF ceramic cap to GND, ISSP |
31 |
GND |
Ground, ISSP |
40 |
PA0/ADC0 |
To LED, LED to 470 Ω resistor, resitor to GND |
Fuse Set-up
The fuses are set for a 16 MHz external crystal oscillator.
Name |
Description |
Value |
Fuse Lo |
Fuse Low Byte |
11101111b, 0xEF |
Fuse Hi |
Fuse High Byte |
10001001b, 0x89 |
Register Set-up
The following registers are set, in this order. Although the order isn’t strictly necessary, some subset of it might be (be specific).
Name |
Description |
Value |
DDRA |
Port A Direction |
00000001b, 0x01 (PA0 output, rest input) |
DDRA |
Port B Direction |
00000000b, 0x00 (all input) |
DDRD |
Port D Direction |
00110000b, 0x30 (PD4, PD5 output, rest input) |
TCCR1A |
Timer/Counter 1 Configuration A |
10100010b, 0xA2 (OC1A & OC1B set at BOTTOM, clear on compare match, WGM=14 [fast PWM, ICR1 is TOP]) |
TCCR1B |
Timer/Counter 1 Configuration B |
00011011b, 0x1B (prescale ClkI/O/64) |
ICR1 |
In Fast PWM mode, becomes TOP |
12500d (20 ms PWM period with ClkI/O/64 prescale) |
Code
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#define kPinInitCompleteLEDÂ Â Â Â Â Â Â Â Â PA0
#define kDelay                      10000
#define kReverseDelay               300000
#define kLowerLimit                300                Â
#define kUpperLimit                450                Â
int
main()
{
    volatile long       d;
    short               dir = 1;
   Â
   Â
   Â
    DDRA = _BV(kPinInitCompleteLED);
    DDRB = 0;                                          Â
   Â
   Â
   Â
    DDRD = _BV(PD4) | _BV(PD5);
   Â
   Â
   Â
   Â
   Â
    TCCR1A = _BV(COM1A1) | !_BV(COM1A0)                Â
                | _BV(COM1B1) | !_BV(COM1B0)           Â
                | !_BV(FOC1A) | !_BV(FOC1B)            Â
                | _BV(WGM11) | !_BV(WGM10);            Â
   Â
    TCCR1B = !_BV(ICNC1) | !_BV(ICES1)                 Â
                                                       Â
                | _BV(WGM13) | _BV(WGM12)              Â
                | !_BV(CS12) | _BV(CS11) | _BV(CS10);  Â
   Â
   Â
   Â
    OCR1A = kLowerLimit;
    OCR1B = kLowerLimit;
   Â
   Â
   Â
    ICR1 = 12500;
   Â
   Â
   Â
    PORTA = _BV(kPinInitCompleteLED);
   Â
   Â
   Â
    OCR1A = kLowerLimit;           Â
    while (1)
    {
        d = kDelay;
        while (d--);
       Â
        OCR1A += dir;
       Â
        if (OCR1A < kLowerLimit)
        {
            OCR1A = kLowerLimit;
            dir = 1;
            d = kReverseDelay;
            while (d--);
        }
       Â
        if (OCR1A > kUpperLimit)   Â
        {
            OCR1A = kUpperLimit;
            dir = -1;
            d = kReverseDelay;
            while (d--);
        }
       Â
    }
   Â
    return 0;
}