From 49dd0c0e3b197b01e67afbd86684031523206e0f Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Tue, 22 Nov 2022 18:29:19 -0500 Subject: [PATCH] Update `README.md` and use `allman` astyle --- CONTRIBUTING.md | 25 +- README.md | 33 +- changelog.md | 11 +- .../ISR_8_PWMs_Array/ISR_8_PWMs_Array.ino | 45 +- .../ISR_8_PWMs_Array_Complex.ino | 71 ++- .../ISR_8_PWMs_Array_Simple.ino | 39 +- .../ISR_Changing_PWM/ISR_Changing_PWM.ino | 54 +- examples/ISR_Modify_PWM/ISR_Modify_PWM.ino | 44 +- .../multiFileProject/multiFileProject.cpp | 2 +- examples/multiFileProject/multiFileProject.h | 6 +- .../multiFileProject/multiFileProject.ino | 12 +- src/Dx_Slow_PWM.h | 2 +- src/Dx_Slow_PWM_ISR.h | 2 +- src/Dx_Slow_PWM_ISR_Impl.h | 194 +++--- src/Dx_Slow_PWM_Impl.h | 551 +++++++++--------- src/PWM_Generic_Debug.h | 2 +- utils/astyle_library.conf | 70 +++ utils/restyle.sh | 6 + 18 files changed, 675 insertions(+), 494 deletions(-) create mode 100644 utils/astyle_library.conf create mode 100644 utils/restyle.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a4f3615..70ec599 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,7 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p Please ensure to specify the following: * Arduino IDE version (e.g. 1.8.19) or Platform.io version -* `DxCore` or `megaTinyCore` Core Version (e.g. Arduino DxCore core v1.4.10 or megaTinyCore core v2.5.11) +* `DxCore` or `megaTinyCore` Core Version (e.g. Arduino DxCore core v1.4.10) * Board (e.g. AVR128DA64, AVR128DB48, AVR64DB32, etc.) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce @@ -31,10 +31,10 @@ Arduino IDE version: 1.8.19 Arduino DxCore core v1.4.10 OS: Ubuntu 20.04 LTS Board: Curiosity AVR128DB48 -Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.15.0-53-generic #59~20.04.1-Ubuntu SMP Thu Oct 20 15:10:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Context: -I encountered a crash while trying to use the Timer Interrupt. +I encountered a crash while using this library Steps to reproduce: 1. ... @@ -42,14 +42,33 @@ Steps to reproduce: 3. ... 4. ... ``` + +--- + ### Sending Feature Requests Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful. There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/Dx_Slow_PWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them. +--- + ### Sending Pull Requests Pull Requests with changes and fixes are also welcome! +Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux) + +1. Change directory to the library GitHub + +``` +xy@xy-Inspiron-3593:~$ cd Arduino/xy/Dx_Slow_PWM_GitHub/ +xy@xy-Inspiron-3593:~/Arduino/xy/Dx_Slow_PWM_GitHub$ +``` + +2. Issue astyle command + +``` +xy@xy-Inspiron-3593:~/Arduino/xy/Dx_Slow_PWM_GitHub$ bash utils/restyle.sh +``` diff --git a/README.md b/README.md index f42a820..c7d322b 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,11 @@ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/Dx_Slow_PWM.svg)](http://github.com/khoih-prog/Dx_Slow_PWM/issues) + + + --- @@ -98,7 +101,7 @@ For example, to run [Change_Interval example](https://github.com/khoih-prog/Dx_T
- +
@@ -106,7 +109,7 @@ After drag-and-drop the `Change_Interval.ino.hex` into `CURIOSITY` virtual drive- +
@@ -165,14 +168,14 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine), - **AVRDA-based boards (AVR128DA, AVR64DA, AVR32DA, etc.) using DxCore**- +
- **AVRDB-based boards (AVR128DB, AVR64DB, AVR32DB, etc.) using DxCore**- +
@@ -183,7 +186,7 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine), - **tinyAVR boards using megaTinyCore**- +
--- @@ -245,14 +248,14 @@ The current library implementation, using `xyz-Impl.h` instead of standard `xyz. You can include this `.hpp` file -``` +```cpp // Can be included as many times as necessary, without `Multiple Definitions` Linker Error #include "Dx_Slow_PWM.hpp" //https://github.com/khoih-prog/Dx_Slow_PWM ``` in many files. But be sure to use the following `.h` file **in just 1 `.h`, `.cpp` or `.ino` file**, which must **not be included in any other file**, to avoid `Multiple Definitions` Linker Error -``` +```cpp // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include "Dx_Slow_PWM.h" //https://github.com/khoih-prog/Dx_Slow_PWM ``` @@ -299,7 +302,7 @@ Before using any Timer, you have to make sure the Timer has not been used by any #### 1. Init Hardware Timer -``` +```cpp // Select USING_FULL_CLOCK == true for 24/16MHz to Timer TCBx => shorter timer, but better accuracy // Select USING_HALF_CLOCK == true for 12/ 8MHz to Timer TCBx => shorter timer, but better accuracy // Select USING_250KHZ == true for 250KHz to Timer TCBx => longer timer, but worse accuracy @@ -334,7 +337,7 @@ Dx_Slow_PWM ISR_PWM; #### 2. Set PWM Frequency, dutycycle, attach irqCallbackStartFunc and irqCallbackStopFunc functions -``` +```cpp void irqCallbackStartFunc() { @@ -387,7 +390,7 @@ https://github.com/khoih-prog/Dx_Slow_PWM/blob/a4f6824724484cd522fc9c98fac4cd5c6 The following is the sample terminal output when running example [ISR_8_PWMs_Array_Complex](examples/ISR_8_PWMs_Array_Complex) **Curiosity Nano AVR128DB48** 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** -``` +```cpp Starting ISR_8_PWMs_Array_Complex on AVR128DB Dx_Slow_PWM v1.0.2 CPU Frequency = 24 MHz @@ -429,7 +432,7 @@ PWM Channel : 7, prog Period (ms): 125.00, actual : 125028, prog DutyCycle : 55, The following is the sample terminal output when running example [**ISR_8_PWMs_Array**](examples/ISR_8_PWMs_Array) on **AVR128DB** to demonstrate how to use multiple PWM channels with simple callback functions. -``` +```cpp Starting ISR_8_PWMs_Array on AVR128DB Dx_Slow_PWM v1.0.2 CPU Frequency = 24 MHz @@ -443,7 +446,7 @@ Starting ITimer1 OK, micros() = 12894 The following is the sample terminal output when running example [**ISR_8_PWMs_Array_Simple**](examples/ISR_8_PWMs_Array_Simple) on **AVR128DB** to demonstrate how to use multiple PWM channels. -``` +```cpp Starting ISR_8_PWMs_Array_Simple on AVR128DB Dx_Slow_PWM v1.0.2 CPU Frequency = 24 MHz @@ -457,7 +460,7 @@ Starting ITimer1 OK, micros() = 14169 The following is the sample terminal output when running example [ISR_Modify_PWM](examples/ISR_Modify_PWM) on **AVR128DB** to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel -``` +```cpp Starting ISR_Modify_PWM on AVR128DB Dx_Slow_PWM v1.0.2 CPU Frequency = 24 MHz @@ -472,7 +475,7 @@ Using PWM Freq = 2.00, PWM DutyCycle = 10.00 The following is the sample terminal output when running example [ISR_Changing_PWM](examples/ISR_Changing_PWM) on **AVR128DB** to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel -``` +```cpp Starting ISR_Changing_PWM on AVR128DB Dx_Slow_PWM v1.0.2 CPU Frequency = 24 MHz @@ -582,6 +585,6 @@ If you want to contribute to this project: ## Copyright -Copyright 2022- Khoi Hoang +Copyright (c) 2022- Khoi Hoang diff --git a/changelog.md b/changelog.md index d50f716..bff5714 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,4 @@ -## Dx_Slow_PWM Library +## Library [![arduino-library-badge](https://www.ardu-badge.com/badge/Dx_Slow_PWM.svg?)](https://www.ardu-badge.com/Dx_Slow_PWM) [![GitHub release](https://img.shields.io/github/release/khoih-prog/Dx_Slow_PWM.svg)](https://github.com/khoih-prog/Dx_Slow_PWM/releases) @@ -6,6 +6,15 @@ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/Dx_Slow_PWM.svg)](http://github.com/khoih-prog/Dx_Slow_PWM/issues) + + + + + + +--- +--- + ## Table of Contents * [Changelog](#changelog) diff --git a/examples/ISR_8_PWMs_Array/ISR_8_PWMs_Array.ino b/examples/ISR_8_PWMs_Array/ISR_8_PWMs_Array.ino index ccb34dc..c4fc786 100644 --- a/examples/ISR_8_PWMs_Array/ISR_8_PWMs_Array.ino +++ b/examples/ISR_8_PWMs_Array/ISR_8_PWMs_Array.ino @@ -5,7 +5,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM Licensed under MIT license - + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers @@ -25,9 +25,9 @@ // Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. #define _PWM_LOGLEVEL_ 1 -#if defined(__AVR_AVR128DA48__) +#if defined(__AVR_AVR128DA48__) #define SerialDebug Serial1 -#elif defined(__AVR_AVR128DB48__) +#elif defined(__AVR_AVR128DB48__) #define SerialDebug Serial3 #else // standard Serial @@ -64,7 +64,7 @@ #elif USE_TIMER_4 #define CurrentTimer ITimer4 #else - #error You must select one Timer + #error You must select one Timer #endif #define USING_MICROS_RESOLUTION true //false @@ -83,9 +83,9 @@ // To modify according to your board // For Curiosity Nano AVR128DA48 => PIN_PC6 // For Curiosity Nano AVR128DB48 => PIN_PB3 - #if defined(__AVR_AVR128DA48__) + #if defined(__AVR_AVR128DA48__) #define LED_BUILTIN PIN_PC6 // PIN_PB3, 13 - #elif defined(__AVR_AVR128DB48__) + #elif defined(__AVR_AVR128DB48__) #define LED_BUILTIN PIN_PB3 // PIN_PC6, 13 #else // standard Arduino pin 13 @@ -107,7 +107,7 @@ DX_SLOW_PWM_ISR ISR_PWM; ////////////////////////////////////////////////////// void TimerHandler() -{ +{ ISR_PWM.run(); } @@ -184,27 +184,32 @@ void doingSomething7() irqCallback irqCallbackStartFunc[] = { - doingSomething0, doingSomething1, doingSomething2, doingSomething3, + doingSomething0, doingSomething1, doingSomething2, doingSomething3, doingSomething4, doingSomething5, doingSomething6, doingSomething7 }; //////////////////////////////////////////////// void setup() -{ +{ SerialDebug.begin(115200); + while (!SerialDebug && millis() < 5000); - SerialDebug.print(F("\nStarting ISR_8_PWMs_Array on ")); SerialDebug.println(BOARD_NAME); + SerialDebug.print(F("\nStarting ISR_8_PWMs_Array on ")); + SerialDebug.println(BOARD_NAME); SerialDebug.println(DX_SLOW_PWM_VERSION); - SerialDebug.print(F("CPU Frequency = ")); SerialDebug.print(F_CPU / 1000000); SerialDebug.println(F(" MHz")); - SerialDebug.print(F("Max number PWM channels = ")); SerialDebug.println(MAX_NUMBER_CHANNELS); + SerialDebug.print(F("CPU Frequency = ")); + SerialDebug.print(F_CPU / 1000000); + SerialDebug.println(F(" MHz")); + SerialDebug.print(F("Max number PWM channels = ")); + SerialDebug.println(MAX_NUMBER_CHANNELS); - SerialDebug.print(F("TCB Clock Frequency = ")); + SerialDebug.print(F("TCB Clock Frequency = ")); -#if USING_FULL_CLOCK +#if USING_FULL_CLOCK SerialDebug.println(F("Full clock (24/16MHz, etc) for highest accuracy")); -#elif USING_HALF_CLOCK +#elif USING_HALF_CLOCK SerialDebug.println(F("Half clock (12/8MHz, etc.) for high accuracy")); #else SerialDebug.println(F("250KHz for lower accuracy but longer time")); @@ -216,24 +221,26 @@ void setup() if (CurrentTimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); - + #else CurrentTimer.init(); if (CurrentTimer.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); #endif // USING_HW_TIMER_INTERVAL_MS - + // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary // You can use up to 16 timer for each ISR_PWM diff --git a/examples/ISR_8_PWMs_Array_Complex/ISR_8_PWMs_Array_Complex.ino b/examples/ISR_8_PWMs_Array_Complex/ISR_8_PWMs_Array_Complex.ino index fdfa81e..941721d 100644 --- a/examples/ISR_8_PWMs_Array_Complex/ISR_8_PWMs_Array_Complex.ino +++ b/examples/ISR_8_PWMs_Array_Complex/ISR_8_PWMs_Array_Complex.ino @@ -5,7 +5,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM Licensed under MIT license - + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers @@ -25,9 +25,9 @@ // Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. #define _PWM_LOGLEVEL_ 1 -#if defined(__AVR_AVR128DA48__) +#if defined(__AVR_AVR128DA48__) #define SerialDebug Serial1 -#elif defined(__AVR_AVR128DB48__) +#elif defined(__AVR_AVR128DB48__) #define SerialDebug Serial3 #else // standard Serial @@ -64,7 +64,7 @@ #elif USE_TIMER_4 #define CurrentTimer ITimer4 #else - #error You must select one Timer + #error You must select one Timer #endif #define USING_MICROS_RESOLUTION true //false @@ -83,9 +83,9 @@ // To modify according to your board // For Curiosity Nano AVR128DA48 => PIN_PC6 // For Curiosity Nano AVR128DB48 => PIN_PB3 - #if defined(__AVR_AVR128DA48__) + #if defined(__AVR_AVR128DA48__) #define LED_BUILTIN PIN_PC6 // PIN_PB3, 13 - #elif defined(__AVR_AVR128DB48__) + #elif defined(__AVR_AVR128DB48__) #define LED_BUILTIN PIN_PB3 // PIN_PC6, 13 #else // standard Arduino pin 13 @@ -93,9 +93,9 @@ #endif #endif -#if defined(__AVR_AVR128DA48__) +#if defined(__AVR_AVR128DA48__) #define SerialDebug Serial1 -#elif defined(__AVR_AVR128DB48__) +#elif defined(__AVR_AVR128DB48__) #define SerialDebug Serial3 #else // standard Serial @@ -377,42 +377,52 @@ void simpleTimerDoingSomething2s() unsigned long currMicros = micros(); - SerialDebug.print(F("SimpleTimer (us): ")); SerialDebug.print(SIMPLE_TIMER_MS); - SerialDebug.print(F(", us : ")); SerialDebug.print(currMicros); - SerialDebug.print(F(", Dus : ")); SerialDebug.println(currMicros - previousMicrosStart); + SerialDebug.print(F("SimpleTimer (us): ")); + SerialDebug.print(SIMPLE_TIMER_MS); + SerialDebug.print(F(", us : ")); + SerialDebug.print(currMicros); + SerialDebug.print(F(", Dus : ")); + SerialDebug.println(currMicros - previousMicrosStart); for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++) { #if USE_COMPLEX_STRUCT - SerialDebug.print(F("PWM Channel : ")); SerialDebug.print(i); + SerialDebug.print(F("PWM Channel : ")); + SerialDebug.print(i); SerialDebug.print(F(", prog Period (ms): ")); SerialDebug.print(1000.f / curISR_PWM_Data[i].PWM_Freq); - SerialDebug.print(F(", actual : ")); SerialDebug.print((uint32_t) curISR_PWM_Data[i].deltaMicrosStart); + SerialDebug.print(F(", actual : ")); + SerialDebug.print((uint32_t) curISR_PWM_Data[i].deltaMicrosStart); SerialDebug.print(F(", prog DutyCycle : ")); SerialDebug.print(curISR_PWM_Data[i].PWM_DutyCycle); - SerialDebug.print(F(", actual : ")); SerialDebug.println((float) curISR_PWM_Data[i].deltaMicrosStop * 100.0f / curISR_PWM_Data[i].deltaMicrosStart); + SerialDebug.print(F(", actual : ")); + SerialDebug.println((float) curISR_PWM_Data[i].deltaMicrosStop * 100.0f / curISR_PWM_Data[i].deltaMicrosStart); //SerialDebug.print(F(", actual deltaMicrosStop : ")); SerialDebug.println(curISR_PWM_Data[i].deltaMicrosStop); //SerialDebug.print(F(", actual deltaMicrosStart : ")); SerialDebug.println(curISR_PWM_Data[i].deltaMicrosStart); #else - SerialDebug.print(F("PWM Channel : ")); SerialDebug.print(i); + SerialDebug.print(F("PWM Channel : ")); + SerialDebug.print(i); SerialDebug.print(1000 / PWM_Freq[i]); - SerialDebug.print(F(", prog. Period (us): ")); SerialDebug.print(PWM_Period[i]); - SerialDebug.print(F(", actual : ")); SerialDebug.print(deltaMicrosStart[i]); + SerialDebug.print(F(", prog. Period (us): ")); + SerialDebug.print(PWM_Period[i]); + SerialDebug.print(F(", actual : ")); + SerialDebug.print(deltaMicrosStart[i]); SerialDebug.print(F(", prog DutyCycle : ")); SerialDebug.print(PWM_DutyCycle[i]); - SerialDebug.print(F(", actual : ")); SerialDebug.println( (float) deltaMicrosStop[i] * 100.0f / deltaMicrosStart[i]); + SerialDebug.print(F(", actual : ")); + SerialDebug.println( (float) deltaMicrosStop[i] * 100.0f / deltaMicrosStart[i]); //SerialDebug.print(F(", actual deltaMicrosStop : ")); SerialDebug.println(deltaMicrosStop[i]); //SerialDebug.print(F(", actual deltaMicrosStart : ")); SerialDebug.println(deltaMicrosStart[i]); #endif @@ -424,18 +434,23 @@ void simpleTimerDoingSomething2s() void setup() { SerialDebug.begin(115200); + while (!SerialDebug && millis() < 5000); - SerialDebug.print(F("\nStarting ISR_8_PWMs_Array_Complex on ")); SerialDebug.println(BOARD_NAME); + SerialDebug.print(F("\nStarting ISR_8_PWMs_Array_Complex on ")); + SerialDebug.println(BOARD_NAME); SerialDebug.println(DX_SLOW_PWM_VERSION); - SerialDebug.print(F("CPU Frequency = ")); SerialDebug.print(F_CPU / 1000000); SerialDebug.println(F(" MHz")); - SerialDebug.print(F("Max number PWM channels = ")); SerialDebug.println(MAX_NUMBER_CHANNELS); + SerialDebug.print(F("CPU Frequency = ")); + SerialDebug.print(F_CPU / 1000000); + SerialDebug.println(F(" MHz")); + SerialDebug.print(F("Max number PWM channels = ")); + SerialDebug.println(MAX_NUMBER_CHANNELS); - SerialDebug.print(F("TCB Clock Frequency = ")); + SerialDebug.print(F("TCB Clock Frequency = ")); -#if USING_FULL_CLOCK +#if USING_FULL_CLOCK SerialDebug.println(F("Full clock (24/16MHz, etc) for highest accuracy")); -#elif USING_HALF_CLOCK +#elif USING_HALF_CLOCK SerialDebug.println(F("Half clock (12/8MHz, etc.) for high accuracy")); #else SerialDebug.println(F("250KHz for lower accuracy but longer time")); @@ -447,18 +462,20 @@ void setup() if (CurrentTimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); - + #else CurrentTimer.init(); if (CurrentTimer.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); diff --git a/examples/ISR_8_PWMs_Array_Simple/ISR_8_PWMs_Array_Simple.ino b/examples/ISR_8_PWMs_Array_Simple/ISR_8_PWMs_Array_Simple.ino index 183a701..03e3f2b 100644 --- a/examples/ISR_8_PWMs_Array_Simple/ISR_8_PWMs_Array_Simple.ino +++ b/examples/ISR_8_PWMs_Array_Simple/ISR_8_PWMs_Array_Simple.ino @@ -5,7 +5,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM Licensed under MIT license - + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers @@ -25,9 +25,9 @@ // Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. #define _PWM_LOGLEVEL_ 1 -#if defined(__AVR_AVR128DA48__) +#if defined(__AVR_AVR128DA48__) #define SerialDebug Serial1 -#elif defined(__AVR_AVR128DB48__) +#elif defined(__AVR_AVR128DB48__) #define SerialDebug Serial3 #else // standard Serial @@ -64,7 +64,7 @@ #elif USE_TIMER_4 #define CurrentTimer ITimer4 #else - #error You must select one Timer + #error You must select one Timer #endif #define USING_MICROS_RESOLUTION true //false @@ -81,9 +81,9 @@ // To modify according to your board // For Curiosity Nano AVR128DA48 => PIN_PC6 // For Curiosity Nano AVR128DB48 => PIN_PB3 - #if defined(__AVR_AVR128DA48__) + #if defined(__AVR_AVR128DA48__) #define LED_BUILTIN PIN_PC6 // PIN_PB3, 13 - #elif defined(__AVR_AVR128DB48__) + #elif defined(__AVR_AVR128DB48__) #define LED_BUILTIN PIN_PB3 // PIN_PC6, 13 #else // standard Arduino pin 13 @@ -105,7 +105,7 @@ DX_SLOW_PWM_ISR ISR_PWM; ////////////////////////////////////////////////////// void TimerHandler() -{ +{ ISR_PWM.run(); } @@ -146,18 +146,23 @@ float PWM_DutyCycle[] = void setup() { SerialDebug.begin(115200); + while (!SerialDebug && millis() < 5000); - SerialDebug.print(F("\nStarting ISR_8_PWMs_Array_Simple on ")); SerialDebug.println(BOARD_NAME); + SerialDebug.print(F("\nStarting ISR_8_PWMs_Array_Simple on ")); + SerialDebug.println(BOARD_NAME); SerialDebug.println(DX_SLOW_PWM_VERSION); - SerialDebug.print(F("CPU Frequency = ")); SerialDebug.print(F_CPU / 1000000); SerialDebug.println(F(" MHz")); - SerialDebug.print(F("Max number PWM channels = ")); SerialDebug.println(MAX_NUMBER_CHANNELS); + SerialDebug.print(F("CPU Frequency = ")); + SerialDebug.print(F_CPU / 1000000); + SerialDebug.println(F(" MHz")); + SerialDebug.print(F("Max number PWM channels = ")); + SerialDebug.println(MAX_NUMBER_CHANNELS); - SerialDebug.print(F("TCB Clock Frequency = ")); + SerialDebug.print(F("TCB Clock Frequency = ")); -#if USING_FULL_CLOCK +#if USING_FULL_CLOCK SerialDebug.println(F("Full clock (24/16MHz, etc) for highest accuracy")); -#elif USING_HALF_CLOCK +#elif USING_HALF_CLOCK SerialDebug.println(F("Half clock (12/8MHz, etc.) for high accuracy")); #else SerialDebug.println(F("250KHz for lower accuracy but longer time")); @@ -169,18 +174,20 @@ void setup() if (CurrentTimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); - + #else CurrentTimer.init(); if (CurrentTimer.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); diff --git a/examples/ISR_Changing_PWM/ISR_Changing_PWM.ino b/examples/ISR_Changing_PWM/ISR_Changing_PWM.ino index bb76dc2..a9c006c 100644 --- a/examples/ISR_Changing_PWM/ISR_Changing_PWM.ino +++ b/examples/ISR_Changing_PWM/ISR_Changing_PWM.ino @@ -5,7 +5,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM Licensed under MIT license - + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers @@ -25,9 +25,9 @@ // Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. #define _PWM_LOGLEVEL_ 1 -#if defined(__AVR_AVR128DA48__) +#if defined(__AVR_AVR128DA48__) #define SerialDebug Serial1 -#elif defined(__AVR_AVR128DB48__) +#elif defined(__AVR_AVR128DB48__) #define SerialDebug Serial3 #else // standard Serial @@ -64,7 +64,7 @@ #elif USE_TIMER_4 #define CurrentTimer ITimer4 #else - #error You must select one Timer + #error You must select one Timer #endif #define USING_MICROS_RESOLUTION true //false @@ -81,9 +81,9 @@ // To modify according to your board // For Curiosity Nano AVR128DA48 => PIN_PC6 // For Curiosity Nano AVR128DB48 => PIN_PB3 - #if defined(__AVR_AVR128DA48__) + #if defined(__AVR_AVR128DA48__) #define LED_BUILTIN PIN_PC6 // PIN_PB3, 13 - #elif defined(__AVR_AVR128DB48__) + #elif defined(__AVR_AVR128DB48__) #define LED_BUILTIN PIN_PB3 // PIN_PC6, 13 #else // standard Arduino pin 13 @@ -105,7 +105,7 @@ DX_SLOW_PWM_ISR ISR_PWM; ////////////////////////////////////////////////////// void TimerHandler() -{ +{ ISR_PWM.run(); } @@ -119,9 +119,9 @@ void TimerHandler() uint32_t PWM_Pin = LED_BUILTIN; // You can assign any interval for any timer here, in Hz -float PWM_Freq1 = 1.0f; +float PWM_Freq1 = 10.0f; // You can assign any interval for any timer here, in Hz -float PWM_Freq2 = 2.0f; +float PWM_Freq2 = 20.0f; // You can assign any interval for any timer here, in microseconds uint32_t PWM_Period1 = 1000000 / PWM_Freq1; @@ -141,18 +141,23 @@ int channelNum; void setup() { SerialDebug.begin(115200); + while (!SerialDebug && millis() < 5000); - SerialDebug.print(F("\nStarting ISR_Changing_PWM on ")); SerialDebug.println(BOARD_NAME); + SerialDebug.print(F("\nStarting ISR_Changing_PWM on ")); + SerialDebug.println(BOARD_NAME); SerialDebug.println(DX_SLOW_PWM_VERSION); - SerialDebug.print(F("CPU Frequency = ")); SerialDebug.print(F_CPU / 1000000); SerialDebug.println(F(" MHz")); - SerialDebug.print(F("Max number PWM channels = ")); SerialDebug.println(MAX_NUMBER_CHANNELS); + SerialDebug.print(F("CPU Frequency = ")); + SerialDebug.print(F_CPU / 1000000); + SerialDebug.println(F(" MHz")); + SerialDebug.print(F("Max number PWM channels = ")); + SerialDebug.println(MAX_NUMBER_CHANNELS); - SerialDebug.print(F("TCB Clock Frequency = ")); + SerialDebug.print(F("TCB Clock Frequency = ")); -#if USING_FULL_CLOCK +#if USING_FULL_CLOCK SerialDebug.println(F("Full clock (24/16MHz, etc) for highest accuracy")); -#elif USING_HALF_CLOCK +#elif USING_HALF_CLOCK SerialDebug.println(F("Half clock (12/8MHz, etc.) for high accuracy")); #else SerialDebug.println(F("250KHz for lower accuracy but longer time")); @@ -164,18 +169,20 @@ void setup() if (CurrentTimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); - + #else CurrentTimer.init(); if (CurrentTimer.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); @@ -185,8 +192,10 @@ void setup() void loop() { - SerialDebug.print(F("Using PWM Freq = ")); SerialDebug.print(PWM_Freq1); - SerialDebug.print(F(", PWM DutyCycle = ")); SerialDebug.println(PWM_DutyCycle1); + SerialDebug.print(F("Using PWM Freq = ")); + SerialDebug.print(PWM_Freq1); + SerialDebug.print(F(", PWM DutyCycle = ")); + SerialDebug.println(PWM_DutyCycle1); #if USING_PWM_FREQUENCY @@ -207,7 +216,10 @@ void loop() ISR_PWM.deleteChannel((unsigned) channelNum); - SerialDebug.print(F("Using PWM Freq = ")); SerialDebug.print(PWM_Freq2); SerialDebug.print(F(", PWM DutyCycle = ")); SerialDebug.println(PWM_DutyCycle2); + SerialDebug.print(F("Using PWM Freq = ")); + SerialDebug.print(PWM_Freq2); + SerialDebug.print(F(", PWM DutyCycle = ")); + SerialDebug.println(PWM_DutyCycle2); #if USING_PWM_FREQUENCY diff --git a/examples/ISR_Modify_PWM/ISR_Modify_PWM.ino b/examples/ISR_Modify_PWM/ISR_Modify_PWM.ino index 41e5b06..f14f85a 100644 --- a/examples/ISR_Modify_PWM/ISR_Modify_PWM.ino +++ b/examples/ISR_Modify_PWM/ISR_Modify_PWM.ino @@ -5,7 +5,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM Licensed under MIT license - + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers @@ -25,9 +25,9 @@ // Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. #define _PWM_LOGLEVEL_ 1 -#if defined(__AVR_AVR128DA48__) +#if defined(__AVR_AVR128DA48__) #define SerialDebug Serial1 -#elif defined(__AVR_AVR128DB48__) +#elif defined(__AVR_AVR128DB48__) #define SerialDebug Serial3 #else // standard Serial @@ -64,7 +64,7 @@ #elif USE_TIMER_4 #define CurrentTimer ITimer4 #else - #error You must select one Timer + #error You must select one Timer #endif #define USING_MICROS_RESOLUTION true //false @@ -81,9 +81,9 @@ // To modify according to your board // For Curiosity Nano AVR128DA48 => PIN_PC6 // For Curiosity Nano AVR128DB48 => PIN_PB3 - #if defined(__AVR_AVR128DA48__) + #if defined(__AVR_AVR128DA48__) #define LED_BUILTIN PIN_PC6 // PIN_PB3, 13 - #elif defined(__AVR_AVR128DB48__) + #elif defined(__AVR_AVR128DB48__) #define LED_BUILTIN PIN_PB3 // PIN_PC6, 13 #else // standard Arduino pin 13 @@ -105,7 +105,7 @@ DX_SLOW_PWM_ISR ISR_PWM; ////////////////////////////////////////////////////// void TimerHandler() -{ +{ ISR_PWM.run(); } @@ -141,18 +141,23 @@ int channelNum; void setup() { SerialDebug.begin(115200); + while (!SerialDebug && millis() < 5000); - SerialDebug.print(F("\nStarting ISR_Modify_PWM on ")); SerialDebug.println(BOARD_NAME); + SerialDebug.print(F("\nStarting ISR_Modify_PWM on ")); + SerialDebug.println(BOARD_NAME); SerialDebug.println(DX_SLOW_PWM_VERSION); - SerialDebug.print(F("CPU Frequency = ")); SerialDebug.print(F_CPU / 1000000); SerialDebug.println(F(" MHz")); - SerialDebug.print(F("Max number PWM channels = ")); SerialDebug.println(MAX_NUMBER_CHANNELS); + SerialDebug.print(F("CPU Frequency = ")); + SerialDebug.print(F_CPU / 1000000); + SerialDebug.println(F(" MHz")); + SerialDebug.print(F("Max number PWM channels = ")); + SerialDebug.println(MAX_NUMBER_CHANNELS); - SerialDebug.print(F("TCB Clock Frequency = ")); + SerialDebug.print(F("TCB Clock Frequency = ")); -#if USING_FULL_CLOCK +#if USING_FULL_CLOCK SerialDebug.println(F("Full clock (24/16MHz, etc) for highest accuracy")); -#elif USING_HALF_CLOCK +#elif USING_HALF_CLOCK SerialDebug.println(F("Half clock (12/8MHz, etc.) for high accuracy")); #else SerialDebug.println(F("250KHz for lower accuracy but longer time")); @@ -164,25 +169,30 @@ void setup() if (CurrentTimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); - + #else CurrentTimer.init(); if (CurrentTimer.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler)) { - SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); SerialDebug.println(micros()); + SerialDebug.print(F("Starting ITimer1 OK, micros() = ")); + SerialDebug.println(micros()); } else SerialDebug.println(F("Can't set CurrentTimer. Select another freq. or timer")); #endif // USING_HW_TIMER_INTERVAL_MS - SerialDebug.print(F("Using PWM Freq = ")); SerialDebug.print(PWM_Freq1); SerialDebug.print(F(", PWM DutyCycle = ")); SerialDebug.println(PWM_DutyCycle1); + SerialDebug.print(F("Using PWM Freq = ")); + SerialDebug.print(PWM_Freq1); + SerialDebug.print(F(", PWM DutyCycle = ")); + SerialDebug.println(PWM_DutyCycle1); #if USING_PWM_FREQUENCY diff --git a/examples/multiFileProject/multiFileProject.cpp b/examples/multiFileProject/multiFileProject.cpp index cc82677..8a6afe5 100644 --- a/examples/multiFileProject/multiFileProject.cpp +++ b/examples/multiFileProject/multiFileProject.cpp @@ -1,6 +1,6 @@ /**************************************************************************************************************************** multiFileProject.cpp - + For Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore Written by Khoi Hoang diff --git a/examples/multiFileProject/multiFileProject.h b/examples/multiFileProject/multiFileProject.h index 6479e21..490f108 100644 --- a/examples/multiFileProject/multiFileProject.h +++ b/examples/multiFileProject/multiFileProject.h @@ -1,6 +1,6 @@ /**************************************************************************************************************************** multiFileProject.h - + For Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore Written by Khoi Hoang @@ -12,9 +12,9 @@ #pragma once -#if defined(__AVR_AVR128DA48__) +#if defined(__AVR_AVR128DA48__) #define SerialDebug Serial1 -#elif defined(__AVR_AVR128DB48__) +#elif defined(__AVR_AVR128DB48__) #define SerialDebug Serial3 #else // standard Serial diff --git a/examples/multiFileProject/multiFileProject.ino b/examples/multiFileProject/multiFileProject.ino index b0c7b31..517e2b3 100644 --- a/examples/multiFileProject/multiFileProject.ino +++ b/examples/multiFileProject/multiFileProject.ino @@ -1,6 +1,6 @@ /**************************************************************************************************************************** multiFileProject.ino - + For Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore Written by Khoi Hoang @@ -23,24 +23,28 @@ // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include "Dx_Slow_PWM.h" -void setup() +void setup() { SerialDebug.begin(115200); + while (!SerialDebug && millis() < 5000); - SerialDebug.print(F("\nStarting multiFileProject on ")); SerialDebug.println(BOARD_NAME); + SerialDebug.print(F("\nStarting multiFileProject on ")); + SerialDebug.println(BOARD_NAME); SerialDebug.println(DX_SLOW_PWM_VERSION); #if defined(DX_SLOW_PWM_VERSION_MIN) + if (DX_SLOW_PWM_VERSION_INT < DX_SLOW_PWM_VERSION_MIN) { SerialDebug.print("Warning. Must use this example on Version equal or later than : "); SerialDebug.println(DX_SLOW_PWM_VERSION_MIN_TARGET); } + #endif } -void loop() +void loop() { // put your main code here, to run repeatedly: } diff --git a/src/Dx_Slow_PWM.h b/src/Dx_Slow_PWM.h index b1ce9ac..4a40366 100644 --- a/src/Dx_Slow_PWM.h +++ b/src/Dx_Slow_PWM.h @@ -5,7 +5,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM Licensed under MIT license - + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers diff --git a/src/Dx_Slow_PWM_ISR.h b/src/Dx_Slow_PWM_ISR.h index 694c268..6c76931 100644 --- a/src/Dx_Slow_PWM_ISR.h +++ b/src/Dx_Slow_PWM_ISR.h @@ -5,7 +5,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM Licensed under MIT license - + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers diff --git a/src/Dx_Slow_PWM_ISR_Impl.h b/src/Dx_Slow_PWM_ISR_Impl.h index 046398d..08b9fda 100644 --- a/src/Dx_Slow_PWM_ISR_Impl.h +++ b/src/Dx_Slow_PWM_ISR_Impl.h @@ -5,7 +5,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/Dx_Slow_PWM Licensed under MIT license - + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers @@ -28,19 +28,19 @@ #include