- Mon Jul 30, 2007 5:19 pm
#33117
Hi need some help I am trying to get PWM working using fast PWM on the ATMEGA128. I have had so many problems!! I have got to the point where I can generate a 1ms pulse but it will only pulse once. It appears to reset the timer at the TOP ICRn and the pulse is the same as what is in the OCR1A register however when the counter resets the pin does not go high again. Any ideas?????
Code: Select all
# include <avr/io.h>
#include <avr/interrupt.h>
void init_servo(void)
{
/* Reset Control Register*/
TCCR1B = 0x00;
/* Set Output Register*/
DDRB=0xFF;
/*Reset Counter*/
TCNT1H = 0;
TCNT1L = 0;
/*Set TOP to 40000 for 20ms period*/
ICR1H = 40000U >> 8;
ICR1L = 40000U & 0xff;
/*
Enable 2 PWM ports */
TCCR1A = 0x52;
/* Prescaler of 8
ICR1 = overflow value
Fast PWM,mode 14(WGM13 & WGM12 HIGH) */
TCCR1B = 0x1A;//start timer
}
void set_servo_A(unsigned value)
{
OCR1AH = value >> 8;
OCR1AL = value & 0xff;
}
void main(void)
{
init_servo();
set_servo_A(2000); //-90 degrees
for(;;)
{
}
}