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

Commit 1272bbc

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. Add example to demo how to modify PWM settings on-the-fly
1 parent 01e005e commit 1272bbc

27 files changed

+932
-430
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
## Contributing to nRF52_Slow_PWM
1+
## Contributing to AVR_Slow_PWM
22

33
### Reporting Bugs
44

5-
Please report bugs in nRF52_Slow_PWM if you find them.
5+
Please report bugs in AVR_Slow_PWM if you find them.
66

77
However, before reporting a bug please check through the following:
88

9-
* [Existing Open Issues](https://github.com/khoih-prog/nRF52_Slow_PWM/issues) - someone might have already encountered this.
9+
* [Existing Open Issues](https://github.com/khoih-prog/AVR_Slow_PWM/issues) - someone might have already encountered this.
1010

11-
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/nRF52_Slow_PWM/issues/new).
11+
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/AVR_Slow_PWM/issues/new).
1212

1313
### How to submit a bug report
1414

1515
Please ensure to specify the following:
1616

1717
* Arduino IDE version (e.g. 1.8.16) or Platform.io version
18-
* `nRF52` Core Version (e.g. Adafruit nRF52 core v1.0.0)
18+
* Arduino / Adafruit / Sparkfun `AVR` Core Version (e.g. Arduino AVR core v1.8.3)
1919
* Contextual information (e.g. what you were trying to achieve)
2020
* Simplest possible steps to reproduce
2121
* Anything that might be relevant in your opinion, such as:
@@ -27,9 +27,9 @@ Please ensure to specify the following:
2727

