STM32_Slow_PWM Library

Working libraries, libraries being ported and related hardware
Post Reply
khoih-prog
Posts: 102
Joined: Thu Feb 27, 2020 7:54 am
Location: Toronto

STM32_Slow_PWM Library

Post by khoih-prog »

STM32_Slow_PWM Library

How To Install Using Arduino Library Manager

This library enables you to use Hardware Timers on STM32F/L/H/G/WB/MP1 boards, such as NUCLEO_H743ZI2, NUCLEO_L552ZE_Q, NUCLEO_F767ZI, BLUEPILL_F103CB, etc., to create and output PWM to pins. Because this library doesn't use the powerful hardware-controlled PWM with limitations, the maximum PWM frequency is currently configurable and limited at 1000Hz, which is suitable for many real-life applications.

It now supports 16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. PWM interval can be very long (uint32_t 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.


Changelog

Initial Releases v1.0.0

1. Initial coding to support STM32F/L/H/G/WB/MP1 boards such as NUCLEO_H743ZI2, NUCLEO_L552ZE_Q, NUCLEO_F767ZI, BLUEPILL_F103CB, etc., using Arduino Core for STM32
2. The hybrid ISR-based PWM channels can generate from very low (much less than 1Hz) to highest PWM frequencies up to 1000Hz with acceptable accuracy.

Currently supported Boards

1. STM32F/L/H/G/WB/MP1 boards such as NUCLEO_H743ZI2, NUCLEO_L552ZE_Q, NUCLEO_F767ZI, BLUEPILL_F103CB, etc., using Arduino Core for STM32


1. ISR_16_PWMs_Array
2. ISR_16_PWMs_Array_Complex
3. ISR_16_PWMs_Array_Simple


Debug Terminal Output Samples

1. ISR_16_PWMs_Array_Complex on BLUEPILL_F103CB

The following is the sample terminal output when running example ISR_16_PWMs_Array_Complex 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: Select all

Starting ISR_16_PWMs_Array_Complex on BLUEPILL_F103CB
STM32_SLOW_PWM v1.0.0
[PWM] STM32TimerInterrupt: Timer Input Freq (Hz) = 72000000
[PWM] Frequency = 1000000.00 , _count = 20
Starting ITimer OK, micros() = 3390333
Channel : 0	Period : 1000000		OnTime : 50000	Start_Time : 3390427
Channel : 1	Period : 500000		OnTime : 50000	Start_Time : 3390427
Channel : 2	Period : 333333		OnTime : 66666	Start_Time : 3390427
Channel : 3	Period : 250000		OnTime : 75000	Start_Time : 3390427
Channel : 4	Period : 200000		OnTime : 80000	Start_Time : 3390427
Channel : 5	Period : 166666		OnTime : 74999	Start_Time : 3390427
Channel : 6	Period : 142857		OnTime : 71428	Start_Time : 3390427
Channel : 7	Period : 125000		OnTime : 68750	Start_Time : 3390427
Channel : 8	Period : 111111		OnTime : 66666	Start_Time : 3390427
Channel : 9	Period : 100000		OnTime : 65000	Start_Time : 3390427
Channel : 10	Period : 66666		OnTime : 46666	Start_Time : 3390427
Channel : 11	Period : 50000		OnTime : 37500	Start_Time : 3390427
Channel : 12	Period : 40000		OnTime : 32000	Start_Time : 3390427
Channel : 13	Period : 33333		OnTime : 28333	Start_Time : 3390427
Channel : 14	Period : 25000		OnTime : 22500	Start_Time : 3390427
Channel : 15	Period : 20000		OnTime : 19000	Start_Time : 3390427
SimpleTimer (ms): 2000, us : 13397013, Dus : 10006588
PWM Channel : 0, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000, actual : 500003, programmed DutyCycle : 10, actual : 10.00
PWM Channel : 2, programmed Period (us): 333333, actual : 333342, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 3, programmed Period (us): 250000, actual : 250006, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 4, programmed Period (us): 200000, actual : 199999, programmed DutyCycle : 40, actual : 40.00
PWM Channel : 5, programmed Period (us): 166666, actual : 166679, programmed DutyCycle : 45, actual : 44.99
PWM Channel : 6, programmed Period (us): 142857, actual : 142865, programmed DutyCycle : 50, actual : 49.99
PWM Channel : 7, programmed Period (us): 125000, actual : 125000, programmed DutyCycle : 55, actual : 54.99
PWM Channel : 8, programmed Period (us): 111111, actual : 111121, programmed DutyCycle : 60, actual : 59.99
PWM Channel : 9, programmed Period (us): 100000, actual : 100003, programmed DutyCycle : 65, actual : 65.00
PWM Channel : 10, programmed Period (us): 66666, actual : 66678, programmed DutyCycle : 70, actual : 69.98
PWM Channel : 11, programmed Period (us): 50000, actual : 50006, programmed DutyCycle : 75, actual : 74.99
PWM Channel : 12, programmed Period (us): 40000, actual : 40008, programmed DutyCycle : 80, actual : 79.99
PWM Channel : 13, programmed Period (us): 33333, actual : 33338, programmed DutyCycle : 85, actual : 84.95
PWM Channel : 14, programmed Period (us): 25000, actual : 25011, programmed DutyCycle : 90, actual : 89.96
PWM Channel : 15, programmed Period (us): 20000, actual : 19984, programmed DutyCycle : 95, actual : 94.91
SimpleTimer (ms): 2000, us : 23412013, Dus : 10015000
PWM Channel : 0, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000, actual : 500003, programmed DutyCycle : 10, actual : 10.00
PWM Channel : 2, programmed Period (us): 333333, actual : 333342, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 3, programmed Period (us): 250000, actual : 250005, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 4, programmed Period (us): 200000, actual : 199999, programmed DutyCycle : 40, actual : 40.00
PWM Channel : 5, programmed Period (us): 166666, actual : 166679, programmed DutyCycle : 45, actual : 44.99
PWM Channel : 6, programmed Period (us): 142857, actual : 142865, programmed DutyCycle : 50, actual : 49.99
PWM Channel : 7, programmed Period (us): 125000, actual : 125020, programmed DutyCycle : 55, actual : 54.98
PWM Channel : 8, programmed Period (us): 111111, actual : 111121, programmed DutyCycle : 60, actual : 59.99
PWM Channel : 9, programmed Period (us): 100000, actual : 100023, programmed DutyCycle : 65, actual : 64.99
PWM Channel : 10, programmed Period (us): 66666, actual : 66678, programmed DutyCycle : 70, actual : 69.98
PWM Channel : 11, programmed Period (us): 50000, actual : 50025, programmed DutyCycle : 75, actual : 74.96
PWM Channel : 12, programmed Period (us): 40000, actual : 40028, programmed DutyCycle : 80, actual : 79.90
PWM Channel : 13, programmed Period (us): 33333, actual : 33338, programmed DutyCycle : 85, actual : 84.97
PWM Channel : 14, programmed Period (us): 25000, actual : 24988, programmed DutyCycle : 90, actual : 90.03
PWM Channel : 15, programmed Period (us): 20000, actual : 19984, programmed DutyCycle : 95, actual : 95.00


2. ISR_16_PWMs_Array_Complex on NUCLEO_H743ZI2

The following is the sample terminal output when running example ISR_16_PWMs_Array_Complex 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: Select all

Starting ISR_16_PWMs_Array_Complex on NUCLEO_H743ZI2
STM32_SLOW_PWM v1.0.0
[PWM] STM32TimerInterrupt: Timer Input Freq (Hz) = 240000000
[PWM] Frequency = 1000000.00 , _count = 20
Starting ITimer OK, micros() = 2015843
Channel : 0	Period : 1000000		OnTime : 50000	Start_Time : 2019319
Channel : 1	Period : 500000		OnTime : 50000	Start_Time : 2019319
Channel : 2	Period : 333333		OnTime : 66666	Start_Time : 2019319
Channel : 3	Period : 250000		OnTime : 75000	Start_Time : 2019319
Channel : 4	Period : 200000		OnTime : 80000	Start_Time : 2019319
Channel : 5	Period : 166666		OnTime : 74999	Start_Time : 2019319
Channel : 6	Period : 142857		OnTime : 71428	Start_Time : 2019319
Channel : 7	Period : 125000		OnTime : 68750	Start_Time : 2019319
Channel : 8	Period : 111111		OnTime : 66666	Start_Time : 2019319
Channel : 9	Period : 100000		OnTime : 65000	Start_Time : 2019319
Channel : 10	Period : 66666		OnTime : 46666	Start_Time : 2019319
Channel : 11	Period : 50000		OnTime : 37500	Start_Time : 2019319
Channel : 12	Period : 40000		OnTime : 32000	Start_Time : 2019319
Channel : 13	Period : 33333		OnTime : 28333	Start_Time : 2019319
Channel : 14	Period : 25000		OnTime : 22500	Start_Time : 2019319
Channel : 15	Period : 20000		OnTime : 19000	Start_Time : 2019319
SimpleTimer (ms): 2000, us : 12111000, Dus : 10091682
PWM Channel : 0, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000, actual : 500000, programmed DutyCycle : 10, actual : 10.00
PWM Channel : 2, programmed Period (us): 333333, actual : 333340, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 3, programmed Period (us): 250000, actual : 250000, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 4, programmed Period (us): 200000, actual : 200020, programmed DutyCycle : 40, actual : 40.00
PWM Channel : 5, programmed Period (us): 166666, actual : 166680, programmed DutyCycle : 45, actual : 45.00
PWM Channel : 6, programmed Period (us): 142857, actual : 142860, programmed DutyCycle : 50, actual : 49.99
PWM Channel : 7, programmed Period (us): 125000, actual : 125020, programmed DutyCycle : 55, actual : 54.98
PWM Channel : 8, programmed Period (us): 111111, actual : 111120, programmed DutyCycle : 60, actual : 59.99
PWM Channel : 9, programmed Period (us): 100000, actual : 100020, programmed DutyCycle : 65, actual : 64.99
PWM Channel : 10, programmed Period (us): 66666, actual : 66680, programmed DutyCycle : 70, actual : 69.98
PWM Channel : 11, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 75, actual : 75.00
PWM Channel : 12, programmed Period (us): 40000, actual : 40000, programmed DutyCycle : 80, actual : 80.00
PWM Channel : 13, programmed Period (us): 33333, actual : 33340, programmed DutyCycle : 85, actual : 84.94
PWM Channel : 14, programmed Period (us): 25000, actual : 25000, programmed DutyCycle : 90, actual : 90.00
PWM Channel : 15, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 95, actual : 95.00
SimpleTimer (ms): 2000, us : 22266000, Dus : 10155000
PWM Channel : 0, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000, actual : 500000, programmed DutyCycle : 10, actual : 10.00
PWM Channel : 2, programmed Period (us): 333333, actual : 333340, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 3, programmed Period (us): 250000, actual : 250020, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 4, programmed Period (us): 200000, actual : 200020, programmed DutyCycle : 40, actual : 40.00
PWM Channel : 5, programmed Period (us): 166666, actual : 166680, programmed DutyCycle : 45, actual : 44.98
PWM Channel : 6, programmed Period (us): 142857, actual : 142860, programmed DutyCycle : 50, actual : 49.99
PWM Channel : 7, programmed Period (us): 125000, actual : 125000, programmed DutyCycle : 55, actual : 54.99
PWM Channel : 8, programmed Period (us): 111111, actual : 111120, programmed DutyCycle : 60, actual : 59.99
PWM Channel : 9, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 65, actual : 65.00
PWM Channel : 10, programmed Period (us): 66666, actual : 66680, programmed DutyCycle : 70, actual : 69.98
PWM Channel : 11, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 75, actual : 75.00
PWM Channel : 12, programmed Period (us): 40000, actual : 40000, programmed DutyCycle : 80, actual : 80.00
PWM Channel : 13, programmed Period (us): 33333, actual : 33340, programmed DutyCycle : 85, actual : 84.94
PWM Channel : 14, programmed Period (us): 25000, actual : 25020, programmed DutyCycle : 90, actual : 89.93
PWM Channel : 15, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 95, actual : 94.91
Post Reply

Return to “Libraries & Hardware”