Changing PWM frequency, Teensy_PWM library

markkimball

Active member
I've been using the Teensy_PWM library for running an ADC and encountered something that puzzles me. I'm using the library to generate a conversion signal that triggers the ADC, and that works fine -- with the initially-established sampling rate. But I've found it useful to change the sampling rate on the fly, and that's where the puzzle arises. When I attempt to change the PWM frequency I get an error (and a failure to change the frequency), reported as:

[PWM] setupPWM: FlexTimer1 already used PWM Frequency = 360000

This is where I try to change the sampling rate from 360000 samples/second to anything else.

I have the source code for the library so I can easily change it: but I'm wondering WHY an attempt to change the PWM frequency would be rejected as an error. I'm using a Teensy4.0 (although the T4.1 generates the same error).

Here's my test code:

Code:
#include <Teensy_PWM.h>

Teensy_PWM* PWM_Instance;

void setup() {
Serial.begin(9600);
  // put your setup code here, to run once:
  PWM_Instance = new Teensy_PWM(0, 360000, 95.0); // start PWM timer on D0, with freq = 360000 and a "throwaway" duty factor.
  PWM_Instance = new Teensy_PWM(0,500000, 95.5 ); // change PWM frequency.
 }

void loop() {
}
 
I think it's because your code is not changing the setting of the original instance. It's trying to create a new instance on the same pin, and the error is telling you the pin is already in use. Try using the setPWM function.
 
Back
Top