5 years, 3 months ago.

Strange PWM issue

I’m using the F303K8 to output 4x PWM waveforms, which are fed to 4x MOSFETS for dimming LED circuits.

I quite happily wrote some code to loop a fade through Red-Yel-Grn-Cyn-Blu-Mag and repeat, which works just fine…

RYGCBM code: include "mbed.h"

PwmOut redpwm(PA_8); PwmOut grnpwm(PA_9); PwmOut blupwm(PA_10); PwmOut whtpwm(PA_11);

int a; int b; float z;

int main() {

redpwm.period_us(1000); grnpwm.period_us(1000); blupwm.period_us(1000); whtpwm.period_us(1000);

z=0.05;

whtpwm.pulsewidth_us(0);

while(1) {

for(a=0; a<=1000; a++) { b=1000-a; blupwm.pulsewidth_us(b); redpwm.pulsewidth_us(a); wait(z); }

for(a=0; a<=1000; a++) { b=1000-a; redpwm.pulsewidth_us(b); grnpwm.pulsewidth_us(a); wait(z); }

for(a=0; a<=1000; a++) { b=1000-a; grnpwm.pulsewidth_us(b); blupwm.pulsewidth_us(a); wait(z); } } }

Then I decided to write a different bit of code to randomise a colour, hold it for a second, then fade to another random colour. I haven’t written the fade bit yet as I’ve discovered a very annoying problem. The red channel on PA_8 does not work when running this code, whereas it works just fine with the RYGCBM code above. I noticed the pins were floating a bit so have put pull-down resistors on each output but this hasn’t solved the problem – PA_8 is still dead while PA_9, PA_10 and PA_11 are fine.

Random colour code: include "mbed.h"

PwmOut redpwm(PA_8); PwmOut grnpwm(PA_9); PwmOut blupwm(PA_10); PwmOut whtpwm(PA_11);

int a; float z;

int redval; int bluval; int grnval; int whtval;

Serial pc(SERIAL_TX, SERIAL_RX);

int main() {

redpwm.period_us(1000); grnpwm.period_us(1000); blupwm.period_us(1000); whtpwm.period_us(1000);

z=1;

while(1) {

redval = rand()%1001; bluval = rand()%1001; grnval = rand()%1001; whtval = rand()%1001;

redpwm.pulsewidth_us(redval); grnpwm.pulsewidth_us(grnval); blupwm.pulsewidth_us(bluval); whtpwm.pulsewidth_us(whtval);

pc.printf("R%d G%d B%d W%d ",redval,grnval,bluval,whtval);

wait(z); } }

I put the pc.printf command in there to output the colour values to a hyperterminal, so I could check that was working OK – it is.

I have even changed the original code into the second bit of code, line by line, in an attempt to find the problem. In that instance, I found PA_8 stops working when I remove the for loops?!?!?! Very confusing! I've also tried moving the PWM from PA_8 to PA_12 and I still encounter the same problem.

Can anyone help? This is driving me nuts!!

Question relating to:

Affordable and flexible platform to ease prototyping using a STM32F303K8T6 microcontroller.

2 Answers

5 years, 3 months ago.

I don’t know that I have an answer, but a few thoughts: First it would be good to format question using the code tags so the code is legible.

You need to make sure you understand exactly how you want to use the PWM/Timer hardware before creating the PWM objects. You might need to take a look at the ST datasheet on PWM and timers. You might want to install STM32 Cube Configurator which can help sort out the pin configurations. Also these sorts of issues are much easier to debug if you can export to an offline toolchain with a debugger and step through the code.

In mbed for your target the peripheral pins are configured here:

https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/TARGET_NUCLEO_F303K8/PeripheralPins.c

By default PA_8 = TIM1_CH1, PA_9 = TIM1_CH2, PA_10 = TIM1_CH3, PA_11 = TIM1_CH1N. So PA_11 by default is the same as channel 1 just inverted. It’s possible this is frustrating your use of PA_8, since they are derived from the same place. I believe if you write to PA_11 it will also change the PA_8 value.

Try removing PA_11 and seeing if that works. Then you can try using pin name PA_11_ALT0, which should map to TIM1_CH4 which should be an independent channel.

I don’t have that board, so I can’t try this.

5 years, 3 months ago.

When I looked at Graham S' post, I saw that in the pin declarations that PA_11 is the complement output of PA8 so that is probably related to the issue.

However, I also noticed that somebody was aware of this and you can use PA_11_ALT0 in your declaration and it will switch to PWM4.

PwmOut redpwm(PA_8);
PwmOut grnpwm(PA_9);
PwmOut blupwm(PA_10);
PwmOut whtpwm(PA_11_ALT0);