Teensy 3.2 PWM pins and IntervalTimer Issues

George1

Active member
I am testing some code with Teeensy 3.2 and using PWM function on pins 3,4,5,6,9. I’d like to update PWM duty cycle inside an IntervalTimer function based on some ADC value readings. The PWM frequency is chosen based on the table on https://www.pjrc.com/teensy/td_pulse.html. The pin 5 seem to jump to 100% duty cyle and it does not change when analogWrite is used to set it to other/lower value.
I wonder if there is any timer conflict having to do with the pin selection since I know that different hardware timers are used for different PWM pins.
I can also post code if that is helpful

Thanks a lot for your help and any advice that might be helpful,
George
 
You really should use the interrupt from the same timer that's generating the PWM.

And yeah, if you want help with a specific problem, posting complete code to reproduce the problem is the way to go. Best if you can find a way to make the problem happen without connecting an analog signal. Maybe a combination of delay and just writing a fixed number can take the place of analogRead? If anyone can reproduce the problem by just running the code on a Teensy without connecting other hardware, odds are much better of getting to the bottom of whatever's wrong.
 
Thanks for the reply Paul.

Below is the gist of my code. Is there something obviously wrong with the code that should interfere with the PWM operation.

Thanks

Code:
IntervalTimer pwmTimer;

//pwm settings Teensy 3.2 frequency = 48MHz
const int pwmFrequency = 187500; //see teensy table https://www.pjrc.com/teensy/td_pulse.html
const int pwmResolution = 8;
const int pwmPins[] = { 3, 4, 5, 6, 9 };
int pwmLevel = 0;
const int pwmMax = (2 << (pwmResolution - 1)) - 1;

int counter = 0;

//----------------------------------------------------------
//setup
void setup() {
  //com
  Serial.begin(9600);

  //pwm port setting
  analogWriteResolution(pwmResolution);
  for (int i = 0; i < 5; i++) {
    pinMode(pwmPins[i], OUTPUT); //set to ouput
    analogWriteFrequency(pwmPins[i], pwmFrequency); //set pin freq
    analogWrite(pwmPins[i], 0); // set 0 pwm to start
  }

  //timer setup
  pwmTimer.begin(pwmUpdate, 200);

}
//end setup



//main
void loop() {

}
//end main


//IntervalTimer
void pwmUpdate() {

  if ((counter % 500) == 0) { //update every 500 cycles or .1  sec)

    for (int i = 0; i < 5; i++) {
      analogWrite(pwmPins[i], pwmLevel); //set same pwm to all pwm pins
    }

    pwmLevel++; // increase pwm

    if (pwmLevel > pwmMax) {
      pwmLevel = 0; // zero pwm when reaches max limit
    }
    Serial.println(pwmLevel);
  }

  counter++; //icrement timer




}
//end timer
 
Back
Top