Mega16 PWM
2008-02-24 by J C
I am trying to control 2 servos with an ATMEGA16 running off of an 8MHz crystal. I have tried to figure it out with various code examples on the internet but don't seem to be getting it. Here is my code, any ideas?
#include <avr/io.h>
#define F_CPU 8000000
// Set for Prescaler of 8 at 8MHz xtl = 1us
#define Timer1ON() TCCR1B|=0x0D
#define Timer1OFF() TCCR1B&=0xF7
int main(void)
{
Timer1OFF(); // stop timer 1
// Set for PWM (Table 47, mode 10 page 112
// of Mega16 datasheet)
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);
TCCR1B = _BV(WGM13);
// 20000 * 1us = 20ms or 50Hz
ICR1 = 20000;
// set the port pin and direction
DDRD |= 0x30;
PORTD &= (0xFF ^ 0x20);
Timer1ON();
while(1)
{
OCR1A = 1500;
OCR1B = 1500;
}
}
---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search.
[Non-text portions of this message have been removed]