Guilherme Santos
Member
Hi everyone, so I need help with my code. I'm using a library called 'Teensy_PWM.h'. I want to create 3 SPWM Signals with a phase shift of 120º. I'm generating the signals in Pins 7, 8, and 25 which belong to FlexPWM1.3. As Teensy 4.1 allows a 'Floating Point Unit' I'm creating the Sine Waves mathematically.
I was expecting to see a phase shift of 120º on my oscilloscope, but in reality, it looks more like 180 degrees. What I am doing wrong?
Ps: I changed the 'Teensy_PWM.h' to work with 12 bits, I have already tested it and it's working (just made a 50% Duty Cycle wave with 15 kHz). I also know that when I define the PWM Frequency in Pin 7 I also set the PWM Frequency to Pins 8 and 25, however I just came across this library and I'm not master yet.
Thank you for your help!
My code:
/*
* Test 1: 12 bits resolution
* 24 Pins (Flex and Quad Timer)
* Duty Cycle = 50%
*
* Timer Pins Total Pins
* FlexPWM1 0, 1, 7, 8, 24, 25 (6)
* FlexPWM2 4, 5, 6, 9, 33, 36, 37 (7)
* FlexPWM3 28, 29 (2)
* FlexPWM4 2, 3, 22, 23 (4)
* (19)
* QuadTimer1 10, 11, 12 (3)
* QuadTimer2 13 (1)
* QuadTimer3 14, 15, 18, 19 (4)
* (8)
*
* Total of PWM Pins is 19 + 8 = 27
*/
#define BOARD_NAME F("Teensy 4.1")
#include "Teensy_PWM.h"
#include "imxrt.h"
#include "core_pins.h"
#include <math.h>
#define USING_FLEX_TIMERS true
uint32_t PWM_Pins[] = {7, 8, 25};
#define NUM_OF_PINS (sizeof(PWM_Pins) / sizeof(uint32_t))
#define MAX_COUNT_12BIT 4095UL
#define MAX_12BIT 4095UL
float sineWave[NUM_OF_PINS] = {0};
float sineFreq = 50.0f; // Sine Wave Frequency of 50 Hz
float pwmFreq = 15000.0f; // PWM Frequency in Hz
float dutyCycle = 0.0f;
uint8_t omega = 0;
// Create PWM Instances
Teensy_PWM* PWM_Instance[NUM_OF_PINS];
void setup() {
// Create instances for each PWM pin
for (int i = 0; i < NUM_OF_PINS; i++) {
PWM_Instance = new Teensy_PWM(PWM_Pins, pwmFreq, dutyCycle);
}
}
void updateDC(){
unsigned long currentTime = millis();
for (uint8_t i = 0; i < NUM_OF_PINS; i++) {
// Calculate sine wave with a phase shift of 120 degrees for each pin
sineWave = sin(((2 * PI * sineFreq * currentTime) / 1000.0) + radians(omega));
omega = omega + 120;
if(omega == 240){
omega = 0;
}
}
for (uint8_t i = 0; i < NUM_OF_PINS; i++) {
// Calculate duty cycle for each pin
uint16_t dutyCycle = map(sineWave, -1.0, 1.0, 0, MAX_COUNT_12BIT);
// Set the PWM duty cycle for each pin
PWM_Instance->setPWM_manual(PWM_Pins, dutyCycle);
}
}
void check_status(){
#define UPDATE_INTERVAL 0.2L
static unsigned long update_timeout = UPDATE_INTERVAL;
// Update DC every UPDATE_INTERVAL (0.2) milliseconds [5000 Hz]
if (millis() > update_timeout)
{
updateDC();
update_timeout = millis() + UPDATE_INTERVAL;
}
}
void loop() {
check_status();
}
I was expecting to see a phase shift of 120º on my oscilloscope, but in reality, it looks more like 180 degrees. What I am doing wrong?
Ps: I changed the 'Teensy_PWM.h' to work with 12 bits, I have already tested it and it's working (just made a 50% Duty Cycle wave with 15 kHz). I also know that when I define the PWM Frequency in Pin 7 I also set the PWM Frequency to Pins 8 and 25, however I just came across this library and I'm not master yet.
Thank you for your help!
My code:
/*
* Test 1: 12 bits resolution
* 24 Pins (Flex and Quad Timer)
* Duty Cycle = 50%
*
* Timer Pins Total Pins
* FlexPWM1 0, 1, 7, 8, 24, 25 (6)
* FlexPWM2 4, 5, 6, 9, 33, 36, 37 (7)
* FlexPWM3 28, 29 (2)
* FlexPWM4 2, 3, 22, 23 (4)
* (19)
* QuadTimer1 10, 11, 12 (3)
* QuadTimer2 13 (1)
* QuadTimer3 14, 15, 18, 19 (4)
* (8)
*
* Total of PWM Pins is 19 + 8 = 27
*/
#define BOARD_NAME F("Teensy 4.1")
#include "Teensy_PWM.h"
#include "imxrt.h"
#include "core_pins.h"
#include <math.h>
#define USING_FLEX_TIMERS true
uint32_t PWM_Pins[] = {7, 8, 25};
#define NUM_OF_PINS (sizeof(PWM_Pins) / sizeof(uint32_t))
#define MAX_COUNT_12BIT 4095UL
#define MAX_12BIT 4095UL
float sineWave[NUM_OF_PINS] = {0};
float sineFreq = 50.0f; // Sine Wave Frequency of 50 Hz
float pwmFreq = 15000.0f; // PWM Frequency in Hz
float dutyCycle = 0.0f;
uint8_t omega = 0;
// Create PWM Instances
Teensy_PWM* PWM_Instance[NUM_OF_PINS];
void setup() {
// Create instances for each PWM pin
for (int i = 0; i < NUM_OF_PINS; i++) {
PWM_Instance = new Teensy_PWM(PWM_Pins, pwmFreq, dutyCycle);
}
}
void updateDC(){
unsigned long currentTime = millis();
for (uint8_t i = 0; i < NUM_OF_PINS; i++) {
// Calculate sine wave with a phase shift of 120 degrees for each pin
sineWave = sin(((2 * PI * sineFreq * currentTime) / 1000.0) + radians(omega));
omega = omega + 120;
if(omega == 240){
omega = 0;
}
}
for (uint8_t i = 0; i < NUM_OF_PINS; i++) {
// Calculate duty cycle for each pin
uint16_t dutyCycle = map(sineWave, -1.0, 1.0, 0, MAX_COUNT_12BIT);
// Set the PWM duty cycle for each pin
PWM_Instance->setPWM_manual(PWM_Pins, dutyCycle);
}
}
void check_status(){
#define UPDATE_INTERVAL 0.2L
static unsigned long update_timeout = UPDATE_INTERVAL;
// Update DC every UPDATE_INTERVAL (0.2) milliseconds [5000 Hz]
if (millis() > update_timeout)
{
updateDC();
update_timeout = millis() + UPDATE_INTERVAL;
}
}
void loop() {
check_status();
}