Hi Clemens,
I am updating a PWM signal's period & duty-cycle every 25 ms. ... I put a scope on the PWM signal and noticed that it misses a pulse every time the period is updated, which would explain the interference observed.
This behaviour is caused by the fact that whenever the period is updated, the counters are reset. This is to avoid race condidtions (e.g. the period counter becoming smaller than the pulsewidth counter), but your use case shows this is not ideal. I'll have a think about whether this could be tackled in a better way.
Another thing I noticed, but that may be intended, is that the duty-cycle changes when you change the period, meaning that you have to update the duty-cycle when you change the period (50% at 1kHz becomes say 25% at 2 kHz) if you want it to stay the same (what I would expect).
Yes, that is the expected behaviour. The two (sane) ways of doing it are the duty-cycle stays the same, or the pulse-width stays the same. I chose the later, but I agree the former would be more attractive for your use case.
I think therefore I prefer the duty-cycle staying the same, so will look at trying a modified version of this and see if there is a way to avoid the unwanted behaviour at the same time.
Thanks for the feedback. Just what we want!
Simon
Hi,
I am updating a PWM signal's period & duty-cycle every 25 ms. The PWM signal drives a buzzer to generate sound. The problem is that the update frequency interferes with the generated sound. I put a scope on the PWM signal and noticed that it misses a pulse every time the period is updated, which would explain the interference observed. I don't see anything in the ARM datasheet that would explain such behaviour. What am I doing wrong? This simple program shows the behaviour:
#include "mbed.h"
PwmOut buzzer(p21);
int main()
{
buzzer.period_us(2000);
buzzer.write(0.5);
while (1)
{
buzzer.period_us(2000);
wait(0.025);
}
}
Another thing I noticed, but that may be intended, is that the duty-cycle changes when you change the period, meaning that you have to update the duty-cycle when you change the period (50% at 1kHz becomes say 25% at 2 kHz) if you want it to stay the same (what I would expect).
Thanks,
Clemens