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

Commit 05921b6

Browse files
authored
v1.0.1 to add PWM_StepperControl example
1 parent 7e7ed82 commit 05921b6

File tree

8 files changed

+126
-22
lines changed

8 files changed

+126
-22
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ 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/SAMD_PWM/issues/new).
1212

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

1517
Please ensure to specify the following:
@@ -24,14 +26,18 @@ Please ensure to specify the following:
2426
* Network configuration
2527

2628

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

2935
```
3036
Arduino IDE version: 1.8.19
3137
Arduino SAMD Core Version 1.8.13
3238
SAMD_NANO_33_IOT
3339
OS: Ubuntu 20.04 LTS
34-
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
40+
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
3541
3642
Context:
3743
I encountered a crash while using this library.
@@ -77,3 +83,5 @@ xy@xy-Inspiron-3593:~/Arduino/xy/SAMD_PWM_GitHub$
7783
xy@xy-Inspiron-3593:~/Arduino/xy/SAMD_PWM_GitHub$ bash utils/restyle.sh
7884
```
7985

86+
87+

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<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>
1111
<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>
1212

13+
1314
---
1415
---
1516

@@ -45,6 +46,7 @@
4546
* [ 5. PWM_Multi](examples/PWM_Multi)
4647
* [ 6. PWM_MultiChannel](examples/PWM_MultiChannel)
4748
* [ 7. PWM_Waveform](examples/PWM_Waveform)
49+
* [ 8. PWM_StepperControl](examples/PWM_StepperControl) **New**
4850
* [Example PWM_Multi](#example-PWM_Multi)
4951
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
5052
* [1. PWM_DynamicDutyCycle on SAMD_NANO_33_IOT](#1-PWM_DynamicDutyCycle-on-SAMD_NANO_33_IOT)
@@ -73,7 +75,7 @@
7375

7476
This hardware-based PWM library enables you to use Hardware-PWM on **SAMD21/SAMD51 boards** such as `NANO_33_IOT`, `ITSYBITSY_M4`, `SEEED_XIAO_M0`, `SparkFun SAMD51_Thing_Plus`, etc., to create and output PWM. These purely hardware-based PWM channels can generate very high PWM frequencies, depending on CPU clock and acceptable accuracy. The maximum resolution can be **16-bit** for better accuracy when using Timer `TCCx`. With Timer `TCx`, only **8-bit** resolution is supported with lower accuracy.
7577

76-
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**
78+
This library is using the **same or similar functions** as other `FastPWM` sibling libraries, as follows, to enable you to **port your PWM code easily between platforms**
7779

7880
1. [**RP2040_PWM**](https://github.com/khoih-prog/RP2040_PWM)
7981
2. [**AVR_PWM**](https://github.com/khoih-prog/AVR_PWM)
@@ -146,7 +148,6 @@ Functions using normal software-based PWMs, relying on `loop()` and calling `mil
146148
6. [Industruino SAMD core](https://github.com/Industruino/IndustruinoSAMD) for SAMD21/SAMD51 boards
147149

148150

149-
150151
---
151152
---
152153

@@ -388,6 +389,7 @@ PWM_Instance->setPWM_manual(PWM_Pins, new_level);
388389
5. [PWM_Multi](examples/PWM_Multi)
389390
6. [PWM_MultiChannel](examples/PWM_MultiChannel)
390391
7. [PWM_Waveform](examples/PWM_Waveform)
392+
8. [PWM_StepperControl](examples/PWM_StepperControl) **New**
391393

392394

393395
---
@@ -410,7 +412,7 @@ The following is the sample terminal output when running example [PWM_DynamicDut
410412

411413
```cpp
412414
Starting PWM_DynamicDutyCycle on SAMD_NANO_33_IOT
413-
SAMD_PWM v1.0.0
415+
SAMD_PWM v1.0.1
414416
[PWM] calcTCCPrescaler: OK pin = 11 , period = 200 , _prescaler = 1 , F_CPU = 48000000
415417
[PWM] calcTCCPrescaler: _dutycycle = 32768 , frequency = 5000.00 , _prescalerConfigBits = 0 , _compareValue = 9599
416418
[PWM] SAMD21 setPWM_Int: _tcNum = 2 , _tcChannel = 0 , _pinAttr = 28
@@ -451,7 +453,7 @@ The following is the sample terminal output when running example [**PWM_Multi**]
451453
452454
```cpp
453455
Starting PWM_Multi on SAMD_NANO_33_IOT
454-
SAMD_PWM v1.0.0
456+
SAMD_PWM v1.0.1
455457
[PWM] calcTCCPrescaler: OK pin = 4 , period = 500 , _prescaler = 1 , F_CPU = 48000000
456458
[PWM] calcTCCPrescaler: _dutycycle = 6553 , frequency = 2000.00 , _prescalerConfigBits = 0 , _compareValue = 23999
457459
[PWM] SAMD21 setPWM_Int: _tcNum = 1 , _tcChannel = 1 , _pinAttr = 28
@@ -501,7 +503,7 @@ The following is the sample terminal output when running example [**PWM_DynamicF
501503

502504
```cpp
503505
Starting PWM_DynamicFreq on ITSYBITSY_M4
504-
SAMD_PWM v1.0.0
506+
SAMD_PWM v1.0.1
505507
[PWM] calcTCCPrescaler: OK pin = 11 , period = 100 , _prescaler = 1 , F_CPU = 120000000
506508
[PWM] calcTCCPrescaler: _dutycycle = 32768 , frequency = 10000.00 , _prescalerConfigBits = 0 , _compareValue = 11999
507509
[PWM] SAMD51 setPWM_Int: _tcNum = 0 , _tcChannel = 1 , _pinAttr = 512
@@ -542,7 +544,7 @@ The following is the sample terminal output when running example [**PWM_Waveform
542544
543545
```cpp
544546
Starting PWM_Waveform on ITSYBITSY_M4
545-
SAMD_PWM v1.0.0
547+
SAMD_PWM v1.0.1
546548
[PWM] calcTCCPrescaler: OK pin = 11 , period = 1000 , _prescaler = 1 , F_CPU = 120000000
547549
[PWM] calcTCCPrescaler: _dutycycle = 0 , frequency = 1000.00 , _prescalerConfigBits = 0 , _compareValue = 119999
548550
[PWM] setPWM: _dutycycle = 655 , frequency = 1000.00
@@ -643,7 +645,7 @@ The following is the sample terminal output when running example [**PWM_Basic**]
643645

644646
```cpp
645647
Starting PWM_Basic on ITSYBITSY_M4
646-
SAMD_PWM v1.0.0
648+
SAMD_PWM v1.0.1
647649
[PWM] calcTCPrescaler: OK pin = 24 , period = 1000 , _prescaler = 1 , F_CPU = 120000000
648650
[PWM] calcTCPrescaler: _dutycycle = 0 , frequency = 1000.00 , _prescalerConfigBits = 0 , _compareValue = 119999
649651
[PWM] setPWM: _dutycycle = 13107 , frequency = 2000.00
@@ -684,7 +686,7 @@ The following is the sample terminal output when running example [**PWM_Basic**]
684686

685687
```cpp
686688
Starting PWM_Basic on SAMD_NANO_33_IOT
687-
SAMD_PWM v1.0.0
689+
SAMD_PWM v1.0.1
688690
[PWM] calcTCPrescaler: OK pin = 12 , period = 1000 , _prescaler = 1 , F_CPU = 48000000
689691
[PWM] calcTCPrescaler: _dutycycle = 0 , frequency = 1000.00 , _prescalerConfigBits = 0 , _compareValue = 47999
690692
[PWM] setPWM: _dutycycle = 13107 , frequency = 2000.00
@@ -759,6 +761,7 @@ Submit issues to: [SAMD_PWM issues](https://github.com/khoih-prog/SAMD_PWM/issue
759761
- [Sparkfun SAMD core](https://github.com/sparkfun/Arduino_Boards)
760762
- [Industruino SAMD core](https://github.com/Industruino/IndustruinoSAMD)
761763
- [Industruino SAMx core](https://github.com/Industruino/IndustruinoSAMx)
764+
2. Add example [PWM_StepperControl](https://github.com/khoih-prog/SAMD_PWM/tree/main/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM
762765

763766
---
764767
---
@@ -767,12 +770,22 @@ Submit issues to: [SAMD_PWM issues](https://github.com/khoih-prog/SAMD_PWM/issue
767770

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

773+
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
774+
775+
776+
<table>
777+
<tr>
778+
<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>
779+
</tr>
780+
</table>
781+
770782

771783
---
772784

773785
## Contributing
774786

775787
If you want to contribute to this project:
788+
776789
- Report bugs and errors
777790
- Ask for enhancements
778791
- Create issues and pull requests

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<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>
1111
<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>
1212

13+
1314
---
1415
---
1516

@@ -23,6 +24,9 @@
2324

2425
## Changelog
2526

27+
### Releases v1.0.1
28+
29+
1. Add example [PWM_StepperControl](https://github.com/khoih-prog/SAMD_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)
2630

2731
### Releases v1.0.0
2832

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/****************************************************************************************************************************
2+
PWM_StepperControl.ino
3+
For SAMD21/SAMD51 boards using hardware-based PWM
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_PWM
7+
Licensed under MIT license
8+
9+
Credits of Paul van Dinther (https://github.com/dinther). Check https://github.com/khoih-prog/RP2040_PWM/issues/16
10+
*****************************************************************************************************************************/
11+
12+
// Use with Stepper-Motor driver, such as TMC2209
13+
14+
#define _PWM_LOGLEVEL_ 4
15+
16+
#include "SAMD_PWM.h"
17+
18+
// Not OK for Nano_33_IoT (0, 1, 7, 8, 13, 14, 15 )
19+
// OK for Nano_33_IoT (2, 3, 4, 5, 6, 9, 10, 11, 12, 16, 17)
20+
// TCC OK => pin 4, 5, 6, 8, 9, 10, 11, 16/A2, 17/A3
21+
// TC OK => pin 12
22+
// For ITSYBITSY_M4
23+
// 16-bit Higher accuracy, Lower Frequency, PWM Pin OK: TCCx: 0-2, 4, 5, 7, 9-13
24+
// 8-bit Lower accuracy, Hi Frequency, PWM Pin OK: TCx: 18-20, 24-25
25+
26+
SAMD_PWM* stepper;
27+
28+
#define STEP_PIN 9
29+
#define DIR_PIN 8
30+
31+
void setSpeed(int speed)
32+
{
33+
if (speed == 0)
34+
{
35+
// Use DC = 0 to stop stepper
36+
stepper->setPWM(STEP_PIN, 500, 0);
37+
}
38+
else
39+
{
40+
// Set the frequency of the PWM output and a duty cycle of 50%
41+
digitalWrite(DIR_PIN, (speed < 0));
42+
stepper->setPWM(STEP_PIN, abs(speed), 50);
43+
}
44+
}
45+
46+
void setup()
47+
{
48+
pinMode(DIR_PIN, OUTPUT);
49+
50+
Serial.begin(115200);
51+
52+
while (!Serial && millis() < 5000);
53+
54+
delay(100);
55+
56+
Serial.print(F("\nStarting PWM_StepperControl on "));
57+
Serial.println(BOARD_NAME);
58+
Serial.println(SAMD_PWM_VERSION);
59+
60+
// Create PWM object and passed just a random frequency of 500
61+
// The duty cycle is how you turn the motor on and off
62+
stepper = new SAMD_PWM(STEP_PIN, 500, 0);
63+
}
64+
65+
void loop()
66+
{
67+
setSpeed(1000);
68+
delay(3000);
69+
70+
// Stop before reversing
71+
setSpeed(0);
72+
delay(3000);
73+
74+
// Reversing
75+
setSpeed(-500);
76+
delay(3000);
77+
78+
// Stop before reversing
79+
setSpeed(0);
80+
delay(3000);
81+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "SAMD_PWM",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"keywords": "timing, device, control, timer, interrupt, hardware, isr, isr-based, hardware-timer, isr-timer, isr-based-timer, mission-critical, accuracy, precise, non-blocking, samd, samd21, samd51, nano-33-iot",
55
"description": "This library enables you to use Hardware-based PWM channels on SAMD21/SAMD51-based 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",
66
"authors":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SAMD_PWM
2-
version=1.0.0
2+
version=1.0.1
33
author=Khoi Hoang <khoih.prog@gmail.com>
44
maintainer=Khoi Hoang <khoih.prog@gmail.com>
55
sentence=This library enables you to use Hardware-based PWM channels on SAMD21/SAMD51-based boards to create and output PWM to pins.

src/PWM_Generic_Debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_PWM
88
Licensed under MIT license
99
10-
Version: 1.0.0
10+
Version: 1.0.1
1111
1212
Version Modified By Date Comments
1313
------- ----------- ---------- -----------
1414
1.0.0 K Hoang 01/11/2022 Initial coding for SAMD21/SAMD51 boards
15+
1.0.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example
1516
*****************************************************************************************************************************/
1617

1718
#pragma once

src/SAMD_PWM.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_PWM
88
Licensed under MIT license
99
10-
Version: 1.0.0
10+
Version: 1.0.1
1111
1212
Version Modified By Date Comments
1313
------- ----------- ---------- -----------
1414
1.0.0 K Hoang 01/11/2022 Initial coding for SAMD21/SAMD51 boards
15+
1.0.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example
1516
*****************************************************************************************************************************/
1617

1718
#pragma once
@@ -136,13 +137,13 @@
136137
#include "Arduino.h"
137138

138139
#ifndef SAMD_PWM_VERSION
139-
#define SAMD_PWM_VERSION "SAMD_PWM v1.0.0"
140+
#define SAMD_PWM_VERSION "SAMD_PWM v1.0.1"
140141

141142
#define SAMD_PWM_VERSION_MAJOR 1
142143
#define SAMD_PWM_VERSION_MINOR 0
143-
#define SAMD_PWM_VERSION_PATCH 0
144+
#define SAMD_PWM_VERSION_PATCH 1
144145

145-
#define SAMD_PWM_VERSION_INT 1000000
146+
#define SAMD_PWM_VERSION_INT 1000001
146147
#endif
147148

148149
#include "PWM_Generic_Debug.h"
@@ -859,7 +860,6 @@ class SAMD_PWM
859860
return false;
860861

861862
#if defined(__SAMD51__)
862-
863863
// Check which timer to use
864864
if (_tcNum >= TCC_INST_NUM)
865865
{
@@ -869,7 +869,7 @@ class SAMD_PWM
869869
//TCx->COUNT8.CC[_tcChannel].reg = (DCValue * _compareValue / 100) >> 8;
870870

871871
//while (TCx->COUNT16.STATUS.bit.SYNCBUSY);
872-
872+
873873
// Set the Dutycycle
874874
TCx->COUNT8.CC[_tcChannel].reg = (DCValue * _compareValue / 100) >> 8;
875875

@@ -879,9 +879,7 @@ class SAMD_PWM
879879
PWM_LOGDEBUG3(F("setPWM_manual TC: New DCValue ="), DCValue * _compareValue / 100, F(", _compareValue ="),
880880
_compareValue);
881881
}
882-
883882
#else
884-
885883
// Check which timer to use
886884
if (_tcNum >= TCC_INST_NUM)
887885
{
@@ -896,8 +894,7 @@ class SAMD_PWM
896894
PWM_LOGDEBUG3(F("setPWM_manual TC: New DCValue ="), DCValue * _compareValue / 100, F(", _compareValue ="),
897895
_compareValue);
898896
}
899-
900-
#endif
897+
#endif
901898
else
902899
{
903900
Tcc* TCCx = (Tcc*) GetTC(_pinDesc.ulPWMChannel);

0 commit comments

Comments
 (0)