Teensy 3.1 AnalogWrite problem with slow PWM frequency

Barney

Active member
Dear Paul,

please test this Code and use an Ozilloscope. You can see the PWM doesnt increased the value straight. The PWM hops back and forward in little steps. Is there an Teensy register bug?

I have tested different resolution but with the same effect.

Barney


Code:
#define AnalogWriteResolutionBits 16   // Festlegen der Aufloesung in Bit fuer die Digital Analog Umsetzer, hier die PWM Auflösung
#define AnalogWriteResolutionValue (uint16_t) pow(2,AnalogWriteResolutionBits) // Festlegen der Aufloesung (Wertebereich) fuer die Analog Digital Umsetzer
#define PWMFrequenz 50                 // Festlegen der PWM Frequenz für die Ansteuerung des Motorstellers. Ueblicherweise alle 20ms ein Impuls
#define UntererStellWertMotor  (uint16_t) float(1*AnalogWriteResolutionValue*PWMFrequenz/1000)   // Wert berechnen für 1ms Pulsweite
#define LeerlaufStellWertMotor (uint16_t) float(1.5*AnalogWriteResolutionValue*PWMFrequenz/1000) // Wert berechnen für 1.5ms Pulsweite
#define ObererStellWertMotor   (uint16_t) float(2*AnalogWriteResolutionValue*PWMFrequenz/1000)   // Wert berechnen für 2ms Pulsweite

#define Pin_Motorstelleranschluss A9 // Pin A9 Motorsteller PWM-Anschluss

uint16_t pos = LeerlaufStellWertMotor;    // variable to store the servo position 

void setup() 
{ 
  // Die Motorsteller benoetigen ein PWM Signal mit einer Impulsweite von
  // 1ms bis 2ms alle 20ms. Diese PWM wird ueber den 16 Bit Timer in Hardware realisiert
  analogWriteFrequency(Pin_Motorstelleranschluss, PWMFrequenz);   // Einstellen der PWM Parameter fuer den Motorsteller
  analogWriteResolution(AnalogWriteResolutionBits);               // analogWrite Aufloesung
  analogWrite(Pin_Motorstelleranschluss, LeerlaufStellWertMotor); // Geschwindigkeitsvorgabe Motor
  delay(2000);  
  // Ende PWM
} 

void loop() 
{ 
  for(pos = UntererStellWertMotor; pos < ObererStellWertMotor; pos += 1)
  {  // von 1ms to 2ms
    analogWrite(Pin_Motorstelleranschluss, pos); // Geschwindigkeitsvorgabe Motor
    delay(1);                                    // waits 15ms for the servo to reach the position 
  } 
  for(pos = ObererStellWertMotor; pos >= UntererStellWertMotor; pos -=1)     // von 2ms to 1ms 
  {                                
    analogWrite(Pin_Motorstelleranschluss, pos); // Geschwindigkeitsvorgabe Motor
    delay(1);                                    // waits 15ms for the servo to reach the position 
  } 
}
 
du schreibst abwechselnd alle 1/1000 Sekunden.. was erwartest du zu sehen ?
 
Last edited:
The problem is that you're trying to change your output value every millisecond, but the FTM can only update once during each period (20ms). What you are seeing is that the FTM will only use the last value you write before it starts another period, so that the PWM output will look like 1, 21, 41, etc, instead of 1,2,3,...

If you change your delays to 20ms instead of 1ms, you should see the behavior you expect.
 
The problem is that you're trying to change your output value every millisecond, but the FTM can only update once during each period (20ms). What you are seeing is that the FTM will only use the last value you write before it starts another period, so that the PWM output will look like 1, 21, 41, etc, instead of 1,2,3,...

If you change your delays to 20ms instead of 1ms, you should see the behavior you expect.

Hi Whollender,

i don't see 1,21,41 i see instead 1,30,20,50,30,... next day i will test your suggestion with longer delay.
 
Code:
void loop() 
{ 
  for(pos = UntererStellWertMotor; pos < ObererStellWertMotor; pos += 10)
  {  // von 1ms to 2ms
    analogWrite(Pin_Motorstelleranschluss, pos); // Geschwindigkeitsvorgabe Motor
    delay(100);                                    // waits 15ms for the servo to reach the position 
  } 
  for(pos = ObererStellWertMotor; pos >= UntererStellWertMotor; pos -=10)     // von 2ms to 1ms 
  {                                
    analogWrite(Pin_Motorstelleranschluss, pos); // Geschwindigkeitsvorgabe Motor
    delay(100);                                    // waits 15ms for the servo to reach the position 
  } 
}

The problem exists!
Das Problem bleibt!

Despite the slow change of the value of the PWM signal bounces back and forth.
Trotz der langsamen Veränderung des Wertes springt das PWM-Signal hin und her.

I suspect a problem with the transfer of values ​​in the DAC.
Ich vermute ein Problem der Werteübernahme in den DAC.
 
Thank you to all involved,
it was a problem from the pocket oscilloscope UNI-T UT81B. After Paul has created the video, I used my second oscilloscope.
There is no jumping of the signal! Everything is fine. Except for my pocket oscilloscope ...

Thank you

Barney
 
Back
Top