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

Commit 42d95b1

Browse files
authored
v1.2.3 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 7b4b1fb commit 42d95b1

File tree

17 files changed

+122
-131
lines changed

17 files changed

+122
-131
lines changed

README.md

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ void setup()
320320
#define USING_MICROS_RESOLUTION true //false
321321
322322
// Default is true, uncomment to false
323-
//#define CHANGING_PWM_END_OF_CYCLE false
323+
//#define CHANGING_PWM_END_OF_CYCLE false
324324
325325
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
326326
#include "AVR_Slow_PWM.h"
@@ -394,9 +394,9 @@ typedef struct
394394
irqCallback irqCallbackStartFunc;
395395
irqCallback irqCallbackStopFunc;
396396
397-
double PWM_Freq;
397+
float PWM_Freq;
398398
399-
double PWM_DutyCycle;
399+
float PWM_DutyCycle;
400400
401401
uint32_t deltaMicrosStart;
402402
uint32_t previousMicrosStart;
@@ -424,19 +424,19 @@ volatile unsigned long previousMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
424424
425425
426426
// You can assign any interval for any timer here, in Microseconds
427-
double PWM_Period[] =
427+
uint32_t PWM_Period[] =
428428
{
429-
1000.0, 500.0, 333.333, 250.0, 200.0, 166.667, 142.857, 125.0
429+
1000, 500, 333, 250, 200, 167, 143, 125
430430
};
431431
432432
// You can assign any interval for any timer here, in Hz
433-
double PWM_Freq[] =
433+
float PWM_Freq[] =
434434
{
435435
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
436436
};
437437
438438
// You can assign any interval for any timer here, in Microseconds
439-
double PWM_DutyCycle[] =
439+
float PWM_DutyCycle[] =
440440
{
441441
5.0, 10.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0
442442
};
@@ -746,7 +746,7 @@ void setup()
746746
curISR_PWM_Data[i].previousMicrosStart = startMicros;
747747
//ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc);
748748
749-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
749+
//void setPWM(uint32_t pin, float frequency, float dutycycle
750750
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
751751
752752
// You can use this with PWM_Freq in Hz
@@ -793,7 +793,7 @@ The following is the sample terminal output when running example [ISR_8_PWMs_Arr
793793

794794
```
795795
Starting ISR_8_PWMs_Array_Complex on Arduino AVR ATMega32U4
796-
AVR_Slow_PWM v1.2.1
796+
AVR_Slow_PWM v1.2.2
797797
CPU Frequency = 16 MHz
798798
[PWM] T3
799799
[PWM] Freq * 1000 = 10000000.00
@@ -837,42 +837,24 @@ PWM Channel : 7, prog Period (ms): 125.00, actual (uS) : 125192, prog DutyCycle
837837
The following is the sample terminal output when running example [**ISR_8_PWMs_Array**](examples/ISR_8_PWMs_Array) on **AVR Mega2560/ADK** to demonstrate how to use multiple PWM channels with simple callback functions.
838838

839839
```
840-
Starting ISR_8_PWMs_Array_Complex on Arduino AVR Mega2560/ADK
841-
AVR_Slow_PWM v1.2.1
840+
Starting ISR_8_PWMs_Array on Arduino AVR Mega2560/ADK
841+
AVR_Slow_PWM v1.2.2
842842
CPU Frequency = 16 MHz
843843
[PWM] T3
844844
[PWM] Freq * 1000 = 10000000.00
845845
[PWM] F_CPU = 16000000 , preScalerDiv = 1
846846
[PWM] OCR = 1599 , preScalerIndex = 1
847847
[PWM] OK in loop => _OCR = 1599
848848
[PWM] _preScalerIndex = 1 , preScalerDiv = 1
849-
Starting ITimer3 OK, micros() = 2024104
850-
Channel : 0 Period : 1000000 OnTime : 50000 Start_Time : 2024988
851-
Channel : 1 Period : 500000 OnTime : 50000 Start_Time : 2024988
852-
Channel : 2 Period : 333333 OnTime : 66666 Start_Time : 2024988
853-
Channel : 3 Period : 250000 OnTime : 62500 Start_Time : 2024988
854-
Channel : 4 Period : 200000 OnTime : 60000 Start_Time : 2024988
855-
Channel : 5 Period : 166666 OnTime : 58333 Start_Time : 2024988
856-
Channel : 6 Period : 142857 OnTime : 57142 Start_Time : 2024988
857-
Channel : 7 Period : 125000 OnTime : 56250 Start_Time : 2024988
858-
SimpleTimer (us): 2000, us : 12070388, Dus : 10045444
859-
PWM Channel : 0, prog Period (ms): 1000.00, actual (uS) : 1000000, prog DutyCycle : 5, actual : 4.98
860-
PWM Channel : 1, prog Period (ms): 500.00, actual (uS) : 499996, prog DutyCycle : 10, actual : 10.00
861-
PWM Channel : 2, prog Period (ms): 333.33, actual (uS) : 333396, prog DutyCycle : 20, actual : 19.98
862-
PWM Channel : 3, prog Period (ms): 250.00, actual (uS) : 250196, prog DutyCycle : 25, actual : 24.94
863-
PWM Channel : 4, prog Period (ms): 200.00, actual (uS) : 200192, prog DutyCycle : 30, actual : 29.88
864-
PWM Channel : 5, prog Period (ms): 166.67, actual (uS) : 166792, prog DutyCycle : 35, actual : 34.90
865-
PWM Channel : 6, prog Period (ms): 142.86, actual (uS) : 142988, prog DutyCycle : 40, actual : 39.87
866-
PWM Channel : 7, prog Period (ms): 125.00, actual (uS) : 125196, prog DutyCycle : 45, actual : 44.89
867-
SimpleTimer (us): 2000, us : 22144772, Dus : 10074384
868-
PWM Channel : 0, prog Period (ms): 1000.00, actual (uS) : 1000000, prog DutyCycle : 5, actual : 5.00
869-
PWM Channel : 1, prog Period (ms): 500.00, actual (uS) : 499996, prog DutyCycle : 10, actual : 10.00
870-
PWM Channel : 2, prog Period (ms): 333.33, actual (uS) : 333396, prog DutyCycle : 20, actual : 19.98
871-
PWM Channel : 3, prog Period (ms): 250.00, actual (uS) : 250196, prog DutyCycle : 25, actual : 24.94
872-
PWM Channel : 4, prog Period (ms): 200.00, actual (uS) : 200196, prog DutyCycle : 30, actual : 29.87
873-
PWM Channel : 5, prog Period (ms): 166.67, actual (uS) : 166792, prog DutyCycle : 35, actual : 34.90
874-
PWM Channel : 6, prog Period (ms): 142.86, actual (uS) : 143016, prog DutyCycle : 40, actual : 39.87
875-
PWM Channel : 7, prog Period (ms): 125.00, actual (uS) : 125008, prog DutyCycle : 45, actual : 44.96
849+
Starting ITimer3 OK, micros() = 2023732
850+
Channel : 0 Period : 1000000 OnTime : 50000 Start_Time : 2031420
851+
Channel : 1 Period : 500000 OnTime : 50000 Start_Time : 2042728
852+
Channel : 2 Period : 333333 OnTime : 66666 Start_Time : 2054724
853+
Channel : 3 Period : 250000 OnTime : 62500 Start_Time : 2067932
854+
Channel : 4 Period : 200000 OnTime : 60000 Start_Time : 2084232
855+
Channel : 5 Period : 166666 OnTime : 58333 Start_Time : 2115432
856+
Channel : 6 Period : 142857 OnTime : 57142 Start_Time : 3075536
857+
Channel : 7 Period : 125000 OnTime : 56250 Start_Time : 4221280
876858
```
877859

878860
---
@@ -883,7 +865,7 @@ The following is the sample terminal output when running example [**ISR_8_PWMs_A
883865

884866
```
885867
Starting ISR_8_PWMs_Array_Complex on Arduino AVR UNO, Nano, etc.
886-
AVR_Slow_PWM v1.2.1
868+
AVR_Slow_PWM v1.2.2
887869
CPU Frequency = 16 MHz
888870
[PWM] T1
889871
[PWM] Freq * 1000 = 10000000.00
@@ -929,20 +911,16 @@ The following is the sample terminal output when running example [ISR_Modify_PWM
929911

930912
```
931913
Starting ISR_Modify_PWM on Arduino AVR Mega2560/ADK
932-
AVR_Slow_PWM v1.2.1
914+
AVR_Slow_PWM v1.2.2
933915
CPU Frequency = 16 MHz
934916
[PWM] T3
935917
[PWM] Freq * 1000 = 10000000.00
936918
[PWM] F_CPU = 16000000 , preScalerDiv = 1
937919
[PWM] OCR = 1599 , preScalerIndex = 1
938920
[PWM] OK in loop => _OCR = 1599
939921
[PWM] _preScalerIndex = 1 , preScalerDiv = 1
940-
Starting ITimer3 OK, micros() = 2023160
941-
Using PWM Freq = 1.00, PWM DutyCycle = 10
942-
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 2028040
943-
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 12033220
944-
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 22034628
945-
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 32036036
922+
Starting ITimer3 OK, micros() = 2023764
923+
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
946924
```
947925

948926
---
@@ -953,23 +931,21 @@ The following is the sample terminal output when running example [ISR_Changing_P
953931

954932
```
955933
Starting ISR_Changing_PWM on Arduino AVR Mega2560/ADK
956-
AVR_Slow_PWM v1.2.1
934+
AVR_Slow_PWM v1.2.2
957935
CPU Frequency = 16 MHz
958936
[PWM] T3
959937
[PWM] Freq * 1000 = 10000000.00
960938
[PWM] F_CPU = 16000000 , preScalerDiv = 1
961939
[PWM] OCR = 1599 , preScalerIndex = 1
962940
[PWM] OK in loop => _OCR = 1599
963941
[PWM] _preScalerIndex = 1 , preScalerDiv = 1
964-
Starting ITimer3 OK, micros() = 2023336
965-
Using PWM Freq = 1.00, PWM DutyCycle = 50
966-
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 2028216
967-
Using PWM Freq = 2.00, PWM DutyCycle = 90
968-
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 12035408
969-
Using PWM Freq = 1.00, PWM DutyCycle = 50
970-
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 22040404
971-
Using PWM Freq = 2.00, PWM DutyCycle = 90
972-
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 32045592
942+
Starting ITimer3 OK, micros() = 2023844
943+
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
944+
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
945+
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
946+
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
947+
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
948+
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
973949
```
974950

975951
---
@@ -1018,7 +994,7 @@ Submit issues to: [AVR_Slow_PWM issues](https://github.com/khoih-prog/AVR_Slow_P
1018994
3. Add functions to modify PWM settings on-the-fly
1019995
4. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
1020996
5. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
1021-
6. Improve accuracy by using `double`, instead of `uint32_t` for `dutycycle`, `period`
997+
6. Improve accuracy by using `float`, instead of `uint32_t` for `dutycycle`
1022998
7. Optimize library code by using `reference-passing` instead of `value-passing`
1023999
8. DutyCycle to be optionally updated at the end current PWM period instead of immediately.
10241000

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_8_PWMs_Array/ISR_8_PWMs_Array.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ uint32_t PWM_Pin[] =
101101
#define NUMBER_ISR_PWMS ( sizeof(PWM_Pin) / sizeof(uint32_t) )
102102

103103
// You can assign any interval for any timer here, in Hz
104-
double PWM_Freq[] =
104+
float PWM_Freq[] =
105105
{
106106
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
107107
};
108108

109109
// You can assign any interval for any timer here, in Microseconds
110-
double PWM_DutyCycle[] =
110+
float PWM_DutyCycle[] =
111111
{
112112
5.0, 10.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0
113113
};
@@ -238,7 +238,7 @@ void setup()
238238
// You can use up to 16 timer for each ISR_PWM
239239
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
240240
{
241-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
241+
//void setPWM(uint32_t pin, float frequency, float dutycycle
242242
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
243243

244244
// You can use this with PWM_Freq in Hz

examples/ISR_8_PWMs_Array_Complex/ISR_8_PWMs_Array_Complex.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ typedef struct
114114
irqCallback irqCallbackStartFunc;
115115
irqCallback irqCallbackStopFunc;
116116

117-
double PWM_Freq;
117+
float PWM_Freq;
118118

119-
double PWM_DutyCycle;
119+
float PWM_DutyCycle;
120120

121121
uint32_t deltaMicrosStart;
122122
uint32_t previousMicrosStart;
@@ -144,19 +144,19 @@ volatile unsigned long previousMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
144144

145145

146146
// You can assign any interval for any timer here, in Microseconds
147-
double PWM_Period[] =
147+
uint32_t PWM_Period[] =
148148
{
149-
1000.0, 500.0, 333.333, 250.0, 200.0, 166.667, 142.857, 125.0
149+
1000, 500, 333, 250, 200, 167, 143, 125
150150
};
151151

152152
// You can assign any interval for any timer here, in Hz
153-
double PWM_Freq[] =
153+
float PWM_Freq[] =
154154
{
155155
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
156156
};
157157

158158
// You can assign any interval for any timer here, in Microseconds
159-
double PWM_DutyCycle[] =
159+
float PWM_DutyCycle[] =
160160
{
161161
5.0, 10.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0
162162
};
@@ -466,7 +466,7 @@ void setup()
466466
curISR_PWM_Data[i].previousMicrosStart = startMicros;
467467
//ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc);
468468

469-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
469+
//void setPWM(uint32_t pin, float frequency, float dutycycle
470470
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
471471

472472
// You can use this with PWM_Freq in Hz

examples/ISR_8_PWMs_Array_Simple/ISR_8_PWMs_Array_Simple.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ uint32_t PWM_Pin[] =
101101
#define NUMBER_ISR_PWMS ( sizeof(PWM_Pin) / sizeof(uint32_t) )
102102

103103
// You can assign any interval for any timer here, in Hz
104-
double PWM_Freq[] =
104+
float PWM_Freq[] =
105105
{
106106
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
107107
};
108108

109109
// You can assign any interval for any timer here, in Microseconds
110-
double PWM_DutyCycle[] =
110+
float PWM_DutyCycle[] =
111111
{
112112
5.0, 10.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0
113113
};

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 = 50.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
@@ -23,8 +23,8 @@
2323
#warning Using Timer3
2424
#endif
2525

26-
#define AVR_SLOW_PWM_VERSION_MIN_TARGET F("AVR_Slow_PWM v1.2.0")
27-
#define AVR_SLOW_PWM_VERSION_MIN 1002000
26+
#define AVR_SLOW_PWM_VERSION_MIN_TARGET F("AVR_Slow_PWM v1.2.2")
27+
#define AVR_SLOW_PWM_VERSION_MIN 1002002
2828

2929
#include "multiFileProject.h"
3030

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "AVR_Slow_PWM",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"keywords": "timing, device, control, timer, interrupt, hardware, isr, isr-based, hardware-timer, mission-critical, accuracy, precise, non-blocking, avr, mega-2560, nano, uno, leonardo, 32u4, 16u4, at-mega",
55
"description": "This library enables you to use ISR-based PWM channels on AVR-based boards, such as Mega-2560, UNO,Nano, Leonardo, etc., to create and output PWM any GPIO pin. It now supports 16 ISR-based PWM channels, while consuming only 1 Hardware Timer. PWM channel interval can be very long (ulong microsecs / millisecs). The most important feature is they're ISR-based PWM channels, supporting lower PWM frequencies with suitable accuracy. Their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These ISR-based PWMs, still work even if other software functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software-based PWM using millis() or micros(). That's necessary if you need to control devices requiring high precision. 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=AVR_Slow_PWM
2-
version=1.2.1
2+
version=1.2.2
33
author=Khoi Hoang <[email protected]>
44
maintainer=Khoi Hoang <[email protected]>
55
sentence=This library enables you to use ISR-based PWM channels on AVR-based boards, such as Mega-2560, UNO,Nano, Leonardo, etc., to create and output PWM any GPIO pin.

0 commit comments

Comments
 (0)