Set Timer for PWM and use pin as digital in/out?

Maybe not the best title, but I've searched and can't find the answer. So here it goes.

My project uses a lot of pins. Based on the physical layout of my shield and the number of components on the shield, it would be great to use pins closer to the components (minimize length of wires). The project also uses 3 of the 5 timers on the Mega2560.

So all that being said, here's the question:

Timer1 uses Pins 11 and 12.
If I set up 25kHz PWM on Timer1 and only use Pin 11 for the PWM output, can I then use Pin 12 as a normal digital in/out? Or is it locked into a PWM output only?

Thanks,
Mike

If I set up 25kHz PWM on Timer1 and only use Pin 11 for the PWM output, can I then use Pin 12 as a normal digital in/out?

Yes.

Or is it locked into a PWM output only?

No.

The PWM output functionality for each of these pins has to be activated using hardware flags. If it's not activated the pin can be used as any other digital pin.

Appreciate your response!

This is a part of the TimerOne library that I'm using to set up the PWM:

void pwm(char pin, unsigned int duty) __attribute__((always_inline)) {
	if (pin == TIMER1_A_PIN) { pinMode(TIMER1_A_PIN, OUTPUT); TCCR1A |= _BV(COM1A1); }
	#ifdef TIMER1_B_PIN
	else if (pin == TIMER1_B_PIN) { pinMode(TIMER1_B_PIN, OUTPUT); TCCR1A |= _BV(COM1B1); }
	#endif
	#ifdef TIMER1_C_PIN
	else if (pin == TIMER1_C_PIN) { pinMode(TIMER1_C_PIN, OUTPUT); TCCR1A |= _BV(COM1C1); }
	#endif
	setPwmDuty(pin, duty);
	TCCR1B = _BV(WGM13) | clockSelectBits;
    }

So if I assign pin 11 as TIMER1_A_PIN, Pin 12 will be unaffected?

Mike

So if I assign pin 11 as TIMER1_A_PIN, Pin 12 will be unaffected?

From a quick overview I would expect that. Does it behave differently? What do you see?

Do you have the processor data sheet by your side? It's the go to place for questions like this.

It's the 'COM' (Compare Output Mode) bits in the TCCR (Timer/Counter Control Register) that determine how the PWM pins act. Each pin has 2 bits: COMnx1 and COMnx0. Leaving both 0 turns off PWM and lets the pin act as a regular I/O pin. Turning on COMnx1 turns on the PWM function. Turning on both COMnx1 and COMnx0 (generally) turns on PWM but inverted.

I assume somewhere in your code the TCCR1A is set to 0. Then any bit not explicitly set to 1 will be left as 0. That would mean the two PWM pins you aren't using would remain normal I/O pins.

pylon:
From a quick overview I would expect that. Does it behave differently? What do you see?

I have not been able to test just yet. Was looking at it from a physical board layout point of view. When I do I will report back.

Mike

aarg:
Do you have the processor data sheet by your side? It’s the go to place for questions like this.

Yeah I looked at it. To be honest, got quite confused quite quickly. I need to learn more about how to manipulate the timer registers.

Mike

johnwasser:
It's the 'COM' (Compare Output Mode) bits in the TCCR (Timer/Counter Control Register) that determine how the PWM pins act. Each pin has 2 bits: COMnx1 and COMnx0. Leaving both 0 turns off PWM and lets the pin act as a regular I/O pin. Turning on COMnx1 turns on the PWM function. Turning on both COMnx1 and COMnx0 (generally) turns on PWM but inverted.

I assume somewhere in your code the TCCR1A is set to 0. Then any bit not explicitly set to 1 will be left as 0. That would mean the two PWM pins you aren't using would remain normal I/O pins.

Yes TCCR1A is set to 0!

Looking again (and finally figuring out what _BV does), the sketch only sets the COMnx1 bit based on what pin you choose to use.

That makes sense now.

Greatly appreciate the help here.

Mike