PWM updates

Status
Not open for further replies.

Raptor

Active member
I'm curious about the way the Teensy 3.6 handles a change to an outputs PWM value. Specifically, if the program decides the PWM value needs to change and it writes that new value is the current PWM cycle interrupted and the new value started immediately or does it allow the current PWM cycle to complete before the new value takes effect? If it interrupts the current cycle there would tend to be an effect that would tend to cause the net/average PWM value to be a bit higher than expected as follows:

Assume a clock tick equals one PWM count and that the current PWM cycle with a PWM value of 100 begins a count 0. If allowed to proceed to the completion of the cycle the bit would be ON for 100 ticks then OFF for 155 ticks, but if interrupted at a clock value of, say, 100, with a new PWM value of, say, 110, then we could get in effect a single cycle lasting not 255 ticks but 355 ticks with an ON time of 210 equating to the equivalent duty-cycle of 150 for a 255 tick cycle. The average for the two cycle listed above would be 105 so 150 would be much higher.

If, OTH, the PWM cycle is not interrupted and the new value is held until the current cycle ends then the average would work out though there would be a small effect in the timing.

Trying to catch this on a scope is, well, not very easy...


Brian
 
I'm pretty sure duty cycle changes happen only after cycle-completion, unless 0 or max is specified, those two values use digitalWrite() and they might ? happen mid-cycle. here is test sketch with scope on PWM pin 23 and pin 13
Code:
void setup() {
  // put your setup code here, to run once:
 analogWriteFrequency(23,10);
 analogWrite(23,127);
 pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(400);
  digitalWrite(13,HIGH);
  analogWrite(23,64);
  delay(200);
  digitalWrite(13,LOW);
  analogWrite(23,192);
}
and scope
pwm.png
yellow is pwm, and blue (pin 13) marks when duty cycle is being changed

also check T3.6 reference manual and hardware/teensy/avr/cores/teensy3/pins_teensy.c
 
Status
Not open for further replies.
Back
Top