cancel
Showing results for 
Search instead for 
Did you mean: 

Question regarding PWM

matic
Associate III
Posted on November 14, 2015 at 19:02

Hi.

I'm using timer in PWM mode to control LED. If output is always 1 (CCR register is equal to ARR), LED is green. If output is 0 (CCR is always 0), LED is red. If I set duty cycle to 50% at high frequency, LED is orange. And if I increase a prescaler when duty cycle is 50%, I achieve visible toggling.

Now, I would like to achieve one more thing: When LED is constantly green, I would like to make it red for 200 ms when an error occurs and after that it should go back to green (CCR constantly equal to ARR). Of course, I would like to do that without looping in a while loop for 200 ms. Do you have any idea how to achieve that?

I am using F303.

#pwm
8 REPLIES 8
vincenthamp9
Associate III
Posted on November 15, 2015 at 10:13

How about a one-shot timer configured for 200ms?

If an error occurs, set the CCR register to zero and trigger the one-shot timer. Within the timer interrupt reset the CCR register to ''green'' (ARR).

matic
Associate III
Posted on November 15, 2015 at 15:36

Thanks, I will try that with one pulse mode.

Posted on November 15, 2015 at 16:06

What frequency are you running the PWM? I don't see any reason why you'd spend 200ms waiting for something, if the PWM was 1KHz, you could just count 200 interrupts, and then switch the pulse width.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
matic
Associate III
Posted on November 15, 2015 at 17:00

In normal operating, when green is ON, frequency doesn't matter much. Becouse CCR is equal to ARR (output always 1). When error occurs, I want to signal that with one pulse of red color (red is on when PWM output is 0) and after that I want the LED is green again. 

Also, I would prefer to do that without many interrupts.
mikael239955_stm1_st
Associate III
Posted on November 15, 2015 at 19:29

You can complexity by using a timer in trigged mutlibyte dma bursts, 1 byte is red

second byte is green, third is brown... for example. No interrupts at all just dma.

But as allready mentioned  why waist 200ms on waiting? Anti flicker is very low frequency process.
matic
Associate III
Posted on November 15, 2015 at 21:48

I want to turn red LED on for 200 ms to signal an error. During that 200 ms program should continue without interrupts. I could afford interrupt only at the beginning and at the end of that 200 ms period, to reconfigure timer registers. I don't want to lose much time with LED.

Posted on November 15, 2015 at 23:14

So get a free running timer, and park a CC event 200 ms in the future. Have that interrupt or poll it periodically as your code runs on. ie TIM2->CCR1 = TIM2->CNT + 20000; for 10us ticker

You're really going to have to own your design and its constraints, and get a lot more familiar with the chip.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
matic
Associate III
Posted on November 16, 2015 at 20:48

I think I can do this with timer One-pulse mode. With increased prescaler, I could achieve 200 ms long pulse.

But is it possible to make inverted pulse? I mean, not pulse from 0 -> 1 and back to 0. But from 1 -> 0 and then back to 1.