This repository was archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Unable to set duty cycle to zero #1
Labels
Support
Library support
Comments
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);
} |
Thanks!
It worked.
…On Fri, Nov 4, 2022 at 1:13 PM Khoi Hoang ***@***.***> wrote:
Hi @snagaitsev <https://github.com/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 orderint 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);
}
—
Reply to this email directly, view it on GitHub
<#1 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKJ6SO4JIRBXCKFHJN2H6WDWGVG5VANCNFSM6AAAAAARXNCUUI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
The text was updated successfully, but these errors were encountered: