diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a7cb3c4..e499564 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,6 +10,8 @@ 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/SAMDUE_PWM/issues/new). +--- + ### How to submit a bug report Please ensure to specify the following: @@ -23,13 +25,17 @@ Please ensure to specify the following: * Network configuration +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 SAMDUE Core Version 1.6.12 OS: Ubuntu 20.04 LTS -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 +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 the Timer Interrupt. diff --git a/README.md b/README.md index df90f28..c796edf 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Donate to my libraries using BuyMeACoffee + --- --- @@ -38,6 +39,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 using PWM on SAM_DUE](#1-PWM_DynamicDutyCycle-using-PWM-on-SAM_DUE) @@ -272,6 +274,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** --- @@ -294,7 +297,7 @@ The following is the sample terminal output when running example [PWM_DynamicDut ```cpp Starting PWM_DynamicDutyCycle using PWM on SAM_DUE -SAMDUE_PWM v1.0.0 +SAMDUE_PWM v1.0.1 [PWM] setupPWM: _pin = 6 , _pinAttr = 12 , frequency = 5000 , _dutycycle = 0 [PWM] setupPWM: Not PWMEnabled, PWMC_ConfigureClocks, frequency = 5000 [PWM] setupPWM: new _pin = 6 , _channel = 7 @@ -342,7 +345,7 @@ The following is the sample terminal output when running example [**PWM_Multi**] ```cpp Starting PWM_Multi using Timer on SAM_DUE -SAMDUE_PWM v1.0.0 +SAMDUE_PWM v1.0.1 [PWM] setupPWM Timer: _pin = 2 , _pinAttr = 20 , frequency = 2000 , dutycycle = 2058 [PWM] setupPWM new Timer, frequency = 2000 [PWM] setupPWM Timer: _pin = 2 , _pinAttr = 20 , frequency = 2000 , dutycycle = 2058 @@ -384,7 +387,7 @@ The following is the sample terminal output when running example [**PWM_DynamicF ```cpp Starting PWM_DynamicFreq using Timer on SAM_DUE -SAMDUE_PWM v1.0.0 +SAMDUE_PWM v1.0.1 [PWM] setupPWM Timer: _pin = 5 , _pinAttr = 20 , frequency = 10000 , dutycycle = 2108 [PWM] setupPWM new Timer, frequency = 10000 ===================================================================================== @@ -424,7 +427,7 @@ The following is the sample terminal output when running example [**PWM_Waveform ```cpp Starting PWM_Waveform using PWM on SAM_DUE -SAMDUE_PWM v1.0.0 +SAMDUE_PWM v1.0.1 [PWM] setupPWM: _pinAttr = 12 , frequency = 2000 , _dutycycle = 0 [PWM] setupPWM: Not PWMEnabled, PWMC_ConfigureClocks, frequency = 2000 [PWM] setupPWM: new _pin = 6 , _channel = 7 @@ -528,7 +531,7 @@ The following is the sample terminal output when running example [**PWM_Waveform ```cpp Starting PWM_Waveform using Timer on SAM_DUE -SAMDUE_PWM v1.0.0 +SAMDUE_PWM v1.0.1 [PWM] setupPWM Timer: _pin = 5 , _pinAttr = 20 , frequency = 2000 , dutycycle = 0 [PWM] setupPWM new Timer, frequency = 2000 [PWM] setPWM: _dutycycle = 0 , frequency = 2000.00 @@ -622,6 +625,8 @@ Submit issues to: [SAMDUE_PWM issues](https://github.com/khoih-prog/SAMDUE_PWM/i ## DONE 1. Basic hardware PWM-channels for **SAM_DUE boards** using [`Arduino SAM core`](https://github.com/arduino/ArduinoCore-sam) + 2. Add example [PWM_StepperControl](https://github.com/khoih-prog/SAMDUE_PWM/tree/main/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM + --- --- @@ -630,6 +635,15 @@ Submit issues to: [SAMDUE_PWM issues](https://github.com/khoih-prog/SAMDUE_PWM/i 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 11355f8..dda3681 100644 --- a/changelog.md +++ b/changelog.md @@ -17,6 +17,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.0.1](#Releases-v101) * [Initial Releases v1.0.0](#Initial-Releases-v100) --- @@ -24,6 +25,10 @@ ## Changelog +### Releases v1.0.1 + +1. Add example [PWM_StepperControl](https://github.com/khoih-prog/SAMDUE_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) + ### Initial Releases v1.0.0 1. Initial coding to support **SAM_DUE**, etc. using [`Arduino SAM core`](https://github.com/arduino/ArduinoCore-sam) diff --git a/examples/PWM_StepperControl/PWM_StepperControl.ino b/examples/PWM_StepperControl/PWM_StepperControl.ino new file mode 100644 index 0000000..bc5b58c --- /dev/null +++ b/examples/PWM_StepperControl/PWM_StepperControl.ino @@ -0,0 +1,87 @@ +/**************************************************************************************************************************** + PWM_StepperControl.ino + For SAM_DUE boards using hardware-based PWM + Written by Khoi Hoang + + Built by Khoi Hoang https://github.com/khoih-prog/SAMDUE_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 + +// Select false to use PWM +#define USING_TIMER true + +#include "SAMDUE_PWM.h" + +// SAM_DUE: +// PWM pins: 6, 7, 8, 9 +// Timer pins: 2-5, 10-13. +// pin2: TC0_CHA0, pin3: TC2_CHA7, pin4: TC2_CHB6, pin5: TC2_CHA6 +// pin 10: TC2_CHB7, pin11: TC2_CHA8, pin12: TC2_CHB8, pin13: TC0_CHB0 + +#if USING_TIMER + #define STEP_PIN 5 +#else + #define STEP_PIN 6 +#endif + +SAMDUE_PWM* stepper; + +#define DIR_PIN 9 + +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(SAMDUE_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 SAMDUE_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 85e6083..b4b593c 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "SAMDUE_PWM", - "version": "1.0.0", + "version": "1.0.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, sam, due, sam-due, sam3x8e, atmel-sam", "description": "This library enables you to use Hardware-based PWM channels on SAMDUE boards to create and output PWM to pins. 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", "authors": diff --git a/library.properties b/library.properties index 69039ab..385811e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SAMDUE_PWM -version=1.0.0 +version=1.0.1 author=Khoi Hoang maintainer=Khoi Hoang sentence=This library enables you to use Hardware-based PWM channels on SAMDUE boards to create and output PWM to pins. diff --git a/src/PWM_Generic_Debug.h b/src/PWM_Generic_Debug.h index 44a50fe..b0b7353 100644 --- a/src/PWM_Generic_Debug.h +++ b/src/PWM_Generic_Debug.h @@ -8,11 +8,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/SAMDUE_PWM Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 04/11/2022 Initial coding for SAM_DUE boards + 1.0.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once diff --git a/src/SAMDUE_PWM.h b/src/SAMDUE_PWM.h index 24b56f7..db77e02 100644 --- a/src/SAMDUE_PWM.h +++ b/src/SAMDUE_PWM.h @@ -8,11 +8,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/SAMDUE_PWM Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 04/11/2022 Initial coding for SAM_DUE boards + 1.0.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once @@ -37,13 +38,13 @@ #include "Arduino.h" #ifndef SAMDUE_PWM_VERSION - #define SAMDUE_PWM_VERSION "SAMDUE_PWM v1.0.0" + #define SAMDUE_PWM_VERSION "SAMDUE_PWM v1.0.1" #define SAMDUE_PWM_VERSION_MAJOR 1 #define SAMDUE_PWM_VERSION_MINOR 0 - #define SAMDUE_PWM_VERSION_PATCH 0 + #define SAMDUE_PWM_VERSION_PATCH 1 - #define SAMDUE_PWM_VERSION_INT 1000000 + #define SAMDUE_PWM_VERSION_INT 1000001 #endif #include "PWM_Generic_Debug.h"