Using PWM output as I/O

Thread Starter

johndeaton

Joined Sep 23, 2015
63
Hi All,

I have an application where I want a pwm output at times and a constant on signal some times. I'm using a Microchip PIC16F1508. Is there a way to do this without reinitializing the port and pin every time?

Thanks
 

Papabravo

Joined Feb 24, 2006
21,159
You need to read the datasheet very carefully to see if you can set the PWM duty cycle to 0% and 100%. If you can do that then you can have the pin output a constant value.
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Probably. As PB indicated according to Equation 23-2:
Pulse Width = PWMxDCH PWMxDCL<7:6> x
TOSC x (TMR2 Prescale Value)
That implies that if DCH/L == 0, the pulse width is 0 so the pin level should be constant low.

You'll get a constant high if you set the pulse width > the period as noted later in the section:
Note: If the pulse width value is greater than the
period the assigned PWM pin(s) will
remain unchanged.
That said, I would prefer to do things explicitly i.e. turn the PWM off for a steady pin level then back on AND re-initialized for PWM. That way you avoid worrying about left over time in the PWM registers before the pin changes etc. But it should more or less work as you described. Give it a try and let us know.

Good luck.
 
Top