New Teensy_Slow_PWM Library

khoih-prog

Well-known member
Teensy_Slow_PWM Library

Features

This library enables you to use ISR-based PWM channels on Teensy boards, such as Teensy 2.x, Teensy LC, Teensy 3.x, Teensy 4.x, Teensy MicroMod, etc., etc. using Teensyduno core to create and output PWM any GPIO pin. Because this library doesn't use the powerful purely hardware-controlled PWM with many limitations, the maximum PWM frequency is currently limited at 500-1000Hz, which is still suitable for many real-life applications.


This library enables you to use Interrupt from Hardware Timers on Teensy boards to create and output PWM to pins. It now supports 16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. PWM interval can be very long (uint64_t microsecs / millisecs). The most important feature is they're ISR-based PWM channels. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware PWM channels, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software PWM using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.

Debug Terminal Output Samples

1. ISR_8_PWMs_Array_Complex on Teensy 4.1

The following is the sample terminal output when running example ISR_8_PWMs_Array_Complex on Teensy 4.1 to demonstrate how to use multiple PWM channels with complex callback functions, the accuracy of ISR Hardware PWM-channels, especially when system is very busy. The ISR PWM-channels is running exactly according to corresponding programmed periods and duty-cycles


Code:
Starting ISR_8_PWMs_Array_Complex on Teensy 4.1
Teensy_Slow_PWM v1.0.0
CPU Frequency = 600 MHz
[PWM] TEENSY_TIMER_1: , F_BUS_ACTUAL (MHz) = 150
[PWM] Request interval = 20 , actual interval (us) = 20
[PWM] Prescale = 0 , _timerCount = 1500
Starting  ITimer OK, micros() = 2968006
Channel : 0	Period : 1000000		OnTime : 50000	Start_Time : 2968007
Channel : 1	Period : 500000		OnTime : 50000	Start_Time : 2968007
Channel : 2	Period : 333333		OnTime : 66666	Start_Time : 2968007
Channel : 3	Period : 250000		OnTime : 62500	Start_Time : 2968007
Channel : 4	Period : 200000		OnTime : 60000	Start_Time : 2968007
Channel : 5	Period : 166666		OnTime : 58333	Start_Time : 2968007
Channel : 6	Period : 142857		OnTime : 57142	Start_Time : 2968007
Channel : 7	Period : 125000		OnTime : 56250	Start_Time : 2968007
SimpleTimer (us): 2000, us : 12968028, Dus : 10000021
PWM Channel : 0, prog Period (ms): 1000.00, actual : 1000014, prog DutyCycle : 5, actual : 5.00
PWM Channel : 1, prog Period (ms): 500.00, actual : 500006, prog DutyCycle : 10, actual : 10.00
PWM Channel : 2, prog Period (ms): 333.33, actual : 333351, prog DutyCycle : 20, actual : 20.00
PWM Channel : 3, prog Period (ms): 250.00, actual : 250003, prog DutyCycle : 25, actual : 25.00
PWM Channel : 4, prog Period (ms): 200.00, actual : 200006, prog DutyCycle : 30, actual : 30.00
PWM Channel : 5, prog Period (ms): 166.67, actual : 166676, prog DutyCycle : 35, actual : 34.99
PWM Channel : 6, prog Period (ms): 142.86, actual : 142868, prog DutyCycle : 40, actual : 39.99
PWM Channel : 7, prog Period (ms): 125.00, actual : 125002, prog DutyCycle : 45, actual : 44.99
SimpleTimer (us): 2000, us : 22968058, Dus : 10000030
PWM Channel : 0, prog Period (ms): 1000.00, actual : 1000013, prog DutyCycle : 5, actual : 5.00
PWM Channel : 1, prog Period (ms): 500.00, actual : 500006, prog DutyCycle : 10, actual : 10.00
PWM Channel : 2, prog Period (ms): 333.33, actual : 333351, prog DutyCycle : 20, actual : 20.00
PWM Channel : 3, prog Period (ms): 250.00, actual : 250003, prog DutyCycle : 25, actual : 24.99
PWM Channel : 4, prog Period (ms): 200.00, actual : 200007, prog DutyCycle : 30, actual : 30.00
PWM Channel : 5, prog Period (ms): 166.67, actual : 166675, prog DutyCycle : 35, actual : 34.99
PWM Channel : 6, prog Period (ms): 142.86, actual : 142867, prog DutyCycle : 40, actual : 39.99
PWM Channel : 7, prog Period (ms): 125.00, actual : 125002, prog DutyCycle : 45, actual : 44.99
SimpleTimer (us): 2000, us : 32968087, Dus : 10000029
PWM Channel : 0, prog Period (ms): 1000.00, actual : 1000013, prog DutyCycle : 5, actual : 5.00
PWM Channel : 1, prog Period (ms): 500.00, actual : 500007, prog DutyCycle : 10, actual : 10.00
PWM Channel : 2, prog Period (ms): 333.33, actual : 333351, prog DutyCycle : 20, actual : 20.00
PWM Channel : 3, prog Period (ms): 250.00, actual : 250003, prog DutyCycle : 25, actual : 24.99
PWM Channel : 4, prog Period (ms): 200.00, actual : 200007, prog DutyCycle : 30, actual : 30.00
PWM Channel : 5, prog Period (ms): 166.67, actual : 166676, prog DutyCycle : 35, actual : 34.99
PWM Channel : 6, prog Period (ms): 142.86, actual : 142868, prog DutyCycle : 40, actual : 39.99
PWM Channel : 7, prog Period (ms): 125.00, actual : 125001, prog DutyCycle : 45, actual : 44.99
 
Back
Top