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

Commit db7ab7d

Browse files
authored
v1.2.2 to replace double with float
### Releases v1.2.2 1. Use `float` for `DutyCycle` and `Freq`, `uint32_t` for `period`. 2. Optimize code by not calculation in ISR
1 parent 7698bc4 commit db7ab7d

File tree

16 files changed

+211
-192
lines changed

16 files changed

+211
-192
lines changed

README.md

Lines changed: 108 additions & 101 deletions
Large diffs are not rendered by default.

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
## Table of Contents
1313

1414
* [Changelog](#changelog)
15+
* [Releases v1.2.2](#releases-v122)
1516
* [Releases v1.2.1](#releases-v121)
1617
* [Releases v1.2.0](#releases-v120)
1718
* [Releases v1.1.0](#releases-v110)
@@ -22,6 +23,11 @@
2223

2324
## Changelog
2425

26+
### Releases v1.2.2
27+
28+
1. Use `float` for `DutyCycle` and `Freq`, `uint32_t` for `period`.
29+
2. Optimize code by not calculation in ISR
30+
2531
### Releases v1.2.1
2632

2733
1. DutyCycle to be optionally updated at the end current PWM period instead of immediately. Check [DutyCycle to be updated at the end current PWM period #2](https://github.com/khoih-prog/ESP8266_PWM/issues/2)

examples/ISR_16_PWMs_Array/ISR_16_PWMs_Array.ino

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,24 @@ uint32_t PWM_Pin[] =
107107
};
108108

109109
// You can assign any interval for any timer here, in microseconds
110-
double PWM_Period[] =
110+
uint32_t PWM_Period[] =
111111
{
112-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0,
113-
111111.111, 100000.0, 66666.667, 50000.0, 40000.0, 33333.333, 25000.0, 20000.0};
112+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000,
113+
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
114+
};
114115

115116
// You can assign any interval for any timer here, in Hz
116-
double PWM_Freq[] =
117+
float PWM_Freq[] =
117118
{
118119
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
119120
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
120121
};
121122

122123
// You can assign any interval for any timer here, in milliseconds
123-
double PWM_DutyCycle[] =
124+
float PWM_DutyCycle[] =
124125
{
125-
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0,
126-
60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0
126+
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
127+
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00
127128
};
128129

129130
typedef void (*irqCallback) ();
@@ -230,7 +231,7 @@ void setup()
230231
// You can use up to 16 timer for each ISR_PWM
231232
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
232233
{
233-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
234+
//void setPWM(uint32_t pin, float frequency, float dutycycle
234235
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
235236

236237
#if USING_PWM_FREQUENCY

examples/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,24 @@ uint32_t PWM_Pin[] =
155155
};
156156

157157
// You can assign any interval for any timer here, in microseconds
158-
double PWM_Period[] =
158+
uint32_t PWM_Period[] =
159159
{
160-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0,
161-
111111.111, 100000.0, 66666.667, 50000.0, 40000.0, 33333.333, 25000.0, 20000.0};
160+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000,
161+
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
162+
};
162163

163164
// You can assign any interval for any timer here, in Hz
164-
double PWM_Freq[] =
165+
float PWM_Freq[] =
165166
{
166167
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
167168
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
168169
};
169170

170171
// You can assign any interval for any timer here, in milliseconds
171-
double PWM_DutyCycle[] =
172+
float PWM_DutyCycle[] =
172173
{
173-
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0,
174-
60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0
174+
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
175+
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00
175176
};
176177

177178
void doingSomethingStart(int index)
@@ -391,22 +392,22 @@ void doingSomethingStop15()
391392
ISR_PWM_Data curISR_PWM_Data[] =
392393
{
393394
// pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
394-
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1000000.0, 5.0, 0, 0, 0, 0 },
395-
{ LED_BLUE, doingSomethingStart1, doingSomethingStop1, 500000.0, 10.0, 0, 0, 0, 0 },
396-
{ LED_RED, doingSomethingStart2, doingSomethingStop2, 333333.0, 20.0, 0, 0, 0, 0 },
397-
{ PIN_D0, doingSomethingStart3, doingSomethingStop3, 250000.0, 30.0, 0, 0, 0, 0 },
398-
{ PIN_D1, doingSomethingStart4, doingSomethingStop4, 200000.0, 40.0, 0, 0, 0, 0 },
399-
{ PIN_D2, doingSomethingStart5, doingSomethingStop5, 166667.0, 45.0, 0, 0, 0, 0 },
400-
{ PIN_D3, doingSomethingStart6, doingSomethingStop6, 142857.0, 50.0, 0, 0, 0, 0 },
401-
{ PIN_D4, doingSomethingStart7, doingSomethingStop7, 125000.0, 55.0, 0, 0, 0, 0 },
402-
{ PIN_D5, doingSomethingStart8, doingSomethingStop8, 111111.0, 60.0, 0, 0, 0, 0 },
403-
{ PIN_D6, doingSomethingStart9, doingSomethingStop9, 100000.0, 65.0, 0, 0, 0, 0 },
404-
{ PIN_D7, doingSomethingStart10, doingSomethingStop10, 66667.0, 70.0, 0, 0, 0, 0 },
405-
{ PIN_D8, doingSomethingStart11, doingSomethingStop11, 50000.0, 75.0, 0, 0, 0, 0 },
406-
{ PIN_D9, doingSomethingStart12, doingSomethingStop12, 40000.0, 80.0, 0, 0, 0, 0 },
407-
{ PIN_D10, doingSomethingStart13, doingSomethingStop13, 33333.0, 85.0.0, 0, 0, 0, 0 },
408-
{ PIN_D11, doingSomethingStart14, doingSomethingStop14, 25000.0, 90.0, 0, 0, 0, 0 },
409-
{ PIN_D12, doingSomethingStart15, doingSomethingStop15, 20000.0, 95.0, 0, 0, 0, 0 }
395+
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1000000, 5.0, 0, 0, 0, 0 },
396+
{ LED_BLUE, doingSomethingStart1, doingSomethingStop1, 500000, 10.0, 0, 0, 0, 0 },
397+
{ LED_RED, doingSomethingStart2, doingSomethingStop2, 333333, 20.0, 0, 0, 0, 0 },
398+
{ PIN_D0, doingSomethingStart3, doingSomethingStop3, 250000, 30.0, 0, 0, 0, 0 },
399+
{ PIN_D1, doingSomethingStart4, doingSomethingStop4, 200000, 40.0, 0, 0, 0, 0 },
400+
{ PIN_D2, doingSomethingStart5, doingSomethingStop5, 166667, 45.0, 0, 0, 0, 0 },
401+
{ PIN_D3, doingSomethingStart6, doingSomethingStop6, 142857, 50.0, 0, 0, 0, 0 },
402+
{ PIN_D4, doingSomethingStart7, doingSomethingStop7, 125000, 55.0, 0, 0, 0, 0 },
403+
{ PIN_D5, doingSomethingStart8, doingSomethingStop8, 111111, 60.0, 0, 0, 0, 0 },
404+
{ PIN_D6, doingSomethingStart9, doingSomethingStop9, 100000, 65.0, 0, 0, 0, 0 },
405+
{ PIN_D7, doingSomethingStart10, doingSomethingStop10, 66667, 70.0, 0, 0, 0, 0 },
406+
{ PIN_D8, doingSomethingStart11, doingSomethingStop11, 50000, 75.0, 0, 0, 0, 0 },
407+
{ PIN_D9, doingSomethingStart12, doingSomethingStop12, 40000, 80.0, 0, 0, 0, 0 },
408+
{ PIN_D10, doingSomethingStart13, doingSomethingStop13, 33333, 85.0.0, 0, 0, 0, 0 },
409+
{ PIN_D11, doingSomethingStart14, doingSomethingStop14, 25000, 90.0, 0, 0, 0, 0 },
410+
{ PIN_D12, doingSomethingStart15, doingSomethingStop15, 20000, 95.0, 0, 0, 0, 0 }
410411
};
411412

412413
#endif // #if USING_PWM_FREQUENCY
@@ -544,7 +545,7 @@ void setup()
544545
curISR_PWM_Data[i].previousMicrosStart = startMicros;
545546
//ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc);
546547

547-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
548+
//void setPWM(uint32_t pin, float frequency, float dutycycle
548549
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
549550

550551
#if USING_PWM_FREQUENCY

examples/ISR_16_PWMs_Array_Simple/ISR_16_PWMs_Array_Simple.ino

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,24 @@ uint32_t PWM_Pin[] =
107107
};
108108

