Strange initial PWM response

Status
Not open for further replies.

Jezza

New member
Hi all,

I am running a PWM at 50/255 with default settings on a Teensy 4.1, using the simple Arduino code below:
Code:
int PWM_Pin = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(PWM_Pin, OUTPUT);
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
 analogWrite(PWM_Pin, 50);
}

However, I noticed that with a single capture on the oscilloscope, that the first pulse varies quite drastically, usually it is as wide as shown in the image below, but sometimes, it can be narrower than the PWM pulses that comes afterwards. I can only see this happening on the very first pulse and none later. Does anyone know what could be causing this and how I would go about fixing this?
Oscilloscope Screenshot.jpg

The oscilloscope probe is connected to the Teensy by 2x 20cm jumper leads going to pin 0 and the ground pin right next to pin 0. The Teensy is being powered via USB, CPU speed is 600MHz.

Thanks!
 
It will depend when the PWM is enabled w.r.t. the clock driving the PWM unit I think - the way PWM units work is that the output
change is triggered by exact matches of the counter register to the PWM value register - if you miss the match it can take a whole
cycle to synchronize. There's probably a way to set the counter to prevent this, but the generic analogWrite can't use this as
many pins may be sharing the same counter.
 
Hi Mark,
Thanks for your reply, I suspected it would have something about setting the counter.
Time to learn the embedded stuff!
 
Status
Not open for further replies.
Back
Top