magvitron
Active Member
Hi Im doing a small project for servo control using the avr mega 16. I am doing a phase correct PWM with timer 2 of the avr. the thing is driving me crazy.
here's the code:
F_CPU = 11.0592Mhz
how can i get a 50Hz wave for the control. and how can i control that. i have done the same with timer 1 with 1 Mhz Internal clock. but need this odd frequency for a better UART communication. and timer 1 is used as a counter, so I cant access that too. any help?
here's the code:
F_CPU = 11.0592Mhz
Code:
void t2_fastpwm_init() // initialization for Phase Correct PWM signal using timer 2
{
// WGM2[1:0]= 11, for Fast PWM mode
// COM2[1:0]= 10, to select non inveting mode
// CS2[2:0] =010. for prescaler=8
// TCCR1A = (0<<COM1A0)|(1<<COM1A1)|(0<<COM1B0)|(0<<COM1B1)|(0<<FOC1A)|(0<<FOC1B)|(1<<WGM11)|(0<<WGM10);
//TCCR1B = (0<<ICNC1)|(0<<ICES1)|(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(0<<CS11)|(1<<CS10);
TCCR2=(1<<FOC2)|(0<<WGM20)|(1<<WGM21)|(0<<COM20)|(1<<COM21)|(2<<CS20);
DDRD|=(1<<PD7); // selcet OC2 as output pin
TIMSK|=(1<<OCIE2); //enable Output compare interrupt
}