Page 1 of 1

How to use analogWrite

Posted: Tue Jan 21, 2020 10:42 pm
by forfrends
Hello!
Tell me how to do analogWrite correctly? I use simple code, but it does not work, the LED just turns on and off:

Code: Select all

const int thisPin = PC13;

void setup() {
    pinMode(thisPin, OUTPUT);
}

void loop() {
    for (int brightness = 0; brightness < 65536; brightness++) {
      analogWrite(thisPin, brightness);
      delay(10);
    }
}

Re: How to use analogWrite

Posted: Wed Jan 22, 2020 3:04 am
by mrburnette

Re: How to use analogWrite

Posted: Wed Jan 22, 2020 4:18 am
by fpiSTM
Don't know which board you use but the PC13 must have PWM capabilities.
Moreover the analogWrite has a default resolution so using 65535 values is not good, default is 8bits so there is 255 values.
https://www.arduino.cc/reference/en/lan ... alogwrite/

Re: How to use analogWrite

Posted: Wed Jan 22, 2020 7:52 am
by forfrends
I'm sorry, I forgot to indicate board. I am using STM32F103S8T6 (Blue Pill). The kernel is using this: https://github.com/stm32duino/Arduino_Core_STM32
For some reason, AnalogWright does not work. I tried like this, the result is the same:

Code: Select all

void setup() {
    pinMode(PC13, OUTPUT);
    analogWrite(PC13, 64);
}

void loop() {
}
The LED either turns on or off. There is no PWM (I checked with an oscilloscope).

Re: How to use analogWrite

Posted: Wed Jan 22, 2020 7:56 am
by forfrends
Hmm ... it’s not clear ... I just replaced PC13 with PA3 (for the test). There is PWM on PA3, but not on PC13. It seems that the PWM core is not configured for all pins.

Re: How to use analogWrite

Posted: Wed Jan 22, 2020 8:15 am
by fpiSTM
forfrends wrote: Wed Jan 22, 2020 7:56 am Hmm ... it’s not clear ... I just replaced PC13 with PA3 (for the test). There is PWM on PA3, but not on PC13. It seems that the PWM core is not configured for all pins.
So if there is no PWM linked to PC13, the analogWrite simply use it as a digitla IO. If the requested value is less than the half of the resolution value it is LOW else HIGH.
Moreover if the pin has DAC capabilities then the DAC is used in prority.

Re: How to use analogWrite

Posted: Wed Jan 22, 2020 11:18 pm
by ag123
i've not really tried stm32 core deep in, but if the speeds isn't too fast. i'd like to suggest one could hook timer interrupts. that would kind of allow one to pwm any pins by doing so using gpio. there are real speed limits with this approach though. it seemed gpio bit banging often has rather low thresholds, but it'd take experiments to find out. so for those needing the speeds, using actual hardware timer pins would be the best approach.
but if one wants 'generic' pwm on any pins, timer interrupts could be a 'work around'

Re: How to use analogWrite

Posted: Thu Jan 23, 2020 5:37 am
by fpiSTM
There is a lot of pins linked to a Timer.
The analogWrite well use the timer if available on the requested pins.
One other solution is to use directly the HardwareTimer api:
https://github.com/stm32duino/wiki/wiki ... er-library

This is what is called by the analogWrite when Timer is available.
To simulate a pwm on a pin without timer, this is the same, use the HardwareTimer librarly. This is done for the servo.

For analogWrite it is also possible to change frequency and resolution:

Code: Select all

/*
 * \brief Set the resolution of analogWrite parameters. Default is 8 bits (range from 0 to 255).
 *
 * \param res
 */
extern void analogWriteResolution(int res);

/*
 * \brief Set the frequency of analogWrite. Default is PWM_FREQUENCY (1000) in Hertz.
 *
 * \param freq
 */
extern void analogWriteFrequency(uint32_t freq);