Hello everyone,

I'm recently trying the ATmega328p because of it's PWM capabilities.

I'm trying to obtain 4 PWMs at around 350Hz, for this, I have already used the timer1 with custom TOP (ICR1). I'm trying to do something similar for timer2 but it doesn't seem to work since the TOP is set by OCR2A and OCR2B, but I don't know how to setup the duty of the PWM.

Here's my code, with timer1 PWM working, based on the code on this website: https://sites.google.com/site/qeewiki/books/avr-guide/pwm-on-the-atmega328

#includeint main(void) { //TIMER1 DDRB |= (1 << DDB1)|(1 << DDB2); // PB1 and PB2 is now an output ICR1 = 25; // set TOP to 180 OCR1A = 10; // duty 177 OCR1B = 10; // duty 177 TCCR1A |= (1 << COM1A1)|(1 << COM1B1); // set none-inverting mode TCCR1A |= (1 << WGM11); TCCR1B |= (1 << WGM12)|(1 << WGM13); // set Fast PWM mode using ICR1 as TOP TCCR1B |= (1 << CS12); // 256 presc //TIMER2 DDRD |= (1 << DDD3); DDRB |= (1 << DDB3); // PD3 and PB3 are now outputs OCR2A = 2; OCR2B = 2; // TOP TCCR2A |= (1 << COM2A1); TCCR2B |= (1 << COM2B1); // set none-inverting mode TCCR2A |= (1 << WGM21) | (1 << WGM20); TCCR2B |= (1 << WGM22); // set fast PWM Mode TCCR2B |= (1 << CS21)| (1 << CS22); // set prescaler to 256 and starts PWM while (1); { // we have a working Fast PWM } }

Thanks for any help!