Reading pwm port?

Thread Starter

geoffers

Joined Oct 25, 2010
488
Hi all,
Hopefully a quick one..... Can I read from a pwm pin using a btfss portx,x on a 16f1827 or will this drive it mad? I need to know how many pwm cycles have occured between events on another pin, I could write a routine using the pwm period data and timer to increment a register, connect timer1 through a pin as counter(lose a i/o :mad:) or check the port the pwm is on, the tris bit has to be set to use the pwm. I know I could try it and see what happens but I fear there maybe problems others may have found?
Cheers Geoff
 

jpanhalt

Joined Jan 18, 2008
11,087
I have used polling of a single pin like that, but whether you may have read-modify-write problems depends on what else other pins of that port do. If you use a CCP-capable pin, you can just poll the interrupt flag for that pin without the previous worry. The interrupt flag is independent of other interrupt settings, so you do not need to enable an interrupt. You will have to clear the flag after reading.

John

Edit: You can read any interrupt flag from a pin with an interrupt, not just the CCP pin.
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
You can read from any pin, input or output, no matter the source.

You may drive yourself crazy (some pins will always read a zero in certain cases) but the device will be fine.
 

Papabravo

Joined Feb 24, 2006
21,228
For an input pin you will read whatever the external source says the pin should be 1 or 0.
For output pins you will read the content of the data latch or other peripheral controlling the output pin.
 

Thread Starter

geoffers

Joined Oct 25, 2010
488
Thanks Guys,
The PWM output pin I'm using is also a CCP2 input, will the interrupt flag be set every time the pin goes high even though its being used as a PWM output? That's handy if it does? :)
Cheers Geoff
 

Papabravo

Joined Feb 24, 2006
21,228
I don't think so. When you configure a multi-function pin for a particular purpose it is usually the case that other functions are isolated from affecting the operation. As always, check the datasheet to be sure.
 

ErnieM

Joined Apr 24, 2011
8,377
Definitely worth delving into the data sheet or something simple on the simulator or breadboard. I've had analog inputs (A2D and comparator) share a common pin when I needed to monitor a signal both ways. Reading an output port has always let you read what is actually happening there so there is no fundamental reason two functions on a CCP pin can't work together on the same pin, but do check register usage and remember they share a common timer (and I've never read thru what the Capture input does).

And even if it doesn't work on the same pin you can always wrap the output back to another pin to get an interrupt when the PWM goes one way or the other.

Lastly if you bit bang the PWM based on an interrupt driven timer you will already be inside the ISR as the bit is changing as your code is the driving force for this change.
 

jpanhalt

Joined Jan 18, 2008
11,087
I agree that the other CCP could be used in a wrap around. I was going to try simulating PWM with CCP1CON to see whether CCP1IF would get set, but upon reviewing CCP1CON, it appears that one cannot set the trigger for the flag in PWM mode:

upload_2015-4-4_20-16-4.png

While the datasheet doesn't say explicitly that the flag won't work, apparently when bits <3:2> are set, the two LSB are ignored. I may still try simulating it for something to do.

John

UPDATE: I just simulated it with MPLab SIM. Can watch RC2 PWM as well as TMR2IF, but CCP1IF does not change in the watch window.
 
Last edited:
Top