2828
```
2929
Arduino IDE version: 1.8.16
30-
Arduino NRF52 Core Version 1.0.0
30+
Arduino AVR core v1.8.3
3131
OS: Ubuntu 20.04 LTS
32-
Linux xy-Inspiron-3593 5.4.0-80-generic #90-Ubuntu SMP Fri Jul 9 22:49:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
32+
Linux xy-Inspiron-3593 5.4.0-90-generic #101-Ubuntu SMP Fri Oct 15 20:00:55 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
3333
3434
Context:
3535
I encountered a crash while trying to use the Timer Interrupt.
@@ -44,7 +44,7 @@ Steps to reproduce:
4444

4545
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
4646

47-
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/nRF52_Slow_PWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
47+
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/AVR_Slow_PWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
4848

4949
### Sending Pull Requests
5050

README.md

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@
3333
* [1. Init Hardware Timer](#1-init-hardware-timer)
3434
* [2. Set PWM Frequency, dutycycle, attach irqCallbackStartFunc and irqCallbackStopFunc functions](#2-Set-PWM-Frequency-dutycycle-attach-irqCallbackStartFunc-and-irqCallbackStopFunc-functions)
3535
* [Examples](#examples)
36-
* [ 1. ISR_8_PWMs_Array](examples/ISR_8_PWMs_Array)
37-
* [ 2. ISR_8_PWMs_Array_Complex](examples/ISR_8_PWMs_Array_Complex)
38-
* [ 3. ISR_8_PWMs_Array_Simple](examples/ISR_8_PWMs_Array_Simple)
36+
* [ 1. ISR_8_PWMs_Array](examples/ISR_8_PWMs_Array)
37+
* [ 2. ISR_8_PWMs_Array_Complex](examples/ISR_8_PWMs_Array_Complex)
38+
* [ 3. ISR_8_PWMs_Array_Simple](examples/ISR_8_PWMs_Array_Simple)
39+
* [ 4. ISR_Changing_PWM](examples/ISR_Changing_PWM)
40+
* [ 5. ISR_Modify_PWM](examples/ISR_Modify_PWM)
3941
* [Example ISR_8_PWMs_Array_Complex](#Example-ISR_8_PWMs_Array_Complex)
4042
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
4143
* [1. ISR_8_PWMs_Array_Complex on Arduino AVR Leonardo ATMega32U4](#1-ISR_8_PWMs_Array_Complex-on-Arduino-AVR-Leonardo-ATMega32U4)
4244
* [2. ISR_8_PWMs_Array on Arduino AVR Mega2560/ADK](#2-isr_8_pwms_array-on-avr-mega2560adk)
4345
* [3. ISR_8_PWMs_Array_Simple on Arduino AVR Nano](#3-ISR_8_PWMs_Array_Simple-on-Arduino-AVR-Nano)
46+
* [4. ISR_Modify_PWM on Arduino AVR Mega2560/ADK](#4-ISR_Modify_PWM-on-avr-mega2560adk)
47+
* [5. ISR_Changing_PWM on Arduino AVR Mega2560/ADK](#5-ISR_Changing_PWM-on-avr-mega2560adk)
4448
* [Debug](#debug)
4549
* [Troubleshooting](#troubleshooting)
4650
* [Issues](#issues)
@@ -58,7 +62,7 @@
5862

5963
### Features
6064

61-
This library enables you to use ISR-based PWM channels on AVR-based boards, such as Mega-2560, UNO,Nano, Leonardo, etc., using AVR 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 **500Hz**, which is still suitable for many real-life applications.
65+
This library enables you to use ISR-based PWM channels on AVR-based boards, such as Mega-2560, UNO,Nano, Leonardo, etc., using AVR 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 **500Hz**, which is still suitable for many real-life applications. Now you can also modify PWM settings on-the-fly.
6266

6367
---
6468

@@ -103,7 +107,7 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
103107

104108
### Currently supported Boards
105109

106-
1. **AVR-based boards** such as **Mega-2560, UNO,Nano, Leonardo**, etc., using AVR core
110+
1. **AVR-based boards** using ATMEGA_328P, ATMEGA_2560, ATMEGA_1280, ATMEGA_640, ATMEGA_16U4, ATMEGA_32U4, etc. boards such as **Mega-2560, UNO, Nano, Leonardo**, etc., using **Arduino, Adafruit or Sparkfun AVR core**
107111

108112
---
109113

@@ -120,7 +124,7 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
120124

121125
1. [`Arduino IDE 1.8.16+` for Arduino](https://www.arduino.cc/en/Main/Software)
122126
2. [`Arduino AVR core 1.8.3+`](https://github.com/arduino/ArduinoCore-avr) for Arduino AVR boards. Use Arduino Board Manager to install. [![Latest release](https://img.shields.io/github/release/arduino/ArduinoCore-avr.svg)](https://github.com/arduino/ArduinoCore-avr/releases/latest/)
123-
3. [`Adafruit AVR core 1.4.13+`](https://github.com/adafruit/Adafruit_Arduino_Boards) for Adafruit AVR boards. Use Arduino Board Manager to install.
127+
3. [`Adafruit AVR core 1.4.14+`](https://github.com/adafruit/Adafruit_Arduino_Boards) for Adafruit AVR boards. Use Arduino Board Manager to install.
124128
4. [`Sparkfun AVR core 1.1.13+`](https://github.com/sparkfun/Arduino_Boards) for Sparkfun AVR boards. Use Arduino Board Manager to install.
125129

126130
5. To use with certain example
@@ -270,7 +274,9 @@ void setup()
270274

271275
1. [ISR_8_PWMs_Array](examples/ISR_8_PWMs_Array)
272276
2. [ISR_8_PWMs_Array_Complex](examples/ISR_8_PWMs_Array_Complex)
273-
3. [ISR_8_PWMs_Array_Simple](examples/ISR_8_PWMs_Array_Simple)
277+
3. [ISR_8_PWMs_Array_Simple](examples/ISR_8_PWMs_Array_Simple)
278+
4. [ISR_Changing_PWM](examples/ISR_Changing_PWM)
279+
5. [ISR_Modify_PWM](examples/ISR_Modify_PWM)
274280

275281

276282
---
@@ -405,11 +411,10 @@ uint32_t PWM_Period[NUMBER_ISR_PWMS] =
405411
1000L, 500L, 333L, 250L, 200L, 166L, 142L, 125L
406412
};
407413
408-
409414
// You can assign any interval for any timer here, in Hz
410-
uint32_t PWM_Freq[NUMBER_ISR_PWMS] =
415+
double PWM_Freq[NUMBER_ISR_PWMS] =
411416
{
412-
1, 2, 3, 4, 5, 6, 7, 8
417+
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
413418
};
414419
415420
// You can assign any interval for any timer here, in Microseconds
@@ -770,7 +775,7 @@ The following is the sample terminal output when running example [ISR_8_PWMs_Arr
770775

771776
```
772777
Starting ISR_8_PWMs_Array_Complex on Arduino AVR ATMega32U4
773-
AVR_Slow_PWM v1.0.0
778+
AVR_Slow_PWM v1.1.0
774779
CPU Frequency = 16 MHz
775780
[PWM] T3
776781
[PWM] Freq * 1000 = 10000000.00
@@ -815,7 +820,7 @@ The following is the sample terminal output when running example [**ISR_8_PWMs_A
815820

816821
```
817822
Starting ISR_8_PWMs_Array_Complex on Arduino AVR Mega2560/ADK
818-
AVR_Slow_PWM v1.0.0
823+
AVR_Slow_PWM v1.1.0
819824
CPU Frequency = 16 MHz
820825
[PWM] T3
821826
[PWM] Freq * 1000 = 10000000.00
@@ -856,11 +861,11 @@ PWM Channel : 7, prog Period (ms): 125.00, actual : 125008, prog DutyCycle : 45,
856861

857862
### 3. ISR_8_PWMs_Array_Simple on Arduino AVR Nano
858863

859-
The following is the sample terminal output when running example [**ISR_8_PWMs_Array_Simple**](examples/ISR_8_PWMs_Array_Simple) on **nRF52-based NRF52840_ITSYBITSY** to demonstrate how to use multiple PWM channels.
864+
The following is the sample terminal output when running example [**ISR_8_PWMs_Array_Simple**](examples/ISR_8_PWMs_Array_Simple) on **Arduino AVR UNO** to demonstrate how to use multiple PWM channels.
860865

861866
```
862867
Starting ISR_8_PWMs_Array_Complex on Arduino AVR UNO, Nano, etc.
863-
AVR_Slow_PWM v1.0.0
868+
AVR_Slow_PWM v1.1.0
864869
CPU Frequency = 16 MHz
865870
[PWM] T1
866871
[PWM] Freq * 1000 = 10000000.00
@@ -898,6 +903,56 @@ PWM Channel : 6, prog Period (ms): 142.86, actual : 143012, prog DutyCycle : 40,
898903
PWM Channel : 7, prog Period (ms): 125.00, actual : 125012, prog DutyCycle : 45, actual : 44.95
899904
```
900905

906+
---
907+
908+
### 4. ISR_Modify_PWM on AVR Mega2560/ADK
909+
910+
The following is the sample terminal output when running example [ISR_Modify_PWM](examples/ISR_Modify_PWM) on **AVR Mega2560/ADK** to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel
911+
912+
```
913+
Starting ISR_Modify_PWM on Arduino AVR Mega2560/ADK
914+
AVR_Slow_PWM v1.1.0
915+
CPU Frequency = 16 MHz
916+
[PWM] T3
917+
[PWM] Freq * 1000 = 10000000.00
918+
[PWM] F_CPU = 16000000 , preScalerDiv = 1
919+
[PWM] OCR = 1599 , preScalerIndex = 1
920+
[PWM] OK in loop => _OCR = 1599
921+
[PWM] _preScalerIndex = 1 , preScalerDiv = 1
922+
Starting ITimer3 OK, micros() = 2023160
923+
Using PWM Freq = 1.00, PWM DutyCycle = 10
924+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 2028040
925+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 12033220
926+
Channel : 0 Period : 1000000 OnTime : 100000 Start_Time : 22034628
927+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 32036036
928+
```
929+
930+
---
931+
932+
### 5. ISR_Changing_PWM on AVR Mega2560/ADK
933+
934+
The following is the sample terminal output when running example [ISR_Changing_PWM](examples/ISR_Changing_PWM) on **AVR Mega2560/ADK** to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel
935+
936+
```
937+
Starting ISR_Changing_PWM on Arduino AVR Mega2560/ADK
938+
AVR_Slow_PWM v1.1.0
939+
CPU Frequency = 16 MHz
940+
[PWM] T3
941+
[PWM] Freq * 1000 = 10000000.00
942+
[PWM] F_CPU = 16000000 , preScalerDiv = 1
943+
[PWM] OCR = 1599 , preScalerIndex = 1
944+
[PWM] OK in loop => _OCR = 1599
945+
[PWM] _preScalerIndex = 1 , preScalerDiv = 1
946+
Starting ITimer3 OK, micros() = 2023336
947+
Using PWM Freq = 1.00, PWM DutyCycle = 50
948+
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 2028216
949+
Using PWM Freq = 2.00, PWM DutyCycle = 90
950+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 12035408
951+
Using PWM Freq = 1.00, PWM DutyCycle = 50
952+
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 22040404
953+
Using PWM Freq = 2.00, PWM DutyCycle = 90
954+
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 32045592
955+
```
901956

902957
---
903958
---
@@ -942,6 +997,7 @@ Submit issues to: [AVR_Slow_PWM issues](https://github.com/khoih-prog/AVR_Slow_P
942997

943998
1. Basic hardware multi-channel PWM for **AVR boards, such as Mega-2560, UNO,Nano, Leonardo, etc.** using AVR core
944999
2. Add Table of Contents
1000+
3. Add functions to modify PWM settings on-the-fly
9451001

9461002
---
9471003
---

changelog.md

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

1414
* [Changelog](#changelog)
15+
* [Releases v1.1.0](#releases-v110)
1516
* [Initial Releases v1.0.0](#Initial-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. Add example to demo how to modify PWM settings on-the-fly
27+
2228
### Initial Releases v1.0.0
2329

2430
1. Initial coding to support **AVR boards, such as Mega-2560, UNO,Nano, Leonardo, etc.**, etc. using AVR core

examples/ISR_8_PWMs_Array/ISR_8_PWMs_Array.ino

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1818
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1919
This important feature is absolutely necessary for mission-critical tasks.
20-
21-
Version: 1.0.0
22-
23-
Version Modified By Date Comments
24-
------- ----------- ---------- -----------
25-
1.0.0 K.Hoang 27/09/2021 Initial coding for AVR-based boards (UNO, Nano, Mega, 32U4, 16U4, etc. )
2620
*****************************************************************************************************************************/
2721

2822
#if ( defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || \
@@ -102,11 +96,10 @@ uint32_t PWM_Pin[] =
10296

10397
#define NUMBER_ISR_PWMS ( sizeof(PWM_Pin) / sizeof(uint32_t) )
10498

105-
10699
// You can assign any interval for any timer here, in Hz
107-
uint32_t PWM_Freq[NUMBER_ISR_PWMS] =
100+
double PWM_Freq[NUMBER_ISR_PWMS] =
108101
{
109-
1, 2, 3, 4, 5, 6, 7, 8
102+
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
110103
};
111104

112105
// You can assign any interval for any timer here, in Microseconds

examples/ISR_8_PWMs_Array_Complex/ISR_8_PWMs_Array_Complex.ino

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1818
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1919
This important feature is absolutely necessary for mission-critical tasks.
20-
21-
Version: 1.0.0
22-
23-
Version Modified By Date Comments
24-
------- ----------- ---------- -----------
25-
1.0.0 K.Hoang 27/09/2021 Initial coding for AVR-based boards (UNO, Nano, Mega, 32U4, 16U4, etc. )
2620
*****************************************************************************************************************************/
2721

2822
#if ( defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || \
@@ -151,11 +145,10 @@ uint32_t PWM_Period[NUMBER_ISR_PWMS] =
151145
1000L, 500L, 333L, 250L, 200L, 166L, 142L, 125L
152146
};
153147

154-
155148
// You can assign any interval for any timer here, in Hz
156-
uint32_t PWM_Freq[NUMBER_ISR_PWMS] =
149+
double PWM_Freq[NUMBER_ISR_PWMS] =
157150
{
158-
1, 2, 3, 4, 5, 6, 7, 8
151+
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
159152
};
160153

161154
// You can assign any interval for any timer here, in Microseconds

examples/ISR_8_PWMs_Array_Simple/ISR_8_PWMs_Array_Simple.ino

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1818
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1919
This important feature is absolutely necessary for mission-critical tasks.
20-
21-
Version: 1.0.0
22-
23-
Version Modified By Date Comments
24-
------- ----------- ---------- -----------
25-
1.0.0 K.Hoang 27/09/2021 Initial coding for AVR-based boards (UNO, Nano, Mega, 32U4, 16U4, etc. )
2620
*****************************************************************************************************************************/
2721

2822
#if ( defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || \
@@ -102,11 +96,10 @@ uint32_t PWM_Pin[] =
10296

10397
#define NUMBER_ISR_PWMS ( sizeof(PWM_Pin) / sizeof(uint32_t) )
10498

105-
10699
// You can assign any interval for any timer here, in Hz
107-
uint32_t PWM_Freq[NUMBER_ISR_PWMS] =
100+
double PWM_Freq[NUMBER_ISR_PWMS] =
108101
{
109-
1, 2, 3, 4, 5, 6, 7, 8
102+
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
110103
};
111104

112105
// You can assign any interval for any timer here, in Microseconds

0 commit comments

Comments
 (0)