diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 363a1f4..cc312b9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,12 +10,14 @@ However, before reporting a bug please check through the following: If you don't find anything, please [open a new issue](https://github.com/khoih-prog/Dx_PWM/issues/new). +--- + ### How to submit a bug report Please ensure to specify the following: * Arduino IDE version (e.g. 1.8.19) or Platform.io version -* `DxCore` Core Version (e.g. Arduino DxCore core v1.5.1) +* `DxCore` Core Version (e.g. Arduino DxCore core v1.5.3) * Board (e.g. AVR128DA64, AVR128DB48, AVR64DB32, etc.) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce @@ -25,13 +27,16 @@ Please ensure to specify the following: Please be educated, civilized and constructive. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted. -### Example +Please be educated, civilized and constructive as you've always been. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted. +--- + +### Example ``` Arduino IDE version: 1.8.19 -Arduino DxCore core v1.5.1 +Arduino DxCore core v1.5.3 Board: Curiosity AVR128DB48 -Linux xy-Inspiron-3593 5.15.0-56-generic #62~20.04.1-Ubuntu SMP Tue Nov 22 21:24:20 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux Context: I encountered a crash while trying to use this library diff --git a/README.md b/README.md index 10453ee..59cafa4 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ * [ 5. PWM_Multi](examples/PWM_Multi) * [ 6. PWM_MultiChannel](examples/PWM_MultiChannel) * [ 7. PWM_Waveform](examples/PWM_Waveform) + * [ 8. PWM_StepperControl](examples/PWM_StepperControl) **New** * [Example PWM_Multi](#example-PWM_Multi) * [Debug Terminal Output Samples](#debug-terminal-output-samples) * [1. PWM_DynamicDutyCycle on AVR128DB](#1-PWM_DynamicDutyCycle-on-AVR128DB) @@ -186,7 +187,7 @@ Functions using normal software-based PWMs, relying on loop() and calling millis

-- **AVRDD-based boards (AVR64DB, AVR32DB, AVR16DB, etc.) using DxCore v1.5.1+** +- **AVRDD-based boards (AVR64DD, AVR32DD, AVR16DD, etc.) using DxCore v1.5.1+** --- @@ -195,7 +196,7 @@ Functions using normal software-based PWMs, relying on loop() and calling millis ## Prerequisites 1. [`Arduino IDE 1.8.19+` for Arduino](https://github.com/arduino/Arduino). [![GitHub release](https://img.shields.io/github/release/arduino/Arduino.svg)](https://github.com/arduino/Arduino/releases/latest) -2. [`SpenceKonde DxCore core 1.5.1+`](https://github.com/SpenceKonde/DxCore) for Arduino AVRDx boards. [![GitHub release](https://img.shields.io/github/release/SpenceKonde/DxCore.svg)](https://github.com/SpenceKonde/DxCore/releases/latest). Follow [**DxCore Installation**](https://github.com/SpenceKonde/DxCore/blob/main/Installation.md). +2. [`SpenceKonde DxCore core 1.5.3+`](https://github.com/SpenceKonde/DxCore) for Arduino AVRDx boards. [![GitHub release](https://img.shields.io/github/release/SpenceKonde/DxCore.svg)](https://github.com/SpenceKonde/DxCore/releases/latest). Follow [**DxCore Installation**](https://github.com/SpenceKonde/DxCore/blob/main/Installation.md). --- @@ -357,6 +358,7 @@ PWM_Instance->setPWM_manual(PWM_Pins, new_level); 5. [PWM_Multi](examples/PWM_Multi) 6. [PWM_MultiChannel](examples/PWM_MultiChannel) 7. [PWM_Waveform](examples/PWM_Waveform) + 8. [PWM_StepperControl](examples/PWM_StepperControl) **New** --- @@ -380,7 +382,7 @@ The following is the sample terminal output when running example [PWM_DynamicDut ```cpp Starting PWM_DynamicDutyCycle on AVR128DB -Dx_PWM v1.1.0 +Dx_PWM v1.1.1 [PWM] Dx_PWM: freq = 5000.00 [PWM] Dx_PWM: _dutycycle = 0 ===================================================================================== @@ -439,7 +441,7 @@ The following is the sample terminal output when running example [**PWM_Multi**] ```cpp Starting PWM_Multi on AVR128DB -Dx_PWM v1.1.0 +Dx_PWM v1.1.1 [PWM] Dx_PWM: freq = 2000.00 [PWM] Dx_PWM: _dutycycle = 13107 [PWM] setPWM_Int: pin = 10 , _dutycycle = 13107 , old frequency = 2000.00 @@ -499,12 +501,12 @@ The following is the sample terminal output when running example [**PWM_DynamicF ```cpp Starting PWM_DynamicFreq on AVR128DB -Dx_PWM v1.1.0 +Dx_PWM v1.1.1 [PWM] Dx_PWM: freq = 10000.00 [PWM] Dx_PWM: _dutycycle = 32767 Stop here forever Starting PWM_DynamicFreq on AVR128DB -Dx_PWM v1.1.0 +Dx_PWM v1.1.1 [PWM] Dx_PWM: freq = 10000.00 [PWM] Dx_PWM: _dutycycle = 32767 ===================================================================================== @@ -551,7 +553,7 @@ The following is the sample terminal output when running example [**PWM_Waveform ```cpp Starting PWM_Waveform on AVR128DB -Dx_PWM v1.1.0 +Dx_PWM v1.1.1 [PWM] Dx_PWM: freq = 2000.00 [PWM] Dx_PWM: _dutycycle = 0 [PWM] setPWM: _dutycycle = 0 @@ -615,7 +617,7 @@ The following is the sample terminal output when running example [**PWM_Waveform ```cpp Starting PWM_Waveform on AVR128DA -Dx_PWM v1.1.0 +Dx_PWM v1.1.1 [PWM] Dx_PWM: freq = 2000.00 [PWM] Dx_PWM: _dutycycle = 0 [PWM] setPWM: _dutycycle = 0 @@ -711,9 +713,9 @@ Submit issues to: [Dx_PWM issues](https://github.com/khoih-prog/Dx_PWM/issues) ## DONE 1. Basic hardware-based multi-channel PWMs for **AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore** - 2. Add support to AVRDD (AVR64DD, AVR32DDx, AVR16DD, etc.) - 3. Modify to use either breaking DxCore v1.5.1+ or v1.4.10- - + 2. Add support to `AVRDD` (AVR64DD, AVR32DDx, AVR16DD, etc.) + 3. Modify to use either breaking DxCore `v1.5.1+` or `v1.4.10-` + 2. Add example [PWM_StepperControl](https://github.com/khoih-prog/Dx_PWM/tree/main/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM --- --- @@ -722,6 +724,14 @@ Submit issues to: [Dx_PWM issues](https://github.com/khoih-prog/Dx_PWM/issues) Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library. +1. Thanks to [Paul van Dinther](https://github.com/dinther) for proposing new way to use PWM to drive Stepper-Motor in [Using PWM to step a stepper driver #16](https://github.com/khoih-prog/RP2040_PWM/issues/16), leading to v2.0.3 + + + + + + +
dinther
Paul van Dinther

