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

Commit 9c93a7b

Browse files
authored
v1.0.1 to add PWM_StepperControl example
### Releases v1.0.1 1. Add example [PWM_StepperControl](https://github.com/khoih-prog/megaAVR_PWM/tree/main/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM
1 parent f4a0a27 commit 9c93a7b

File tree

8 files changed

+146
-16
lines changed

8 files changed

+146
-16
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,32 @@ However, before reporting a bug please check through the following:
1010

1111
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/megaAVR_PWM/issues/new).
1212

13+
---
14+
1315
### How to submit a bug report
1416

1517
Please ensure to specify the following:
1618

1719
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18-
* `Arduino megaAVR` or `MegaCoreX` Core Version (e.g. Arduino megaAVR core v1.8.7 or MegaCoreX core v1.1.0)
20+
* `Arduino megaAVR` or `MegaCoreX` Core Version (e.g. Arduino megaAVR core v1.8.7 or MegaCoreX core v1.1.1)
1921
* Contextual information (e.g. what you were trying to achieve)
2022
* Simplest possible steps to reproduce
2123
* Anything that might be relevant in your opinion, such as:
2224
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
2325
* Network configuration
2426

2527

28+
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.
29+
30+
---
31+
2632
### Example
2733

2834
```
2935
Arduino IDE version: 1.8.19
3036
Arduino megaAVR Core Version 1.8.7
3137
OS: Ubuntu 20.04 LTS
32-
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
38+
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
3339
3440
Context:
3541
I encountered a crash while trying to use the Timer Interrupt.

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* [ 5. PWM_Multi](examples/PWM_Multi)
4343
* [ 6. PWM_MultiChannel](examples/PWM_MultiChannel)
4444
* [ 7. PWM_Waveform](examples/PWM_Waveform)
45+
* [ 8. PWM_StepperControl](examples/PWM_StepperControl) **New**
4546
* [Example PWM_Multi](#example-PWM_Multi)
4647
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
4748
* [1. PWM_DynamicDutyCycle on MegaCoreX Nano Every](#1-PWM_DynamicDutyCycle-on-MegaCoreX-Nano-Every)
@@ -131,7 +132,7 @@ Functions using normal software-based PWMs, relying on loop() and calling millis
131132
## Prerequisites
132133

133134
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)
134-
2. [`Arduino megaAVR core 1.8.7+`](https://github.com/arduino/ArduinoCore-megaavr/releases) for Arduino megaAVR boards. Use Arduino Board Manager to install.
135+
2. [`Arduino megaAVR core 1.8.7+`](https://github.com/arduino/ArduinoCore-megaavr/releases) for Arduino megaAVR boards. [![GitHub release](https://img.shields.io/github/release/arduino/ArduinoCore-megaavr.svg)](https://github.com/arduino/ArduinoCore-megaavr/releases/latest). Use Arduino Board Manager to install.
135136
3. [`MegaCoreX megaAVR core 1.1.0+`](https://github.com/MCUdude/MegaCoreX/releases) for Arduino megaAVR boards. [![GitHub release](https://img.shields.io/github/release/MCUdude/MegaCoreX.svg)](https://github.com/MCUdude/MegaCoreX/releases/latest). Follow [**How to install**](https://github.com/MCUdude/MegaCoreX#how-to-install).
136137

137138

@@ -307,7 +308,7 @@ PWM_Instance->setPWM_manual(PWM_Pins, new_level);
307308
5. [PWM_Multi](examples/PWM_Multi)
308309
6. [PWM_MultiChannel](examples/PWM_MultiChannel)
309310
7. [PWM_Waveform](examples/PWM_Waveform)
310-
311+
8. [PWM_StepperControl](examples/PWM_StepperControl) **New**
311312

312313
---
313314
---
@@ -329,7 +330,7 @@ The following is the sample terminal output when running example [PWM_DynamicDut
329330

330331
```cpp
331332
Starting PWM_DynamicDutyCycle on MegaCoreX Nano Every
332-
megaAVR_PWM v1.0.0
333+
megaAVR_PWM v1.0.1
333334
[PWM] megaAVR_PWM: _dutycycle = 32767
334335
[PWM] setPWM_Int: input dutycycle = 127
335336
[PWM] setPWM_Int: _timer = 3
@@ -381,7 +382,7 @@ The following is the sample terminal output when running example [**PWM_Multi**]
381382
382383
```cpp
383384
Starting PWM_Multi on megaAVR Nano Every
384-
megaAVR_PWM v1.0.0
385+
megaAVR_PWM v1.0.1
385386
=====================================================================================
386387
Index Pin PWM_freq DutyCycle Actual Freq
387388
=====================================================================================
@@ -403,7 +404,7 @@ The following is the sample terminal output when running example [**PWM_DynamicF
403404

404405
```cpp
405406
Starting PWM_DynamicFreq on megaAVR Nano Every
406-
megaAVR_PWM v1.0.0
407+
megaAVR_PWM v1.0.1
407408
[PWM] megaAVR_PWM: _dutycycle = 32767
408409
[PWM] setPWM_Int: input dutycycle = 127
409410
[PWM] setPWM_Int: _timer = 3
@@ -446,7 +447,7 @@ The following is the sample terminal output when running example [**PWM_Waveform
446447
447448
```cpp
448449
Starting PWM_Waveform on megaAVR Nano Every
449-
megaAVR_PWM v1.0.0
450+
megaAVR_PWM v1.0.1
450451
[PWM] megaAVR_PWM: _dutycycle = 0
451452
[PWM] setPWM: _dutycycle = 0
452453
[PWM] setPWM_Int: input dutycycle = 0
@@ -537,7 +538,7 @@ Submit issues to: [megaAVR_PWM issues](https://github.com/khoih-prog/megaAVR_PWM
537538
1. Basic hardware-based multi-channel PWMs for **megaAVR-based boards** such as `UNO WiFi Rev2`, `AVR_Nano_Every`, etc.**, using either
538539
- [`Arduino megaAVR core`](https://github.com/arduino/ArduinoCore-megaavr) or
539540
- [`MegaCoreX megaAVR core`](https://github.com/MCUdude/MegaCoreX)
540-
541+
2. Add example [PWM_StepperControl](examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM
541542

542543

543544
---
@@ -547,6 +548,15 @@ Submit issues to: [megaAVR_PWM issues](https://github.com/khoih-prog/megaAVR_PWM
547548

548549
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
549550

551+
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
552+
553+
554+
<table>
555+
<tr>
556+
<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>
557+
</tr>
558+
</table>
559+
550560

551561
---
552562

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
## Table of Contents
1818

1919
* [Changelog](#changelog)
20+
* [Releases v1.0.1](#Releases-v101)
2021
* [Initial Releases v1.0.0](#Initial-Releases-v100)
2122

2223
---
2324
---
2425

2526
## Changelog
2627

28+
### Releases v1.0.1
29+
30+
1. Add example [PWM_StepperControl](https://github.com/khoih-prog/megaAVR_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)
31+
2732
### Initial Releases v1.0.0
2833

2934
1. Initial coding to support **megaAVR boards, such as UNO WiFi Rev2, AVR_Nano_Every, etc.**, using `Arduino megaAVR` or `MegaCoreX` core
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/****************************************************************************************************************************
2+
PWM_StepperControl.ino
3+
For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. )
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/megaAVR_PWM
7+
Licensed under MIT license
8+
9+
This is pure hardware-based PWM
10+
*****************************************************************************************************************************/
11+
/******************************************************************************************************************************
12+
Pins can be used for hardware-PWM
13+
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.)
14+
TCA0 (16-bit) used by PWM generation on pins 5, 9 and 10
15+
TCB0 (16-bit) used by PWM generation on pin 6
16+
TCB1 (16-bit) used by PWM generation on pin 3
17+
TCB2 (16-bit)
18+
TCB3 (16-bit)
19+
////////////////////////////////////////////
20+
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.)
21+
Pin 3 => TIMERB1, // 3 PF5, 8-bit PWM, 16-bit counter
22+
Pin 5 => TIMERA0, // 5 PB2, 16-bit PWM, 16-bit counter
23+
Pin 6 => TIMERB0, // 6 PF4, 8-bit PWM, 16-bit counter
24+
Pin 9 => TIMERA0, // 9 PB0, 16-bit PWM, 16-bit counter
25+
Pin 10 => TIMERA0, // 10 PB1, 16-bit PWM, 16-bit counter
26+
////////////////////////////////////////////
27+
******************************************************************************************************************************/
28+
29+
// Use with Stepper-Motor driver, such as TMC2209
30+
31+
#define _PWM_LOGLEVEL_ 4
32+
33+
#include "megaAVR_PWM.h"
34+
35+
#define USING_TIMERB true
36+
37+
#if USING_TIMERB
38+
// Pins tested OK in Nano Every ATmega4809
39+
#define STEP_PIN 3 // TimerB1, for higher frequencies, up to 100KHz
40+
//#define STEP_PIN 6 // TimerB0, for higher frequencies, up to 100KHz
41+
#elif USING_ARDUINO_MEGA_AVR_CORE
42+
// Pins tested OK in Nano Every ATmega4809 using Arduino megaAVR core
43+
// TimerA0 somehow can't be used with MegaCoreX
44+
#define STEP_PIN 5 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
45+
//#define STEP_PIN 9 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
46+
//#define STEP_PIN 10 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
47+
#else
48+
#error TimerA0 to be used with Arduino megaAVR Core
49+
#endif
50+
51+
#define DIR_PIN 4
52+
53+
////////////////////////////////////////////
54+
55+
megaAVR_PWM* stepper;
56+
57+
void setSpeed(int speed)
58+
{
59+
if (speed == 0)
60+
{
61+
// Use DC = 0 to stop stepper
62+
stepper->setPWM(STEP_PIN, 500, 0);
63+
}
64+
else
65+
{
66+
// Set the frequency of the PWM output and a duty cycle of 50%
67+
digitalWrite(DIR_PIN, (speed < 0));
68+
stepper->setPWM(STEP_PIN, abs(speed), 50);
69+
}
70+
}
71+
72+
void setup()
73+
{
74+
pinMode(DIR_PIN, OUTPUT);
75+
76+
Serial.begin(115200);
77+
78+
while (!Serial && millis() < 5000);
79+
80+
delay(100);
81+
82+
Serial.print(F("\nStarting PWM_StepperControl on "));
83+
Serial.println(BOARD_NAME);
84+
Serial.println(MEGA_AVR_PWM_VERSION);
85+
86+
// Create PWM object and passed just a random frequency of 500
87+
// The duty cycle is how you turn the motor on and off
88+
stepper = new megaAVR_PWM(STEP_PIN, 500, 0);
89+
}
90+
91+
void loop()
92+
{
93+
setSpeed(1000);
94+
delay(3000);
95+
96+
// Stop before reversing
97+
setSpeed(0);
98+
delay(3000);
99+
100+
// Reversing
101+
setSpeed(-500);
102+
delay(3000);
103+
104+
// Stop before reversing
105+
setSpeed(0);
106+
delay(3000);
107+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "megaAVR_PWM",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"keywords": "timing, device, control, timer, pwm, fast-pwm, hardware-based-pwm, high-frequency-pwm, hardware-pwm, mission-critical, accuracy, precise, non-blocking, megaavr, mega-avr, atmega4809, nano-every, uno-wifi-rev2, megacorex",
55
"description": "This library enables you to use Hardware-based PWM channels on megaAVR-based boards, such as UNO WiFi Rev2, AVR_Nano_Every, 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. Supporting MegaCoreX.",
66
"authors":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=megaAVR_PWM
2-
version=1.0.0
2+
version=1.0.1
33
author=Khoi Hoang
44
maintainer=Khoi Hoang <khoih.prog@gmail.com>
55
sentence=This library enables you to use Hardware-based PWM channels on megaAVR-based boards, such as UNO WiFi Rev2, AVR_Nano_Every, etc., to create and output PWM.

src/PWM_Generic_Debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
99
This is pure hardware-based PWM
1010
11-
Version: 1.0.0
11+
Version: 1.0.1
1212
1313
Version Modified By Date Comments
1414
------- ----------- ---------- -----------
1515
1.0.0 K.Hoang 31/10/2022 Initial coding for megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. )
16+
1.0.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example
1617
*****************************************************************************************************************************/
1718

1819
#pragma once

src/megaAVR_PWM.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
99
This is pure hardware-based PWM
1010
11-
Version: 1.0.0
11+
Version: 1.0.1
1212
1313
Version Modified By Date Comments
1414
------- ----------- ---------- -----------
1515
1.0.0 K.Hoang 31/10/2022 Initial coding for megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. )
16+
1.0.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example
1617
*****************************************************************************************************************************/
1718

1819
#pragma once
@@ -74,13 +75,13 @@
7475
////////////////////////////////////////
7576

7677
#ifndef MEGA_AVR_PWM_VERSION
77-
#define MEGA_AVR_PWM_VERSION F("megaAVR_PWM v1.0.0")
78+
#define MEGA_AVR_PWM_VERSION F("megaAVR_PWM v1.0.1")
7879

7980
#define MEGA_AVR_PWM_VERSION_MAJOR 1
8081
#define MEGA_AVR_PWM_VERSION_MINOR 0
81-
#define MEGA_AVR_PWM_VERSION_PATCH 0
82+
#define MEGA_AVR_PWM_VERSION_PATCH 1
8283

83-
#define MEGA_AVR_PWM_VERSION_INT 1000000
84+
#define MEGA_AVR_PWM_VERSION_INT 1000001
8485
#endif
8586

8687
////////////////////////////////////////

0 commit comments

Comments
 (0)