# ATmega48PA: can not run PWM on OC0A and OC2A pins

I have some troubles trying to run Fast PWM on OC0A and OC2A pins.

The code is the following:

TCNT0 = 0;
TCNT2 = 76/2;
OCR0B = 76;
OCR2B = 76;
OCR0A = 10;
OCR2A = 10;
TCCR0A = 0b11000011;
TCCR2A = 0b10000011;
TCCR0B = 0b1010;
TCCR2B = 0b1010;


So I assume that timers will run with 1:8 speed from 0 (and 38 for Timer 2) to the top defined by OCRxB registers (76) and will change pins state on TOP (76) and OCRxA (10).

That's not work: the pins OC0A and OC2A connecting to the timers (as I can not change their state by putting 1 and 0 into respecting PORTD and PORTB bits) but there is no PWM signal.

However if I try to use OC0B and OC2B pins - everything works:

TCNT0 = 0;
TCNT2 = 76/2;
OCR0A = 76;
OCR2A = 76;
OCR0B = 10;
OCR2B = 10;
TCCR0A = 0b00110011;
TCCR2A = 0b00100011;
TCCR0B = 0b1010;
TCCR2B = 0b1010;


Need to note that in this case OC0A and OC2A pins can be driven directly by changing the pin state (which proofs that I don't have any schematic flaws).

I tried to replace the values of OCRxA and OCRxB in first code but that didn't help:

TCNT0 = 0;
TCNT2 = 76/2;
OCR0A = 76;
OCR2A = 76;
OCR0B = 10;
OCR2B = 10;
TCCR0A = 0b11000011;
TCCR2A = 0b10000011;
TCCR0B = 0b1010;
TCCR2B = 0b1010;


Any thoughts?

UPDATE 1

Sorry for omitting part of the code. The ports definitions goes before the main code:

DDRB =  0b00001010;
PORTB = 0b11110101;
DDRC =   0b0000000;
PORTC =  0b1111110;
DDRD =  0b01101000;
PORTD = 0b10010011;

• Where are you setting your ports? DDRA and DDRD? – Doodle Mar 21 '16 at 11:32
• Hi, @Hayman! I ommited this part. I just put it in UPDATE 1. Please F5 the post! – Roman Matveev Mar 21 '16 at 11:35
• OC2A Uses port B data bit 3, which you have set to 0, this needs to be set as an output. OC0B and OC2B are data bits 3 and 5 on port D which you have set to 1 which explains why they're working fine – Doodle Mar 21 '16 at 11:38
• @Hayman DDRB.3 is set to one: DDRB = 0b00001010; – Roman Matveev Mar 21 '16 at 11:50
• Sorry you're right that's my mistake. However I still believe that the error lies in the definitions as you've changed the main code and checked your circuit. – Doodle Mar 21 '16 at 12:08

## 1 Answer

Bad news for me:

'You will not get a useful PWM on the OCnA pin on an AVR8 if you select a mode that has OCRnA as TOP. As you have found, you can use OCR0A as TOP and OCR0B for the duty and output on OC0B. Or use a mode with a fixed TOP, and then you can get PWM on both OC pins.'

This I received on AVRFreaks forum from a member.

So it looks like I will not be able to configure the PWM to work on OCxA pins with OCxA registers as a TOP value. :(((