0

I am using STM32F769 Disc board with Mbed online compiler. My task is to change the PWM frequency and duty ratio according to input.

I've created a simple algorithm according to my need, the program is working well but whenever the controller updates the PWM frequency, there is strange behavior(overlapped maybe, difficult to explain verbally for me), the frequency is changed instantly and the output is incorrect at that moment. For other controllers (like arduino) this never happens, the controller updates value after the time period of PWM is over. But not in this case. What can be wrong?

I thought to add a small delay before value is updated but that will not work, as every time a different delay would be needed. I have attached the code and screenshots.

#include "mbed.h"

AnalogIn analog_value(A0);

PwmOut pulse(D11);

int main() {

  double meas_v = 0;
  double out_freq, out_duty, s_time;
  while (1) {

    meas_v = analog_value.read() * 3300;
    if (meas_v < 1) {
      out_freq = 50000;
      out_duty = 40;
    } else if (meas_v >= 1000) {
      out_freq = 100000;
      out_duty = 80;
    } else {
      out_freq = 50000 + (meas_v * 50);
      out_duty = 40 + (meas_v * 0.04);
    }

    pulse.period(1.0 / out_freq);
    pulse = out_duty / 100;
    s_time = 0.0001;
    wait(s_time);
  }
}

The output should be updated after the current period is completed, not instantly.

Error I am getting

2 Answers 2

0

The underlying HAL code probably resets the current count value of the timer when you set the new period. You'll have to read the current timer cnt value and wait for it to reach 0. You can set a new period when the timer cnt value reaches 0.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to update the value in the update interrupt or use the timer DMA burst mode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.