cancel
Showing results for 
Search instead for 
Did you mean: 

16bit PWM output is jittery using ST Arduino core on STM32F103C8T6

Dw.726
Associate

I got my Bluepill STM32F103C8T6 device working in the Arduino IDE with the STM32duino bootloader recently.

I was curious about 16bit PWM as opposed to the crude PWM output of standard Arduinos.

Using core 'maple' from platform in folder: C:\Users\dentaku\Documents\Arduino\hardware\Arduino_STM32\STM32F1

I can do pwmWrite and get a very high resolution 16bit PWM.

I have an optical encoder that updates a variable which I use to change the value of pwmWrite and 2 LEDs conected to PA8 and PA9 fade up and down according to that value.

but... switching over to ST's arduino core which doesn't use pwmWrite but just regular old analogWrite.

Using board 'GenF1' from platform in folder: C:\Users\dentaku\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.7.0

Using core 'arduino' from platform in folder: C:\Users\dentaku\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.7.0

In void setup () I set the resolution to 16

analogWriteResolution (16);

in my main loop

if (lastValue != encoderValue) { //only update if encoderValue has changed
    Serial.print(encoderValue); //NOT println to create two seperate traces in Serial Plotter
    analogWrite(LED1, encoderValue); //set PWM amount for LED1 linearly
    Serial.print(","); //divider to create two seperate traces in Serial Plotter
    analogWrite(LED2, ceil(pow(1.000169, encoderValue) - 1)); // --set PWM amount for LED2 pin exponentially
    Serial.println(ceil(pow(1.000169, encoderValue) - 1));
    lastValue = encoderValue;
  }

This seems to give me 16bit PWM output from PA8 and PA9 (LED1 and LED2) but looking at it on my scope it gives me all kinds of weird down-going spikes while the encoder is turning that I don't see at all when using the MAPLE core.

0690X00000ArcCBQAZ.png

It's fine as long as the encoder is not turning though.

The encoder code isn't spitting out random zeros either because if I plot the value being sent to PA8 and PA9 in Serial Plotter it creates perfectly smooth lines.0690X00000ArcBhQAJ.png

I guess I'll try it without the encoder and simply use a for loop to increment and decrement a variable I can analogWrite to those two pins instead, to narrow down the problem.

1 REPLY 1

There is not much Arduino-specific knowledge on this forum.

Maybe you should go to the source of those libraries, and try to find help there.

If you are willing to find and fix the root cause of problem yourself, dig down to the functions that actually manage the PWM, and find out how exactly do they work. Then, if you post relevant snippets of code, or links to its sources, we might perhaps try to work out what's wrong and how could that be fixed.

JW

PS. Change your username to a normal nick.