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

Commit 3be04ce

Browse files
authored
v1.1.0 to modify PWM settings on-the-fly
### Releases v1.1.0 1. Add functions to modify PWM settings on-the-fly 2. Fix bug 3. Add example to demo how to modify PWM settings on-the-fly
1 parent 1928c71 commit 3be04ce

23 files changed

+570
-119
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ Please ensure to specify the following:
2727

2828
```
2929
Arduino IDE version: 1.8.16
30-
ESP8266 core v3.020
30+
ESP8266 core v3.0.2
3131
ESP8266_NODEMCU_ESP12E Module
3232
OS: Ubuntu 20.04 LTS
33-
Linux xy-Inspiron-3593 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
33+
Linux xy-Inspiron-3593 5.4.0-86-generic #97-Ubuntu SMP Fri Sep 17 19:19:40 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
3434
3535
Context:
3636
I encountered a crash while using TimerInterrupt.

README.md

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [Why using ISR-based PWM-channels is better](#Why-using-ISR-based-PWM-channels-is-better)
1717
* [Currently supported Boards](#currently-supported-boards)
1818
* [Important Notes about ISR](#important-notes-about-isr)
19+
* [Changelog](changelog.md)
1920
* [Prerequisites](#prerequisites)
2021
* [Installation](#installation)
2122
* [Use Arduino Library Manager](#use-arduino-library-manager)
@@ -24,14 +25,18 @@
2425
* [HOWTO Fix `Multiple Definitions` Linker Error](#howto-fix-multiple-definitions-linker-error)
2526
* [More useful Information](#more-useful-information)
2627
* [Examples](#examples)
27-
* [ 1. ISR_16_PWMs_Array](examples/ISR_16_PWMs_Array)
28-
* [ 2. ISR_16_PWMs_Array_Complex](examples/ISR_16_PWMs_Array_Complex)
29-
* [ 3. ISR_16_PWMs_Array_Simple](examples/ISR_16_PWMs_Array_Simple)
28+
* [ 1. ISR_16_PWMs_Array](examples/ISR_16_PWMs_Array)
29+
* [ 2. ISR_16_PWMs_Array_Complex](examples/ISR_16_PWMs_Array_Complex)
30+
* [ 3. ISR_16_PWMs_Array_Simple](examples/ISR_16_PWMs_Array_Simple)
31+
* [ 4. ISR_Changing_PWM](examples/ISR_Changing_PWM)
32+
* [ 5. ISR_Modify_PWM](examples/ISR_Modify_PWM)
3033
* [Example ISR_16_PWMs_Array_Complex](#Example-ISR_16_PWMs_Array_Complex)
3134
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
3235
* [1. ISR_16_PWMs_Array_Complex on ESP8266_NODEMCU_ESP12E](#1-ISR_16_PWMs_Array_Complex-on-ESP8266_NODEMCU_ESP12E)
3336
* [2. ISR_16_PWMs_Array on ESP8266_NODEMCU_ESP12E](#2-ISR_16_PWMs_Array-on-ESP8266_NODEMCU_ESP12E)
3437
* [3. ISR_16_PWMs_Array_Simple on ESP8266_NODEMCU_ESP12E](#3-ISR_16_PWMs_Array_Simple-on-ESP8266_NODEMCU_ESP12E)
38+
* [4. ISR_Modify_PWM on ESP8266_NODEMCU_ESP12E](#4-ISR_Modify_PWM-on-ESP8266_NODEMCU_ESP12E)
39+
* [5. ISR_Changing_PWM on ESP8266_NODEMCU_ESP12E](#5-ISR_Changing_PWM-on-ESP8266_NODEMCU_ESP12E)
3540
* [Debug](#debug)
3641
* [Troubleshooting](#troubleshooting)
3742
* [Issues](#issues)
@@ -49,7 +54,7 @@
4954

5055
## Features
5156

52-
This library enables you to use Interrupt from Hardware Timers on an ESP8266-based board to create and output PWM to pins. The maximum PWM frequency is currently limited at **500Hz**.
57+
This library enables you to use Interrupt from Hardware Timers on an ESP8266-based board to create and output PWM to pins. The maximum PWM frequency is currently limited at **500Hz**. Now you can also modify PWM settings on-the-fly.
5358

5459
---
5560

@@ -199,7 +204,9 @@ You'll see blynkTimer Software is blocked while system is connecting to WiFi / I
199204

200205
1. [ISR_16_PWMs_Array](examples/ISR_16_PWMs_Array)
201206
2. [ISR_16_PWMs_Array_Complex](examples/ISR_16_PWMs_Array_Complex)
202-
3. [ISR_16_PWMs_Array_Simple](examples/ISR_16_PWMs_Array_Simple)
207+
3. [ISR_16_PWMs_Array_Simple](examples/ISR_16_PWMs_Array_Simple)
208+
4. [ISR_Changing_PWM](examples/ISR_Changing_PWM)
209+
5. [ISR_Modify_PWM](examples/ISR_Modify_PWM)
203210

204211
---
205212
---
@@ -671,7 +678,7 @@ The following is the sample terminal output when running example [ISR_16_PWMs_Ar
671678

672679
```
673680
Starting ISR_16_PWMs_Array_Complex on ESP8266_NODEMCU_ESP12E
674-
ESP8266_PWM v1.0.0
681+
ESP8266_PWM v1.1.0
675682
CPU Frequency = 160 MHz
676683
[PWM] ESP8266TimerInterrupt: _fre = 312500.00 , _count = 6
677684
Starting ITimer OK, micros() = 2068917
@@ -720,7 +727,7 @@ The following is the sample terminal output when running example [ISR_16_PWMs_Ar
720727

721728
```
722729
Starting ISR_16_PWMs_Array on ESP8266_NODEMCU_ESP12E
723-
ESP8266_PWM v1.0.0
730+
ESP8266_PWM v1.1.0
724731
CPU Frequency = 160 MHz
725732
[PWM] ESP8266TimerInterrupt: _fre = 312500.00 , _count = 6
726733
Starting ITimer OK, micros() = 2069342
@@ -743,7 +750,7 @@ The following is the sample terminal output when running example [ISR_16_PWMs_Ar
743750

744751
```
745752
Starting ISR_16_PWMs_Array_Simple on ESP8266_NODEMCU_ESP12E
746-
ESP8266_PWM v1.0.0
753+
ESP8266_PWM v1.1.0
747754
CPU Frequency = 160 MHz
748755
[PWM] ESP8266TimerInterrupt: _fre = 312500.00 , _count = 6
749756
Starting ITimer OK, micros() = 2069144
@@ -757,6 +764,65 @@ Channel : 6 Period : 142857 OnTime : 71428 Start_Time : 2072624
757764
Channel : 7 Period : 125000 OnTime : 68750 Start_Time : 2072624
758765
```
759766

767+
---
768+
769+
### 4. ISR_Modify_PWM on **ESP8266_NODEMCU_ESP12E**
770+
771+
The following is the sample terminal output when running example [ISR_Modify_PWM](examples/ISR_Modify_PWM) to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel
772+
773+
```
774+
Starting ISR_Modify_PWM on ESP8266_NODEMCU_ESP12E
775+
ESP8266_PWM v1.1.0
776+
CPU Frequency = 160 MHz
777+
[PWM] ESP8266TimerInterrupt: _fre = 312500.00 , _count = 6
778+
Starting ITimer OK, micros() = 2067012
779+
Using PWM Freq = 1.00, PWM DutyCycle = 10
780+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 2074213
781+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 12081063
782+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 22082014
783+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 32083015
784+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 42084018
785+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 52085016
786+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 62086011
787+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 72087015
788+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 82088012
789+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 92089018
790+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 102090015
791+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 112091018
792+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 122092014
793+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 132093012
794+
```
795+
796+
---
797+
798+
### 5. ISR_Changing_PWM on **ESP8266_NODEMCU_ESP12E**
799+
800+
The following is the sample terminal output when running example [ISR_Changing_PWM](examples/ISR_Changing_PWM) to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel
801+
802+
```
803+
Starting ISR_Changing_PWM on ESP8266_NODEMCU_ESP12E
804+
ESP8266_PWM v1.1.0
805+
CPU Frequency = 160 MHz
806+
[PWM] ESP8266TimerInterrupt: _fre = 312500.00 , _count = 6
807+
Starting ITimer OK, micros() = 2069740
808+
Using PWM Freq = 1.00, PWM DutyCycle = 50
809+
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 2077044
810+
Using PWM Freq = 2.00, PWM DutyCycle = 90
811+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 12082957
812+
Using PWM Freq = 1.00, PWM DutyCycle = 50
813+
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 22083187
814+
Using PWM Freq = 2.00, PWM DutyCycle = 90
815+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 32083386
816+
Using PWM Freq = 1.00, PWM DutyCycle = 50
817+
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 42083596
818+
Using PWM Freq = 2.00, PWM DutyCycle = 90
819+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 52083796
820+
Using PWM Freq = 1.00, PWM DutyCycle = 50
821+
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 62084005
822+
Using PWM Freq = 2.00, PWM DutyCycle = 90
823+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 72084206
824+
```
825+
760826
---
761827
---
762828

@@ -803,6 +869,7 @@ Submit issues to: [ESP8266_PWM issues](https://github.com/khoih-prog/ESP8266_PWM
803869
1. Basic hardware PWM-channels for ESP8266 for [ESP8266 core v3.0.2+](https://github.com/esp8266/Arduino/releases/tag/3.0.2)
804870
2. Longer time interval
805871
3. Add complex examples.
872+
4. Add functions to modify PWM settings on-the-fly
806873

807874
---
808875
---
@@ -811,6 +878,16 @@ Submit issues to: [ESP8266_PWM issues](https://github.com/khoih-prog/ESP8266_PWM
811878

812879
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
813880

881+
1. Thanks to [AnselPeng2019](https://github.com/AnselPeng2019) to request new feature to modify PWM settings on-the-fly in
882+
883+
- [Change Duty Cycle #1](https://github.com/khoih-prog/ESP8266_PWM/issues/1) leading to v1.1.0
884+
885+
886+
<table>
887+
<tr>
888+
<td align="center"><a href="https://github.com/AnselPeng2019"><img src="https://github.com/AnselPeng2019.png" width="100px;" alt="AnselPeng2019"/><br /><sub><b>AnselPeng2019</b></sub></a><br /></td>
889+
</tr>
890+
</table>
814891

815892
---
816893

changelog.md

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

1414
* [Changelog](#changelog)
15+
* [Releases v1.1.0](#releases-v110)
1516
* [Releases v1.0.0](#releases-v100)
1617

1718
---
1819
---
1920

2021
## Changelog
2122

23+
### Releases v1.1.0
24+
25+
1. Add functions to modify PWM settings on-the-fly
26+
2. Fix bug
27+
3. Add example to demo how to modify PWM settings on-the-fly
28+
2229
### Releases v1.0.0
2330

2431
1. Initial coding for ESP8266 boards using [ESP8266 core v3.0.2+](https://github.com/esp8266/Arduino/releases/tag/3.0.2)

examples/ISR_16_PWMs_Array/ISR_16_PWMs_Array.ino

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1616
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1717
This important feature is absolutely necessary for mission-critical tasks.
18-
19-
Version: 1.0.0
20-
21-
Version Modified By Date Comments
22-
------- ----------- ---------- -----------
23-
1.0.0 K Hoang 21/09/2021 Initial coding for ESP8266 boards with ESP8266 core v3.0.2+
2418
*****************************************************************************************************************************/
2519

2620
#if !defined(ESP8266)

examples/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
2020
Therefore, their executions are not blocked by bad-behaving functions / tasks.
2121
This important feature is absolutely necessary for mission-critical tasks.
22-
23-
Version: 1.0.0
24-
25-
Version Modified By Date Comments
26-
------- ----------- ---------- -----------
27-
1.0.0 K Hoang 20/09/2021 Initial coding for ESP8266, ESP8266_S2, ESP8266_C3 boards with ESP8266 core v2.0.0+
2822
*****************************************************************************************************************************/
2923

3024
#if !defined(ESP8266)
@@ -371,13 +365,14 @@ void simpleTimerdoingSomething2s()
371365

372366
Serial.print(F(", actual : ")); Serial.print(curISR_PWM_Data[i].deltaMicrosStart);
373367

374-
Serial.print(F(", programmed DutyCycle : "));
368+
Serial.print(F(", programmed DutyCycle : "));
375369

376-
Serial.print(curISR_PWM_Data[i].PWM_DutyCycle);
370+
Serial.print(curISR_PWM_Data[i].PWM_DutyCycle);
377371

378-
Serial.print(F(", actual : ")); Serial.println((float) curISR_PWM_Data[i].deltaMicrosStop * 100.0f / curISR_PWM_Data[i].deltaMicrosStart);
372+
Serial.print(F(", actual : ")); Serial.println((float) curISR_PWM_Data[i].deltaMicrosStop * 100.0f / curISR_PWM_Data[i].deltaMicrosStart);
379373

380374
#else
375+
381376
Serial.print(F("PWM Channel : ")); Serial.print(i);
382377

383378
#if USING_PWM_FREQUENCY
@@ -402,8 +397,6 @@ void simpleTimerdoingSomething2s()
402397

403398
void setup()
404399
{
405-
pinMode(LED_BUILTIN, OUTPUT);
406-
407400
Serial.begin(115200);
408401
while (!Serial);
409402

examples/ISR_16_PWMs_Array_Simple/ISR_16_PWMs_Array_Simple.ino

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1616
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1717
This important feature is absolutely necessary for mission-critical tasks.
18-
19-
Version: 1.0.0
20-
21-
Version Modified By Date Comments
22-
------- ----------- ---------- -----------
23-
1.0.0 K Hoang 21/09/2021 Initial coding for ESP8266 boards with ESP8266 core v3.0.2+
2418
*****************************************************************************************************************************/
2519

2620
#if !defined(ESP8266)

examples/ISR_Changing_PWM/ISR_Changing_PWM.ino

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1616
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1717
This important feature is absolutely necessary for mission-critical tasks.
18-
19-
Version: 1.0.0
20-
21-
Version Modified By Date Comments
22-
------- ----------- ---------- -----------
23-
1.0.0 K Hoang 21/09/2021 Initial coding for ESP8266 boards with ESP8266 core v3.0.2+
2418
*****************************************************************************************************************************/
2519

2620
#if !defined(ESP8266)
@@ -85,15 +79,15 @@ void IRAM_ATTR TimerHandler()
8579
// You can assign pins here. Be carefull to select good pin to use or crash
8680
uint32_t PWM_Pin = LED_BUILTIN;
8781

88-
// You can assign any interval for any timer here, in microseconds
89-
uint32_t PWM_Period1 = 1000000L;
90-
// You can assign any interval for any timer here, in microseconds
91-
uint32_t PWM_Period2 = 500000L;
92-
9382
// You can assign any interval for any timer here, in Hz
94-
uint32_t PWM_Freq1 = 1;
83+
double PWM_Freq1 = 1.0f;
9584
// You can assign any interval for any timer here, in Hz
96-
uint32_t PWM_Freq2 = 2;
85+
double PWM_Freq2 = 2.0f;
86+
87+
// You can assign any interval for any timer here, in microseconds
88+
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
89+
// You can assign any interval for any timer here, in microseconds
90+
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
9791

9892
// You can assign any duty_cycle for any PWM here, from 0-100
9993
uint32_t PWM_DutyCycle1 = 50;
@@ -138,7 +132,7 @@ void loop()
138132
#if USING_PWM_FREQUENCY
139133

140134
// You can use this with PWM_Freq in Hz
141-
ISR_PWM.setPWM(PWM_Pin, PWM_Freq1, PWM_DutyCycle1);
135+
channelNum = ISR_PWM.setPWM(PWM_Pin, PWM_Freq1, PWM_DutyCycle1);
142136

143137
#else
144138
#if USING_MICROS_RESOLUTION
@@ -159,7 +153,7 @@ void loop()
159153
#if USING_PWM_FREQUENCY
160154

161155
// You can use this with PWM_Freq in Hz
162-
ISR_PWM.setPWM(PWM_Pin, PWM_Freq2, PWM_DutyCycle2);
156+
channelNum = ISR_PWM.setPWM(PWM_Pin, PWM_Freq2, PWM_DutyCycle2);
163157

164158
#else
165159
#if USING_MICROS_RESOLUTION
@@ -172,4 +166,6 @@ void loop()
172166
#endif
173167

174168
delay(10000);
169+
170+
ISR_PWM.deleteChannel((unsigned) channelNum);
175171
}

0 commit comments

Comments
 (0)