109109
// You can assign any interval for any timer here, in microseconds
110-
double PWM_Period[] =
110+
uint32_t PWM_Period[] =
111111
{
112-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0,
113-
111111.111, 100000.0, 66666.667, 50000.0, 40000.0, 33333.333, 25000.0, 20000.0};
112+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000,
113+
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
114+
};
114115

115116
// You can assign any interval for any timer here, in Hz
116-
double PWM_Freq[] =
117+
float PWM_Freq[] =
117118
{
118119
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
119120
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
120121
};
121122

122123
// You can assign any interval for any timer here, in milliseconds
123-
double PWM_DutyCycle[] =
124+
float PWM_DutyCycle[] =
124125
{
125-
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0,
126-
60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0
126+
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
127+
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00
127128
};
128129

129130

@@ -152,7 +153,7 @@ void setup()
152153
// You can use up to 16 timer for each ISR_PWM
153154
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
154155
{
155-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
156+
//void setPWM(uint32_t pin, float frequency, float dutycycle
156157
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
157158

158159
#if USING_PWM_FREQUENCY

examples/ISR_Changing_PWM/ISR_Changing_PWM.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ void TimerHandler()
8686
uint32_t PWM_Pin = LED_BUILTIN;
8787

8888
// You can assign any interval for any timer here, in Hz
89-
double PWM_Freq1 = 1.0f;
89+
float PWM_Freq1 = 1.0f;
9090
// You can assign any interval for any timer here, in Hz
91-
double PWM_Freq2 = 2.0f;
91+
float PWM_Freq2 = 2.0f;
9292

9393
// You can assign any interval for any timer here, in microseconds
94-
double PWM_Period1 = 1000000.0 / PWM_Freq1;
94+
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
9595
// You can assign any interval for any timer here, in microseconds
96-
double PWM_Period2 = 1000000.0 / PWM_Freq2;
96+
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
9797

9898
// You can assign any duty_cycle for any PWM here, from 0-100
99-
double PWM_DutyCycle1 = 10.0;
99+
float PWM_DutyCycle1 = 50.0;
100100
// You can assign any duty_cycle for any PWM here, from 0-100
101-
double PWM_DutyCycle2 = 90.0;
101+
float PWM_DutyCycle2 = 90.0;
102102

103103
// Channel number used to identify associated channel
104104
int channelNum;

examples/ISR_Modify_PWM/ISR_Modify_PWM.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ void TimerHandler()
8686
uint32_t PWM_Pin = LED_BUILTIN;
8787

8888
// You can assign any interval for any timer here, in Hz
89-
double PWM_Freq1 = 1.0f;
89+
float PWM_Freq1 = 1.0f;
9090
// You can assign any interval for any timer here, in Hz
91-
double PWM_Freq2 = 2.0f;
91+
float PWM_Freq2 = 2.0f;
9292

9393
// You can assign any interval for any timer here, in microseconds
94-
double PWM_Period1 = 1000000.0 / PWM_Freq1;
94+
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
9595
// You can assign any interval for any timer here, in microseconds
96-
double PWM_Period2 = 1000000.0 / PWM_Freq2;
96+
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
9797

9898
// You can assign any duty_cycle for any PWM here, from 0-100
99-
double PWM_DutyCycle1 = 10.0;
99+
float PWM_DutyCycle1 = 50.0;
100100
// You can assign any duty_cycle for any PWM here, from 0-100
101-
double PWM_DutyCycle2 = 90.0;
101+
float PWM_DutyCycle2 = 90.0;
102102

103103
// Channel number used to identify associated channel
104104
int channelNum;

examples/multiFileProject/multiFileProject.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting.
1717
#endif
1818

19-
#define STM32_SLOW_PWM_VERSION_MIN_TARGET "STM32_SLOW_PWM v1.2.0"
20-
#define STM32_SLOW_PWM_VERSION_MIN 1002000
19+
#define STM32_SLOW_PWM_VERSION_MIN_TARGET "STM32_SLOW_PWM v1.2.2"
20+
#define STM32_SLOW_PWM_VERSION_MIN 1002002
2121

2222
#include "multiFileProject.h"
2323

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "STM32_Slow_PWM",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"keywords": "timing, device, control, timer, pwm, interrupt, isr, isr-based, hardware-timer, mission-critical, accuracy, non-blocking, stm32, stm32h7, stm32l5, stm32f1, stm32f4, stm32f7, stm32g4, precise, hardware",
55
"description": "This library enables you to use Hardware Timers on STM32F/L/H/G/WB/MP1 boards to create and output PWM to pins. The most important feature is they're purely hardware-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 timers, 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 timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy. PWM feature can now be used. Max PWM frequency is limited at 1000Hz. Now you can change the PWM settings on-the-fly",
66
"authors":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=STM32_Slow_PWM
2-
version=1.2.1
2+
version=1.2.2
33
author=Khoi Hoang <khoih.prog@gmail.com>
44
maintainer=Khoi Hoang <khoih.prog@gmail.com>
55
sentence=This library enables you to use Hardware Timers on STM32F/L/H/G/WB/MP1 boards to create and output PWM to pins.

0 commit comments

Comments
 (0)