cancel
Showing results for 
Search instead for 
Did you mean: 

PWM outputs on non-TIM pins

russ
Associate II
Posted on October 12, 2008 at 07:45

PWM outputs on non-TIM pins

6 REPLIES 6
russ
Associate II
Posted on May 17, 2011 at 12:47

What is the easiest way to get a PWM output on non-Timer pins (ex. PD2)?

I am trying to dim a whole bunch of LEDs that are on different circuits to the same brightness level.

I was planning on using a standard timer and interrupt when a certain compare-value is reached which will correspond to the on-duration. Another timer will control the off-duration. When the ON-timer interrupt is reached it will do a bit-check to see which LEDs are active (PWM high signal needed) and which are inactive (will do nothing). The active LEDs will be turned on in this ISR. When the OFF-timer interrupt is reached the ISR will switch all LEDs off, which will cause the low value on the active LEDs.

I hope this is clear.

Essentially all I am trying to do is mask my current active LEDs with the off-duration of a PWM signal.

lanchon
Associate II
Posted on May 17, 2011 at 12:47

set up a value in ram encoding the on/off state of all the leds in a bit vector. use a timer with one compare channel. in the timebase ISR copy the value to the GPIO register. in the compare ISR clear the GPIO register.

use the same high preemptive priority for the two ISRs, with two non-preemptive levels within it: high priority for timebase ISR, low priority for compare ISR. (zero width is not possible. avoid using very wide pulse widths or int service latency might turn off the leds for full period.)

or use a single ISR and check the int pending bits. for the case that both are set, clear them both and set the GPIO register to the ram value if the pulse width >50%, or clear it if width

these methods will flicker the brightness in the presence of ISR latency. if results are unacceptable, you should measure the real period of each half-cycle by reading a timer, and adjust the length of the next same-type half-cycle by the negative of the error amount. that way, mean brightness would be exact. (you can even have more resolution than what the timer provides.)

russ
Associate II
Posted on May 17, 2011 at 12:47

Thank you. I will give this a try.

russ
Associate II
Posted on May 17, 2011 at 12:47

or use a single ISR and check the int pending bits. for the case that both are set, clear them both and set the GPIO register to the ram value if the pulse width >50%, or clear it if width <50%.

I have to confess that I don't really follow you on this.

russ
Associate II
Posted on May 17, 2011 at 12:47

I have a PWM signal being output on one the LEDs right now (PC8->Full remapped TIM3_Ch3). Is there a way for me to use the output of this pin has an external (though it is not really external) interrupt which would trigger all LEDs on (using the bit vector)?

lanchon
Associate II
Posted on May 17, 2011 at 12:47

Quote:

On 10-10-2008 at 18:44, Anonymous wrote:

or use a single ISR and check the int pending bits. for the case that both are set, clear them both and set the GPIO register to the ram value if the pulse width >50%, or clear it if width <50%.

I have to confess that I don't really follow you on this.

use a single ISR to handle two events: timebase update (reload) int and the channel compare match int. each int will have a pending flag bit.

in the ISR:

if only the timebase flag is set: clear only that flag; copy the value in ram to the GPIO register.

if only the compare flag is set: clear only that flag; clear the GPIO register.

if both flags are set: clear both flags; set the GPIO register to the ram value if the pulse width >50%, or clear it if width

this option allows zero and full brightness (though near zero and near full response will of course be limited).

note: both flags will be set when there's not enough time (or zero time) between the events. this will happen near the two extremes of brightness.