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/AVR_PWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM
  • Loading branch information
khoih-prog authored Jan 22, 2023
1 parent a3f0d9f commit 7e2bedc
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 39 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/AVR_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 AVR core v1.8.6
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 using this library
Expand Down
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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)
* [Example PWM_Multi](#example-PWM_Multi)
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
* [1. PWM_DynamicDutyCycle on Arduino AVR Mega2560](#1-PWM_DynamicDutyCycle-on-Arduino-AVR-Mega2560)
Expand Down Expand Up @@ -72,7 +73,7 @@

This hardware-based PWM library enables you to use Hardware-PWM on AVR-based boards to create and output PWM. These purely hardware-based PWM channels can generate very high PWM frequencies, depending on CPU clock and acceptable accuracy, due to 8 or 16-bit PWM / Timer registers.

This library is using the **same or similar functions** as other FastPWM libraries, as follows, to enable you to **port your PWM code easily between platforms**
This library is using the **same or similar functions** as other `FastPWM` libraries, as follows, to enable you to **port your PWM code easily between platforms**

1. [**RP2040_PWM**](https://github.com/khoih-prog/RP2040_PWM)
2. [**AVR_PWM**](https://github.com/khoih-prog/AVR_PWM)
Expand Down Expand Up @@ -364,7 +365,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 @@ -386,7 +387,7 @@ The following is the sample terminal output when running example [PWM_DynamicDut

```cpp
Starting PWM_DynamicDutyCycle on Arduino AVR Mega2560/ADK
AVR_PWM v1.0.0
AVR_PWM v1.0.1
[PWM] AVR_PWM: _dutycycle = 32767
[PWM] setPWM_Int: _dutycycle = 32767
[PWM] setPWM_Int:using TIMER4C
Expand Down Expand Up @@ -424,7 +425,7 @@ The following is the sample terminal output when running example [**PWM_Multi**]
```cpp
Starting PWM_Multi on Arduino AVR Mega2560/ADK
AVR_PWM v1.0.0
AVR_PWM v1.0.1
=====================================================================================
Index Pin PWM_freq DutyCycle Actual Freq
=====================================================================================
Expand Down Expand Up @@ -454,7 +455,7 @@ The following is the sample terminal output when running example [**PWM_DynamicF

```cpp
Starting PWM_DynamicFreq on Arduino AVR Mega2560/ADK
AVR_PWM v1.0.0
AVR_PWM v1.0.1
[PWM] AVR_PWM: _dutycycle = 32767
[PWM] setPWM_Int: _dutycycle = 32767
[PWM] setPWM_Int:using TIMER4C
Expand Down Expand Up @@ -504,7 +505,7 @@ The following is the sample terminal output when running example [**PWM_Waveform
```cpp
Starting PWM_Waveform on Arduino AVR Mega2560/ADK
AVR_PWM v1.0.0
AVR_PWM v1.0.1
[PWM] AVR_PWM: _dutycycle = 0
[PWM] setPWM: _dutycycle = 0
[PWM] setPWM_Int: _dutycycle = 0
Expand Down Expand Up @@ -559,7 +560,7 @@ The following is the sample terminal output when running example [**PWM_Waveform

```cpp
Starting PWM_Waveform on Arduino AVR ATMega32U4
AVR_PWM v1.0.0
AVR_PWM v1.0.1
[PWM] AVR_PWM: _dutycycle = 0
[PWM] setPWM: _dutycycle = 0
[PWM] setPWM_Int: _dutycycle = 0
Expand Down Expand Up @@ -613,7 +614,7 @@ The following is the sample terminal output when running example [**PWM_Waveform
```cpp
Starting PWM_Waveform on Arduino AVR UNO, Nano, etc.
AVR_PWM v1.0.0
AVR_PWM v1.0.1
[PWM] AVR_PWM: _dutycycle = 0
[PWM] setPWM: _dutycycle = 0
[PWM] setPWM_Int: _dutycycle = 0
Expand Down Expand Up @@ -703,7 +704,7 @@ Submit issues to: [AVR_PWM issues](https://github.com/khoih-prog/AVR_PWM/issues)
- [`Arduino AVR core`](https://github.com/arduino/ArduinoCore-avr)
- [`Adafruit AVR core`](https://github.com/adafruit/Adafruit_Arduino_Boards)
- [`Sparkfun AVR core`](https://github.com/sparkfun/Arduino_Boards)

2. Add example [PWM_StepperControl](https://github.com/khoih-prog/AVR_PWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM

---
---
Expand All @@ -712,6 +713,15 @@ Submit issues to: [AVR_PWM issues](https://github.com/khoih-prog/AVR_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 v1.0.1


<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 @@ -16,13 +16,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/AVR_PWM/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 **AVR boards, such as Mega-2560, UNO, Nano, Leonardo, etc.**, using AVR cores
Expand Down
44 changes: 22 additions & 22 deletions examples/PWM_DynamicFreq/PWM_DynamicFreq.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,33 @@
#include "AVR_PWM.h"

#if ( PWM_USING_ATMEGA2560 )
// Pins tested OK in Mega
//#define pinToUse 12 // Timer1B on Mega
//#define pinToUse 11 // Timer1A on Mega
//#define pinToUse 9 // Timer2B on Mega
//#define pinToUse 2 // Timer3B on Mega
//#define pinToUse 3 // Timer3C on Mega
//#define pinToUse 5 // Timer3A on Mega
//#define pinToUse 6 // Timer4A on Mega
//#define pinToUse 7 // Timer4B on Mega
#define pinToUse 8 // Timer4C on Mega
//#define pinToUse 46 // Timer5A on Mega
//#define pinToUse 45 // Timer5B on Mega
//#define pinToUse 44 // Timer5C on Mega
// Pins tested OK in Mega
//#define pinToUse 12 // Timer1B on Mega
//#define pinToUse 11 // Timer1A on Mega
//#define pinToUse 9 // Timer2B on Mega
//#define pinToUse 2 // Timer3B on Mega
//#define pinToUse 3 // Timer3C on Mega
//#define pinToUse 5 // Timer3A on Mega
//#define pinToUse 6 // Timer4A on Mega
//#define pinToUse 7 // Timer4B on Mega
#define pinToUse 8 // Timer4C on Mega
//#define pinToUse 46 // Timer5A on Mega
//#define pinToUse 45 // Timer5B on Mega
//#define pinToUse 44 // Timer5C on Mega

#elif ( PWM_USING_ATMEGA_32U4 )
// Pins tested OK on 32u4
//#define pinToUse 5 // Timer3A on 32u4
#define pinToUse 9 // Timer1A on 32u4
//#define pinToUse 10 // Timer1B on 32u4
// Pins tested OK on 32u4
//#define pinToUse 5 // Timer3A on 32u4
#define pinToUse 9 // Timer1A on 32u4
//#define pinToUse 10 // Timer1B on 32u4

#else

// Pins tested OK on Nano / UNO
//#define pinToUse 9 // Timer1A on UNO, Nano, etc
#define pinToUse 10 // Timer1B on UNO, Nano, etc
//#define pinToUse 5 // Timer0B on UNO, Nano, e
//#define pinToUse 3 // Timer2B on UNO, Nano, etc
// Pins tested OK on Nano / UNO
//#define pinToUse 9 // Timer1A on UNO, Nano, etc
#define pinToUse 10 // Timer1B on UNO, Nano, etc
//#define pinToUse 5 // Timer0B on UNO, Nano, e
//#define pinToUse 3 // Timer2B on UNO, Nano, etc
#endif

AVR_PWM* PWM_Instance;
Expand Down
150 changes: 150 additions & 0 deletions examples/PWM_StepperControl/PWM_StepperControl.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/****************************************************************************************************************************
PWM_StepperControl.ino
For AVR-based boards (UNO, Nano, Mega, 32U4, 16U4, etc. )
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/AVR_PWM
Licensed under MIT license
This is pure hardware-based PWM
*****************************************************************************************************************************/
/******************************************************************************************************************************
// For UNO / Nano
Timer0 ( 8-bit) used by delay(), millis() and micros(), and PWM generation on pins 5 (6 not usable)
Timer1 (16-bit) used by the Servo.h library and PWM generation on pins 9 and 10
Timer2 ( 8-bit) used by Tone() and PWM generation on pins 3 and 11
// For Mega
Timer0 ( 8-bit) used by delay(), millis() and micros(), and PWM generation on pins 4 (13 not usable)
Timer1 (16-bit) used by the Servo.h library and PWM generation on pins 11, 12
Timer2 ( 8-bit) used by Tone() and PWM generation on pins 9 and 10
Timer3 (16-bit) used by PWM generation on pins 2, 3 and 5
Timer4 (16-bit) used by PWM generation on pins 6, 7 and 8
Timer5 (16-bit) used by PWM generation on pins 44, 45 and 46
////////////////////////////////////////////
// For Mega (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46)
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 46 => TIMER5A // PL 3 ** 46 ** D46
////////////////////////////////////////////
// For 32u4 (3, 5, 6, 9, 10, 11, 13)
Pin 3 => TIMER0B
Pin 5 => TIMER3A
Pin 6 => TIMER4D
Pin 9 => TIMER1A
Pin 10 => TIMER1B
Pin 11 => TIMER0A
Pin 13 => TIMER4A
////////////////////////////////////////////
// For UNO, Nano (3, 5, 6, 9, 10, 11)
Pin 3 => TIMER2B,
Pin 5 => TIMER0B
Pin 6 => TIMER0A
Pin 9 => TIMER1A
Pin 10 => TIMER1B
Pin 11 => TIMER2(A)
******************************************************************************************************************************/

// Use with Stepper-Motor driver, such as TMC2209

#define _PWM_LOGLEVEL_ 4

#include "AVR_PWM.h"

#if ( PWM_USING_ATMEGA2560 )
// Pins tested OK in Mega
//#define STEP_PIN 12 // Timer1B on Mega
//#define STEP_PIN 11 // Timer1A on Mega
//#define STEP_PIN 9 // Timer2B on Mega
//#define STEP_PIN 2 // Timer3B on Mega
//#define STEP_PIN 3 // Timer3C on Mega
//#define STEP_PIN 5 // Timer3A on Mega
//#define STEP_PIN 6 // Timer4A on Mega
//#define STEP_PIN 7 // Timer4B on Mega
#define STEP_PIN 8 // Timer4C on Mega
//#define STEP_PIN 46 // Timer5A on Mega
//#define STEP_PIN 45 // Timer5B on Mega
//#define STEP_PIN 44 // Timer5C on Mega

#elif ( PWM_USING_ATMEGA_32U4 )
// Pins tested OK on 32u4
//#define STEP_PIN 5 // Timer3A on 32u4
#define STEP_PIN 9 // Timer1A on 32u4
//#define STEP_PIN 10 // Timer1B on 32u4

#else

// Pins tested OK on Nano / UNO
//#define STEP_PIN 9 // Timer1A on UNO, Nano, etc
#define STEP_PIN 10 // Timer1B on UNO, Nano, etc
//#define STEP_PIN 5 // Timer0B on UNO, Nano, e
//#define STEP_PIN 3 // Timer2B on UNO, Nano, etc
#endif

#define DIR_PIN 12

AVR_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(AVR_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 AVR_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": "AVR_PWM",
"version": "1.0.0",
"version": "1.0.1",
"keywords": "timing, device, control, timer, pwm, fast-pwm, hardware-based-pwm, high-frequency-pwm, hardware-pwm, mission-critical, accuracy, precise, non-blocking, avr, mega-2560, nano, uno, leonardo, 32u4, 16u4, at-mega",
"description": "This library enables you to use Hardware-based PWM channels on AVR-based boards, such as Nano, UNO, Mega, Leonardo, 32u4, etc., 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",
"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=AVR_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 AVR-based boards, such as Nano, UNO, Mega, Leonardo, 32u4, etc., to create and output PWM.
Expand Down
Loading

0 comments on commit 7e2bedc

Please sign in to comment.