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

Commit

Permalink
v1.0.1 to add PWM_StepperControl example
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
khoih-prog authored Jan 22, 2023
1 parent 0f86358 commit e9f074c
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 13 deletions.
8 changes: 7 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Donate to my libraries using BuyMeACoffee" style="height: 50px !important;width: 181px !important;" ></a>
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-orange.svg?logo=buy-me-a-coffee&logoColor=FFDD00" style="height: 20px !important;width: 200px !important;" ></a>


---
---

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


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


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


<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
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
## Table of Contents

* [Changelog](#changelog)
* [Releases v1.0.1](#Releases-v101)
* [Initial Releases v1.0.0](#Initial-Releases-v100)

---
---

## 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)
Expand Down
87 changes: 87 additions & 0 deletions examples/PWM_StepperControl/PWM_StepperControl.ino
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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":
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=SAMDUE_PWM
version=1.0.0
version=1.0.1
author=Khoi Hoang <[email protected]>
maintainer=Khoi Hoang <[email protected]>
sentence=This library enables you to use Hardware-based PWM channels on SAMDUE boards to create and output PWM to pins.
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 @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/SAMDUE_PWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit e9f074c

Please sign in to comment.