Brief clarification on pwm frequency behavior for slow PWM frequency and PID

Status
Not open for further replies.

Tomek

Well-known member
Hi all, [using teensy 3.5 here]

I'm looking for input/advice about the behavior of analogwrite when I have used teensy's analogwriteFrequency() function set to a particularly low frequency.
Particularly, what happens to the signal when I update analogWrite several times a cycle. Am I interrupting timer count at all? Does the timer go to zero? I am running the standard arduino PID library and end up calling analogWrite(pin, output) quite a few times during a given pwm cycle, because my frequency is set so low. For me, it's a 1hz frequency that I want to use.
I assume that if I am at the time point in a cycle of 500/1000ms and my flag is set to 510ms, if I write analogWrite(pin,output) such that the pin should now turn off at 520/1000ms, it just turns off in 20ms, not another 520ms. Similarly, if I am at 500/1000ms and I write analogWrite(pin,output) such that the cycle should end at 490mS instead of 510mS, that the pin state simply changes when I write analogWrite(pin, output), since it is over the point it should trigger. I could imagine if the behavior doesn't follow what I've described, I could have problems by calling analogWrite so frequently within a cycle.

The reason I am using a 1hz frequency is that I selected that as "slow enough" to be useful with a zero-crossing SSR, i.e, the kind that can only turn off 60 times a second. I know I could deal with this in software instead of PWM, but feel it would be more versatile to use pwm since the writeFrequency function lets me change between fast and slow PWM for different projects.


Please let me know if I can clarify the question any better. Thanks for any advice and input!
 
https://playground.arduino.cc/Code/PIDLibraryAdvancedMethods
I just found this, and learned the compute only runs every 1 second, even if I call it more frequently. So in reality, I think that is why so far my performance seems to follow what I expect, but it could just be masking what would happen in analogwrite if I were changing the value frequently. Would still be helpful if anyone does know the answer to my question.
 
Particularly, what happens to the signal when I update analogWrite several times a cycle.

The PWM hardware has a buffer. Your writes go into the buffer, each one overwriting the prior data. At the end of the PWM cycle, the next cycle is started using whatever your last write put into the buffer. The data from all prior writes never gets used and is lost.

Freescale (and the companies making most other microcontrollers) design their PWM hardware this way so you always get the proper PWM carrier frequency. You can write new data any time, and it gets applied at the beginning of the next PWM cycle.
 
Thanks! That makes sense, and is quite practical. Very much appreciate having such an answer, and feel better knowing it is working as it should/was intended, and not just by a lucky quirk of my sample time.
 
Status
Not open for further replies.
Back
Top