I'm having a hard time to set a 16 bit PWM on atmega328 that works with frequency of 50Hz and have a duty cycle that can be varied. I also have my doubts that if I can have an ADC working together with this 16 bit PWM since both use timer1. There are the auto trigger source selection ADTSX on the register ADCSRB that I supposed should be able to select the clock source for my analog conversion but I've setted and nothing changed, I couldn't have timer1 ISR and analog conversion at same time and I didn't tested for PWM and ADC together because I don't know how to set the PWM.
This is the code just for setting PWM that I've right now:
#include <avr/io.h>
int flag = 0;
ISR(TIMER1_COMPA_vect) {
if (flag == 0)
OCR1A = 11;
else
OCR1A = 160;
}
int main(void) {
DDRB |= (1 << DDB1);
// PB1 as output
OCR1A = 160;
// set non-inverting mode
TCCR1A |= (1 << COM1A1);
// set 10bit phase corrected PWM Mode
TCCR1B |= (1 << WGM12);
// set no prescaler
TCCR1B |= (1 << CS10);
while (1)
{
}
}
PS: I'll be varying the duty cycle between 1ms to 2ms activating an ESC controlling a brushless motor.