Society of Robots - Robot Forum

Software => Software => Topic started by: spizzak on September 21, 2011, 05:08:35 PM

Title: Atmega8 PWM Help
Post by: spizzak on September 21, 2011, 05:08:35 PM
I'm trying to control two servos with the ATMega8 using the some of the sample code under the PWM tutorial section as follows:

Code: [Select]
void init_timers()
{
TCCR1A = 0;          // disable all PWM on Timer1 whilst we set it up

ICR1 = 19999;   // frequency is every 20ms

// Configure timer 1 for Fast PWM mode via ICR1, with 8x prescaling
TCCR1A = (1<<WGM11);
TCCR1B = (1 << WGM13) | (1<<WGM12) | (1 << CS11);

DDRB |= _BV(1);      // make port B1 an output pin
TCCR1A |= 2 <<  6;  // enable PWM on port B1 to use non-inverting mode - mode2

DDRB |= _BV(2);      // make port B2 an output pin
TCCR1A |= 2 <<  4;  // enable PWM on port B2 to use non-inverting mode - mode 2

void configure_ports(void)
{
DDRC = 0x00;  //configure all C ports for input
DDRD = 0xFF;  //configure all D ports for output
DDRB = 0xFF;  //configure B ports for output
}
}

int main(void)
{
configure_ports();         //Sets up ports for input/output
init_timers(); //Initialiazes Timer1 for two 16-bit PWM channels

while(1)
{
//Testing servos
OCR1A = ICR1 * 2 /20; // 2ms pulse to left motor on PB1
OCR1B = ICR1 * 1 /20; // 2ms pulse to right motor on PB2
}

return 0;
}


Anyone see anything that might be wrong? I get nothing on the servos, or an LED that I plugged in to attempt to debug.
Title: Re: Atmega8 PWM Help
Post by: spizzak on September 21, 2011, 07:54:55 PM
Nvm, looks like I've got it working. I MAY have been connected to the wrong header.  ::)