diff --git a/README.md b/README.md index c8a26c1..eaf1694 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ * [Examples](#examples) * [ 1. PWM_Multi](examples/PWM_Multi) * [ 2. PWM_Single](examples/PWM_Single) - * [ 3. multiFileProject](examples/multiFileProject). **New** + * [ 3. multiFileProject](examples/multiFileProject) + * [ 4. PWM_StepperControl](examples/PWM_StepperControl) **New** * [Example PWM_Multi](#example-PWM_Multi) * [Debug Terminal Output Samples](#debug-terminal-output-samples) * [1. PWM_Single on Nano_33_BLE](#1-PWM_Single-on-Nano_33_BLE) @@ -101,7 +102,7 @@ This non-being-blocked important feature is absolutely necessary for mission-cri 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. [`Arduino mbed_nano core 3.4.1+`](https://github.com/arduino/ArduinoCore-mbed) for Arduino (Use Arduino Board Manager) MBED nRF52840-based boards such as **Nano_33_BLE, Nano_33_BLE_Sense**. [![GitHub release](https://img.shields.io/github/release/arduino/ArduinoCore-mbed.svg)](https://github.com/arduino/ArduinoCore-mbed/releases/latest) -3. `Seeeduino mbed core 2.7.2+` for Seeeduino nRF52840-based boards such as **SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE** +3. [`Seeeduino mbed core 2.9.0+`](https://github.com/Seeed-Studio/ArduinoCore-mbed) for Seeed nRF52840-based boards such as **SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE**. [![GitHub release](https://img.shields.io/github/release/Seeed-Studio/ArduinoCore-mbed.svg)](https://github.com/Seeed-Studio/ArduinoCore-mbed/releases/latest) --- --- @@ -238,7 +239,8 @@ if (pwm) 1. [PWM_Multi](examples/PWM_Multi) 2. [PWM_Single](examples/PWM_Single) - 3. [**multiFileProject**](examples/multiFileProject) **New** + 3. [multiFileProject](examples/multiFileProject) + 4. [**PWM_StepperControl**](examples/PWM_StepperControl) **New** --- @@ -261,7 +263,7 @@ The following is the sample terminal output when running example [PWM_Single](ex ```cpp Starting PWM_Single on Nano_33_BLE -nRF52_MBED_PWM v1.0.2 +nRF52_MBED_PWM v1.0.3 [PWM] Freq = 5000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 2 ============================================ @@ -293,7 +295,7 @@ The following is the sample terminal output when running example [**PWM_Multi**] ```cpp Starting PWM_Multi on Nano_33_BLE -nRF52_MBED_PWM v1.0.2 +nRF52_MBED_PWM v1.0.3 [PWM] Freq = 1000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 2 [PWM] Freq = 2500.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 3 [PWM] Freq = 4000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 4 @@ -366,14 +368,16 @@ Submit issues to: [nRF52_MBED_PWM issues](https://github.com/khoih-prog/nRF52_MB ## DONE -1. Basic hardware multi-channel PWM for **Nano_33_BLE**. -2. Add Table of Contents -3. Permit to start, stop, modify, restore PWM settings on-the-fly -4. Optimize library code by using `reference-passing` instead of `value-passing` -5. Use `h-only` style -6. Add functions to read PWM parameters. -7. Add support to Seeeduino nRF52840-based boards such as **SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE**, etc. using Seeeduino `mbed` core -8. Add astyle using `allman` style. Restyle the library + 1. Basic hardware multi-channel PWM for **Nano_33_BLE**. + 2. Add Table of Contents + 3. Permit to start, stop, modify, restore PWM settings on-the-fly + 4. Optimize library code by using `reference-passing` instead of `value-passing` + 5. Use `h-only` style + 6. Add functions to read PWM parameters. + 7. Add support to Seeeduino nRF52840-based boards such as **SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE**, etc. using Seeeduino `mbed` core + 8. Add astyle using `allman` style. Restyle the library + 9. Add example [PWM_StepperControl](https://github.com/khoih-prog/nRF52_MBED_PWM/tree/main/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM + --- --- @@ -382,6 +386,15 @@ Submit issues to: [nRF52_MBED_PWM issues](https://github.com/khoih-prog/nRF52_MB 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

+ --- ## Contributing diff --git a/changelog.md b/changelog.md index 430ac94..8fdd390 100644 --- a/changelog.md +++ b/changelog.md @@ -11,12 +11,14 @@ Donate to my libraries using BuyMeACoffee + --- --- ## Table of Contents * [Changelog](#changelog) + * [Releases v1.0.3](#Releases-v103) * [Releases v1.0.2](#Releases-v102) * [Releases v1.0.1](#Releases-v101) * [Initial Releases v1.0.0](#Initial-Releases-v100) @@ -26,6 +28,10 @@ ## Changelog +### Releases v1.0.3 + +1. Add example [PWM_StepperControl](https://github.com/khoih-prog/nRF52_MBED_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) + ### Releases v1.0.2 1. Add support to Seeeduino nRF52840-based boards such as **SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE**, etc. using Seeeduino `mbed` core diff --git a/examples/PWM_StepperControl/PWM_StepperControl.ino b/examples/PWM_StepperControl/PWM_StepperControl.ino new file mode 100644 index 0000000..009fd0d --- /dev/null +++ b/examples/PWM_StepperControl/PWM_StepperControl.ino @@ -0,0 +1,76 @@ +/**************************************************************************************************************************** + PWM_StepperControl.ino + For Nano_33_BLE or Nano_33_BLE_Sense boards + Written by Khoi Hoang + + Built by Khoi Hoang https://github.com/khoih-prog/nRF52_MBED_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 + +#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE ) + #error This code is designed to run on nRF52-based Nano-33-BLE boards using mbed-RTOS platform! Please check your Tools->Board setting. +#endif + +#define _PWM_LOGLEVEL_ 1 + +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error +#include "nRF52_MBED_PWM.h" + +// All the digital pins on Arduino Nano 33 BLE sense are PWM-enabled pins which are numbered from D0 to D13 + +#define STEP_PIN D2 +#define DIR_PIN D9 + +mbed::PwmOut* stepper = nullptr; + +void setSpeed(int speed) +{ + if (speed == 0) + { + // Use DC = 0 to stop stepper + setPWM(stepper, STEP_PIN, 500, 0); + } + else + { + // Set the frequency of the PWM output and a duty cycle of 50% + digitalWrite(DIR_PIN, (speed < 0)); + setPWM(stepper, 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(nRF52_MBED_PWM_VERSION); +} + +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 e1d2983..c3bae8f 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "nRF52_MBED_PWM", - "version": "1.0.2", + "version": "1.0.3", "keywords": "timing, device, control, timer, pwm, interrupt, isr, isr-based, hardware-timer, mission-critical, accuracy, non-blocking, mbed, mbed-nano, nrf52840, nano-33-ble, nano-33-ble-sense, precise, hardware", "description": "This library enables you to use Hardware-based PWM to create and output PWM to pins on an nRF52840-based Nano_33_BLE board. These PWM channels, using nRF52840 Hardware PWM, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software ir ISR-based PWM, using millis(), micros() or Timer Interrupt. This important feature is absolutely necessary for mission-critical tasks. You can start, stop, change and restore the settings of any PWM channel on-the-fly.", "authors": diff --git a/library.properties b/library.properties index b99b485..f9fb5e1 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=nRF52_MBED_PWM -version=1.0.2 +version=1.0.3 author=Khoi Hoang maintainer=Khoi Hoang sentence=This library enables you to use Hardware-based PWM to create and output PWM to pins on an nRF52840-based Nano_33_BLE board. diff --git a/src/PWM_Generic_Debug.h b/src/PWM_Generic_Debug.h index 115c390..2889d4f 100644 --- a/src/PWM_Generic_Debug.h +++ b/src/PWM_Generic_Debug.h @@ -6,13 +6,14 @@ Built by Khoi Hoang https://github.com/khoih-prog/nRF52_MBED_PWM Licensed under MIT license - Version: 1.0.2 + Version: 1.0.3 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/02/2022 Initial coding for Nano_33_BLE / Nano_33_BLE_Sense using ArduinoCore-mbed mbed_nano core 1.0.1 K.Hoang 09/02/2022 Add functions to read PWM parameters 1.0.2 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed + 1.0.3 K Hoang 22/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once diff --git a/src/nRF52_MBED_PWM.h b/src/nRF52_MBED_PWM.h index 6bed619..52fd04e 100644 --- a/src/nRF52_MBED_PWM.h +++ b/src/nRF52_MBED_PWM.h @@ -6,13 +6,14 @@ Built by Khoi Hoang https://github.com/khoih-prog/nRF52_MBED_PWM Licensed under MIT license - Version: 1.0.2 + Version: 1.0.3 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/02/2022 Initial coding for Nano_33_BLE / Nano_33_BLE_Sense using ArduinoCore-mbed mbed_nano core 1.0.1 K.Hoang 09/02/2022 Add functions to read PWM parameters 1.0.2 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed + 1.0.3 K Hoang 22/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once diff --git a/src/nRF52_MBED_PWM.hpp b/src/nRF52_MBED_PWM.hpp index 955eb18..36c42b9 100644 --- a/src/nRF52_MBED_PWM.hpp +++ b/src/nRF52_MBED_PWM.hpp @@ -6,13 +6,14 @@ Built by Khoi Hoang https://github.com/khoih-prog/nRF52_MBED_PWM Licensed under MIT license - Version: 1.0.2 + Version: 1.0.3 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/02/2022 Initial coding for Nano_33_BLE / Nano_33_BLE_Sense using ArduinoCore-mbed mbed_nano core 1.0.1 K.Hoang 09/02/2022 Add functions to read PWM parameters 1.0.2 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed + 1.0.3 K Hoang 22/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once @@ -25,13 +26,13 @@ #endif #ifndef nRF52_MBED_PWM_VERSION - #define nRF52_MBED_PWM_VERSION "nRF52_MBED_PWM v1.0.2" + #define nRF52_MBED_PWM_VERSION "nRF52_MBED_PWM v1.0.3" #define nRF52_MBED_PWM_VERSION_MAJOR 1 #define nRF52_MBED_PWM_VERSION_MINOR 0 - #define nRF52_MBED_PWM_VERSION_PATCH 2 + #define nRF52_MBED_PWM_VERSION_PATCH 3 - #define nRF52_MBED_PWM_VERSION_INT 1000002 + #define nRF52_MBED_PWM_VERSION_INT 1000003 #endif diff --git a/src/nRF52_MBED_PWM_Impl.h b/src/nRF52_MBED_PWM_Impl.h index 8902c56..c190092 100644 --- a/src/nRF52_MBED_PWM_Impl.h +++ b/src/nRF52_MBED_PWM_Impl.h @@ -6,13 +6,14 @@ Built by Khoi Hoang https://github.com/khoih-prog/nRF52_MBED_PWM Licensed under MIT license - Version: 1.0.2 + Version: 1.0.3 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/02/2022 Initial coding for Nano_33_BLE / Nano_33_BLE_Sense using ArduinoCore-mbed mbed_nano core 1.0.1 K.Hoang 09/02/2022 Add functions to read PWM parameters 1.0.2 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed + 1.0.3 K Hoang 22/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #ifndef nRF52_MBED_PWM_IMPL_H