Pinmode vs DDR issue with Timer1 PWM

Hi,
I needed to setup Timer1 in the "Phase and Frequency Correct PWM Mode"

After reading the data sheet a dozen times I felt I had all ther correct register settings but still no output (PB5 and PB6). After more register searching I added the "DDR" setup for the data direction, where I had been using the pinmode method. Voila! we had PWM.

So my question is:

Why did the DDR method work and the pinmode not?

// below 3 lines did not result in an output on the PB5 and PB6 port pins.
   pinMode(LED_BUILTIN, OUTPUT);
   pinMode(13, OUTPUT);
   pinMode(14, OUTPUT);

//replaced by these lines and the PWM functioned:
   pinMode(LED_BUILTIN, OUTPUT);
   PORTB = 0;
   DDRB = (1<<DDB2)|(1<<DDB1);

Blinking LED worked in either method, only the PWM output required the DDR form.

Anyone have any similar experience?

Thanks

JohnRob

pinMode() allows the pin to be an output or input. Connecting the PWM timer output to the pin is a separate step, done with the registers.

What Arduino do you have? Where do you see OC1A and OC1B on pins 13 and 14?

 DDRB = (1<<DDB2)|(1<<DDB1);

These are digital Pins 9 and 10 which is where the OC1A and OC1B pwm output of Timer 1 is located. This will enable the same outputs as the DDRB code.

pinMode(9,OUTPUT);
pinMode(10,OUTPUT);

Hi,

Thanks, I'll have to research "pinmode" more. Or maybe just stay with DDR's as that's where I'm most comfortable.

BTW I am using a Pro Mini Clone(ish). I figure if I donate to Arduino (when downloading the IDE) I don't feel too bad about using clone hardware.

I was thinking of writing a quick description of what I learned with Timer 1. Where do you think it would be best posted?

JohnRob

I have the same problem with PWM and Timer1 on an arduino minipro, but for me it works only the opposite way.
It works only with pinmode.

Did you find the reason?

He was using wrong number for pinMode (see reply #2).

I found my issue.

Strange, but when i was using pinmode to make pin 9 output, the PWM (mode 14) on this pin was working.
If i was set it with DDRB |= (1 << DDB1); the PWM was not working.

On the setup, after DDR settings, I was using digitalwrite command to make the outputs low.
Next i set the Timer1.

When i deleted the digital write commands and replaced them with PORT settings the issue disappeared and everything is working properly.

Thank you!