Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Unable to set duty cycle to zero #1

Closed
snagaitsev opened this issue Nov 4, 2022 · 2 comments
Closed

Unable to set duty cycle to zero #1

snagaitsev opened this issue Nov 4, 2022 · 2 comments
Labels
Support Library support

Comments

@snagaitsev
Copy link

Describe the bug

I need to set and modify 16 PWM channels in order to create 16 PID controllers for heaters. The frequency is 5 Hz and I need to set the duty cycle in 1 ms steps, 0 to 200 ms.

If I set the duty cycle to 0%, I get a pulse 1 ms long every 200 ms (which is wrong as there should be no pulse at all). For the duty cycle = 1%, I get a pulse 3 ms (again wrong, it should be 2 ms), 2% -- 5 ms, …. 99% -- 199 ms, 100% -- 200 ms

Steps to Reproduce

https://github.com/snagaitsev/PWM_slow/blob/main/slow-pwm-test.ino

Expected behavior

There should not be any pulse at 0% duty cycle.

Actual behavior

At 5 Hz (same behavior happens at other frequencies like 50 Hz):
For the duty cycle of 0%, I get a pulse 1 ms long every 200 ms (which is wrong as there should be no pulse at all). For the duty cycle = 1%, I get a pulse 3 ms (again wrong, it should be 2 ms), 2% -- 5 ms, …. 99% -- 199 ms, 100% -- 200 ms

Debug and AT-command log (if applicable)

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

Information

Please ensure to specify the following:

Arduino 1.18.19
Teensyduino 1.56
Teensy slow PWM library 1.2.1
Teensy 4.0, 600 MHz
OS: Mint 20

@khoih-prog
Copy link
Owner

Hi @snagaitsev

It seems that you didn't use the higher uS resolution (default is mS) causing the counter to be imperfect.

Try to add

#define USING_MICROS_RESOLUTION     true

before

#include "Teensy_Slow_PWM.h"

such as in your modified code

/*
  Slow PWM test
*/

#define USING_MICROS_RESOLUTION     true

#include "Teensy_Slow_PWM.h"

#define HW_TIMER_INTERVAL_MS        0.01f
#define HW_TIMER_INTERVAL_FREQ      100000L

#define PIN_COUNT 16


int ssr0 = 3;
int ssr1 = 2;
int ssr2 = 5;
int ssr3 = 4;
int ssr4 = 7;
int ssr5 = 6;
int ssr6 = 9;
int ssr7 = 8;
int ssr8 = 11;
int ssr9 = 10;
int ssr10 = 14;
int ssr11 = 12;
int ssr12 = 18;
int ssr13 = 15;
int ssr14 = 22;
int ssr15 = 19;

// these are in ssr order
int pins[] = {
  3,
  2,
  5,
  4,
  7,
  6,
  9,
  8,
  11,
  10,
  14,
  12,
  18,
  15,
  22,
  19
};

// Init Teensy timer TEENSY_TIMER_1
TeensyTimer ITimer(TEENSY_TIMER_1);
// Init Teensy_SLOW_PWM, each can service 16 different ISR-based PWM channels
Teensy_SLOW_PWM ISR_PWM;

void TimerHandler()
{
  ISR_PWM.run();
}

int channels[PIN_COUNT];

float dutyCycles[PIN_COUNT];

float freq = 50.0f;

// the setup routine runs once when you press reset:
void setup() 
{
  Serial.begin(115200);

// Must be after attachInterrupt()
#if 0
  // initialize the digital pin as an output.
  for (int i = 0; i < PIN_COUNT; i++) 
  {
    pinMode(pins[i], OUTPUT);
    dutyCycles[i] = 0.0f;
    channels[i] = ISR_PWM.setPWM(pins[i], freq, dutyCycles[i]);
  }
#endif

  if (ITimer.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler)) 
  {
    Serial.println("Timer is good");
  } 
  else 
  {
    Serial.println("Timer is bad");
  }

  // initialize the digital pin as an output.
  for (int i = 0; i < PIN_COUNT; i++) 
  {
    pinMode(pins[i], OUTPUT);
    dutyCycles[i] = 50.0f;
    channels[i] = ISR_PWM.setPWM(pins[i], freq, dutyCycles[i]);
  }

  // Display 50% for 5s
  delay(5000);

  for (int i = 0; i < PIN_COUNT; i++) 
  {
    pinMode(pins[i], OUTPUT);
    dutyCycles[i] = 0.0;
    ISR_PWM.modifyPWMChannel(channels[i], pins[i], freq, dutyCycles[i]);
  }
}

bool dutySwitch = false;

// the loop routine runs over and over again forever:
void loop() 
{
  delay(2000);
}

@khoih-prog khoih-prog added the Support Library support label Nov 4, 2022
@snagaitsev
Copy link
Author

snagaitsev commented Nov 4, 2022 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Support Library support
Projects
None yet
Development

No branches or pull requests

2 participants