--- diff --git a/changelog.md b/changelog.md index 16bdcfb..c5d7e35 100644 --- a/changelog.md +++ b/changelog.md @@ -18,6 +18,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Release v1.1.1](#release-v111) * [Release v1.1.0](#release-v110) * [Initial Release v1.0.0](#initial-release-v100) @@ -26,6 +27,9 @@ ## Changelog +### Release v1.1.1 + +1. Add example [PWM_StepperControl](https://github.com/khoih-prog/Dx_PWM/tree/main/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM. Check [Using PWM to step a stepper driver #16](https://github.com/khoih-prog/RP2040_PWM/issues/16) ### Release v1.1.0 diff --git a/examples/PWM_StepperControl/PWM_StepperControl.ino b/examples/PWM_StepperControl/PWM_StepperControl.ino new file mode 100644 index 0000000..e27febf --- /dev/null +++ b/examples/PWM_StepperControl/PWM_StepperControl.ino @@ -0,0 +1,100 @@ +/**************************************************************************************************************************** + PWM_StepperControl.ino + For Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore + Written by Khoi Hoang + + Built by Khoi Hoang https://github.com/khoih-prog/Dx_PWM + Licensed under MIT license + + Credits of Paul van Dinther (https://github.com/dinther). Check https://github.com/khoih-prog/RP2040_PWM/issues/16 +*****************************************************************************************************************************/ + +// Use with Stepper-Motor driver, such as TMC2209 + +#define _PWM_LOGLEVEL_ 4 + +#if defined(__AVR_AVR128DA48__) + #define SerialDebug Serial1 +#elif defined(__AVR_AVR128DB48__) + #define SerialDebug Serial3 +#else + // standard Serial + #define SerialDebug Serial +#endif + +#define PWM_GENERIC_DEBUG_PORT SerialDebug + +#include "Dx_PWM.h" + +// On DX AVR128DB48 +// PA0-3: Not PWM +// PA4-7: TD0 => not supported yet +// PB0-5: TCA1 +// PC0-5: TCA0 +// PC6-7: Not PWM +// PD0-7: Not PWM +// PE0-3: Not PWM +// PF0-3: Not PWM +// PF4-5: TCB + +#if defined(PIN_PF5) + #define STEP_PIN PIN_PF5 +#else + #define STEP_PIN PIN_PC0 +#endif + +#define DIR_PIN PIN_PA0 + +Dx_PWM* stepper; + +void setSpeed(int speed) +{ + if (speed == 0) + { + // Use DC = 0 to stop stepper + stepper->setPWM(STEP_PIN, 500, 0); + } + else + { + // Set the frequency of the PWM output and a duty cycle of 50% + digitalWrite(DIR_PIN, (speed < 0)); + stepper->setPWM(STEP_PIN, abs(speed), 50); + } +} + +void setup() +{ + pinMode(DIR_PIN, OUTPUT); + + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + delay(100); + + Serial.print(F("\nStarting PWM_StepperControl on ")); + Serial.println(BOARD_NAME); + Serial.println(DX_PWM_VERSION); + + // Create PWM object and passed just a random frequency of 500 + // The duty cycle is how you turn the motor on and off + stepper = new Dx_PWM(STEP_PIN, 500, 0); +} + +void loop() +{ + setSpeed(1000); + delay(3000); + + // Stop before reversing + setSpeed(0); + delay(3000); + + // Reversing + setSpeed(-500); + delay(3000); + + // Stop before reversing + setSpeed(0); + delay(3000); +} diff --git a/library.json b/library.json index 682a395..4420521 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Dx_PWM", - "version": "1.1.0", + "version": "1.1.1", "keywords": "timing, device, control, timer, pwm, pwm-driver, pwm-frequency, dynamic-pwm, duty-cycle, hardware-based-pwm, multi-channel-pwm, waveform-generator, mission-critical, accuracy, non-blocking, megaavr, avr-da, avr-db, avr-dd, dxcore, avr128dx, avr64dx, avr32dx, megatinycore, dx-timerinterrupt, tcb-timers", "description": "This library enables you to use Hardware-based PWM channels on Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.), using DxCore, to create and output PWM. The most important feature is they're purely hardware-based PWM channels, supporting very high PWM frequencies. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware-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 ISR, millis() or micros(). That's necessary if you need to control devices requiring high precision. New efficient setPWM_manual function to facilitate waveform creation using PWM. Now supporting AVRDD and DxCore v1.5.1", "authors": diff --git a/library.properties b/library.properties index abeb8c8..a394a87 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Dx_PWM -version=1.1.0 +version=1.1.1 author=Khoi Hoang maintainer=Khoi Hoang sentence=This library enables you to use Hardware-based PWM channels on Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.), using DxCore, to create and output PWM. diff --git a/src/Dx_PWM.h b/src/Dx_PWM.h index 6ccf22c..9e7371c 100644 --- a/src/Dx_PWM.h +++ b/src/Dx_PWM.h @@ -9,12 +9,13 @@ This is pure hardware-based PWM - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/11/2022 Initial coding to support AVR Dx (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore 1.1.0 K.Hoang 30/12/2022 Add support to AVR DD (AVR64DD, AVR32DDx, AVR16DD, etc.) using breaking DxCore v1.5.1+ + 1.1.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once @@ -110,13 +111,13 @@ //////////////////////////////////////// #ifndef DX_PWM_VERSION - #define DX_PWM_VERSION F("Dx_PWM v1.1.0") + #define DX_PWM_VERSION F("Dx_PWM v1.1.1") #define DX_PWM_VERSION_MAJOR 1 #define DX_PWM_VERSION_MINOR 1 - #define DX_PWM_VERSION_PATCH 0 + #define DX_PWM_VERSION_PATCH 1 - #define DX_PWM_VERSION_INT 1001000 + #define DX_PWM_VERSION_INT 1001001 #endif //////////////////////////////////////// diff --git a/src/PWM_Generic_Debug.h b/src/PWM_Generic_Debug.h index d5de5d4..5cd4f22 100644 --- a/src/PWM_Generic_Debug.h +++ b/src/PWM_Generic_Debug.h @@ -9,12 +9,13 @@ This is pure hardware-based PWM - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/11/2022 Initial coding to support AVR Dx (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore 1.1.0 K.Hoang 30/12/2022 Add support to AVR DD (AVR64DD, AVR32DDx, AVR16DD, etc.) using breaking DxCore v1.5.1+ + 1.1.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once