Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
v1.1.1 to add PWM_StepperControl example
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
khoih-prog authored Jan 22, 2023
1 parent 0c3e4f8 commit def46a7
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 22 deletions.
13 changes: 9 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -186,7 +187,7 @@ Functions using normal software-based PWMs, relying on loop() and calling millis
<img src="https://github.com/khoih-prog/Dx_PWM/raw/main/pics/Curiosity_AVR128DB48.png">
</p>

- **AVRDD-based boards (AVR64DB, AVR32DB, AVR16DB, etc.) using DxCore v1.5.1+**
- **AVRDD-based boards (AVR64DD, AVR32DD, AVR16DD, etc.) using DxCore v1.5.1+**


---
Expand All @@ -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).


---
Expand Down Expand Up @@ -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**


---
Expand All @@ -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
=====================================================================================
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
=====================================================================================
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

---
---
Expand All @@ -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


<table>
<tr>
<td align="center"><a href="https://github.com/dinther"><img src="https://github.com/dinther.png" width="100px;" alt="dinther"/><br /><sub><b>Paul van Dinther</b></sub></a><br /></td>
</tr>
</table>

---

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down
100 changes: 100 additions & 0 deletions examples/PWM_StepperControl/PWM_StepperControl.ino
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Dx_PWM
version=1.1.0
version=1.1.1
author=Khoi Hoang
maintainer=Khoi Hoang <khoih.prog@gmail.com>
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.
Expand Down
9 changes: 5 additions & 4 deletions src/Dx_PWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

////////////////////////////////////////
Expand Down
3 changes: 2 additions & 1 deletion src/PWM_Generic_Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit def46a7

Please sign in to comment.