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

Commit 277e0a8

Browse files
authored
v1.0.0 for hardware-PWM on SAMD21/SAMD51 boards
### Releases v1.0.0 1. Initial coding for **SAMD21/SAMD51 boards** such as `NANO_33_IOT`, `ITSYBITSY_M4`, `SEEED_XIAO_M0`, `SparkFun SAMD51_Thing_Plus`, etc. using - [Arduino SAMD core](https://github.com/arduino/ArduinoCore-samd) - [Adafruit SAMD core](https://github.com/adafruit/ArduinoCore-samd) - [Seeed-Studio SAMD core](https://github.com/Seeed-Studio/ArduinoCore-samd) - [Sparkfun SAMD core](https://github.com/sparkfun/Arduino_Boards) - [Industruino SAMD core](https://github.com/Industruino/IndustruinoSAMD) - [Industruino SAMx core](https://github.com/Industruino/IndustruinoSAMx)
1 parent cd003bc commit 277e0a8

File tree

17 files changed

+2451
-0
lines changed

17 files changed

+2451
-0
lines changed

CONTRIBUTING.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## Contributing to SAMD_PWM
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in SAMD_PWM if you find them.
6+
7+
However, before reporting a bug please check through the following:
8+
9+
* [Existing Open Issues](https://github.com/khoih-prog/SAMD_PWM/issues) - someone might have already encountered this.
10+
11+
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/SAMD_PWM/issues/new).
12+
13+
### How to submit a bug report
14+
15+
Please ensure to specify the following:
16+
17+
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18+
* `SAMD` Core Version (e.g. Arduino SAMD core v1.8.13, Adafruit SAMD core v1.7.11, Seeed Studio SAMD v1.8.3, Sparkfun SAMD v1.8.3)
19+
* Board type and relevant info
20+
* Contextual information (e.g. what you were trying to achieve)
21+
* Simplest possible steps to reproduce
22+
* Anything that might be relevant in your opinion, such as:
23+
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
24+
* Network configuration
25+
26+
27+
### Example
28+
29+
```
30+
Arduino IDE version: 1.8.19
31+
Arduino SAMD Core Version 1.8.13
32+
SAMD_NANO_33_IOT
33+
OS: Ubuntu 20.04 LTS
34+
Linux xy-Inspiron-3593 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
35+
36+
Context:
37+
I encountered a crash while using this library.
38+
39+
Steps to reproduce:
40+
1. ...
41+
2. ...
42+
3. ...
43+
4. ...
44+
```
45+
46+
47+
### Additional context
48+
49+
Add any other context about the problem here.
50+
51+
---
52+
53+
### Sending Feature Requests
54+
55+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
56+
57+
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/SAMD_PWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
58+
59+
---
60+
61+
### Sending Pull Requests
62+
63+
Pull Requests with changes and fixes are also welcome!
64+
65+
Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)
66+
67+
1. Change directory to the library GitHub
68+
69+
```
70+
xy@xy-Inspiron-3593:~$ cd Arduino/xy/SAMD_PWM_GitHub/
71+
xy@xy-Inspiron-3593:~/Arduino/xy/SAMD_PWM_GitHub$
72+
```
73+
74+
2. Issue astyle command
75+
76+
```
77+
xy@xy-Inspiron-3593:~/Arduino/xy/SAMD_PWM_GitHub$ bash utils/restyle.sh
78+
```
79+

changelog.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SAMD_PWM Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/SAMD_PWM.svg?)](https://www.ardu-badge.com/SAMD_PWM)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/SAMD_PWM.svg)](https://github.com/khoih-prog/SAMD_PWM/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/SAMD_PWM/blob/main/LICENSE)
6+
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
7+
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/SAMD_PWM.svg)](http://github.com/khoih-prog/SAMD_PWM/issues)
8+
9+
---
10+
---
11+
12+
## Table of Contents
13+
14+
## Table of Contents
15+
16+
* [Changelog](#changelog)
17+
* [Releases v1.0.0](#releases-v100)
18+
19+
---
20+
---
21+
22+
## Changelog
23+
24+
25+
### Releases v1.0.0
26+
27+
1. Initial coding for **SAMD21/SAMD51 boards** such as `NANO_33_IOT`, `ITSYBITSY_M4`, `SEEED_XIAO_M0`, `SparkFun SAMD51_Thing_Plus`, etc. using
28+
- [Arduino SAMD core](https://github.com/arduino/ArduinoCore-samd)
29+
- [Adafruit SAMD core](https://github.com/adafruit/ArduinoCore-samd)
30+
- [Seeed-Studio SAMD core](https://github.com/Seeed-Studio/ArduinoCore-samd)
31+
- [Sparkfun SAMD core](https://github.com/sparkfun/Arduino_Boards)
32+
- [Industruino SAMD core](https://github.com/Industruino/IndustruinoSAMD)
33+
- [Industruino SAMx core](https://github.com/Industruino/IndustruinoSAMx)
34+
35+
36+
---
37+
---
38+
39+
## Copyright
40+
41+
Copyright 2022- Khoi Hoang
42+
43+

examples/PWM_Basic/PWM_Basic.ino

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/****************************************************************************************************************************
2+
PWM_Basic.ino
3+
4+
For SAMD21/SAMD51 boards using hardware-based PWM
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
#define _PWM_LOGLEVEL_ 4
12+
13+
#include "SAMD_PWM.h"
14+
15+
// Not OK for Nano_33_IoT (0, 1, 7, 8, 13, 14, 15 )
16+
// OK for Nano_33_IoT (2, 3, 4, 5, 6, 9, 10, 11, 12, 16, 17)
17+
// TCC OK => pin 4, 5, 6, 8, 9, 10, 11, 16/A2, 17/A3
18+
// TC OK => pin 12
19+
// For ITSYBITSY_M4
20+
// 16-bit Higher accuracy, Lower Frequency, PWM Pin OK: TCCx: 0-2, 4, 5, 7, 9-13
21+
// 8-bit Lower accuracy, Hi Frequency, PWM Pin OK: TCx: 18-20, 24-25
22+
23+
#define pinToUse 11
24+
25+
//creates pwm instance
26+
SAMD_PWM* PWM_Instance;
27+
28+
float frequency = 1000.0f;
29+
30+
float dutyCycle = 0.0f;
31+
32+
uint8_t channel = 0;
33+
34+
void setup()
35+
{
36+
Serial.begin(115200);
37+
38+
while (!Serial && millis() < 5000);
39+
40+
delay(500);
41+
42+
Serial.print(F("\nStarting PWM_Basic on "));
43+
Serial.println(BOARD_NAME);
44+
Serial.println(SAMD_PWM_VERSION);
45+
46+
//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%
47+
PWM_Instance = new SAMD_PWM(pinToUse, frequency, dutyCycle);
48+
}
49+
50+
void loop()
51+
{
52+
// You can change frequency here, anytime
53+
frequency = 2000.0f;
54+
dutyCycle = 20.0f;
55+
56+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
57+
58+
delay(10000);
59+
60+
// You can change frequency here, anytime
61+
frequency = 5000.0f;
62+
dutyCycle = 90.0f;
63+
64+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
65+
66+
//while (1)
67+
delay(10000);
68+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/****************************************************************************************************************************
2+
PWM_DynamicDutyCycle.ino
3+
4+
For SAMD21/SAMD51 boards using hardware-based PWM
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
#define _PWM_LOGLEVEL_ 4
12+
13+
#include "SAMD_PWM.h"
14+
15+
// Not OK for Nano_33_IoT (0, 1, 7, 8, 13, 14, 15 )
16+
// OK for Nano_33_IoT (2, 3, 4, 5, 6, 9, 10, 11, 12, 16, 17)
17+
// TCC OK => pin 4, 5, 6, 8, 9, 10, 11, 16/A2, 17/A3
18+
// TC OK => pin 12
19+
// For ITSYBITSY_M4
20+
// 16-bit Higher accuracy, Lower Frequency, PWM Pin OK: TCCx: 0-2, 4, 5, 7, 9-13
21+
// 8-bit Lower accuracy, Hi Frequency, PWM Pin OK: TCx: 18-20, 24-25
22+
23+
#define pinToUse 11
24+
25+
//creates pwm instance
26+
SAMD_PWM* PWM_Instance;
27+
28+
float frequency;
29+
float dutyCycle;
30+
31+
char dashLine[] = "=====================================================================================";
32+
33+
void printPWMInfo(SAMD_PWM* PWM_Instance)
34+
{
35+
Serial.println(dashLine);
36+
Serial.print("Actual data: pin = ");
37+
Serial.print(PWM_Instance->getPin());
38+
Serial.print(", PWM DC = ");
39+
Serial.print(PWM_Instance->getActualDutyCycle());
40+
Serial.print(", PWMPeriod = ");
41+
Serial.print(PWM_Instance->getPWMPeriod());
42+
Serial.print(", PWM Freq (Hz) = ");
43+
Serial.println(PWM_Instance->getActualFreq(), 4);
44+
Serial.println(dashLine);
45+
}
46+
47+
void setup()
48+
{
49+
Serial.begin(115200);
50+
51+
while (!Serial && millis() < 5000);
52+
53+
delay(500);
54+
55+
Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
56+
Serial.println(BOARD_NAME);
57+
Serial.println(SAMD_PWM_VERSION);
58+
59+
frequency = 5000.0f;
60+
61+
PWM_Instance = new SAMD_PWM(pinToUse, frequency, 50.0f);
62+
63+
if (PWM_Instance)
64+
{
65+
if (!PWM_Instance->setPWM())
66+
{
67+
Serial.println(F("Stop here"));
68+
69+
// stop here
70+
while (true)
71+
delay(1000);
72+
}
73+
}
74+
75+
Serial.println(dashLine);
76+
}
77+
78+
void loop()
79+
{
80+
dutyCycle = 90.0f;
81+
82+
Serial.print(F("Change PWM DutyCycle to "));
83+
Serial.println(dutyCycle);
84+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
85+
86+
printPWMInfo(PWM_Instance);
87+
88+
delay(5000);
89+
dutyCycle = 20.0f;
90+
91+
Serial.print(F("Change PWM DutyCycle to "));
92+
Serial.println(dutyCycle);
93+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
94+
printPWMInfo(PWM_Instance);
95+
96+
delay(5000);
97+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/****************************************************************************************************************************
2+
PWM_DynamicDutyCycle_Int.ino
3+
4+
For SAMD21/SAMD51 boards using hardware-based PWM
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
#define _PWM_LOGLEVEL_ 4
12+
13+
#include "SAMD_PWM.h"
14+
15+
// Not OK for Nano_33_IoT (0, 1, 7, 8, 13, 14, 15 )
16+
// OK for Nano_33_IoT (2, 3, 4, 5, 6, 9, 10, 11, 12, 16, 17)
17+
// TCC OK => pin 4, 5, 6, 8, 9, 10, 11, 16/A2, 17/A3
18+
// TC OK => pin 12
19+
// For ITSYBITSY_M4
20+
// 16-bit Higher accuracy, Lower Frequency, PWM Pin OK: TCCx: 0-2, 4, 5, 7, 9-13
21+
// 8-bit Lower accuracy, Hi Frequency, PWM Pin OK: TCx: 18-20, 24-25
22+
23+
#define pinToUse 11
24+
25+
//creates pwm instance
26+
SAMD_PWM* PWM_Instance;
27+
28+
float frequency;
29+
uint32_t dutyCycle;
30+
31+
char dashLine[] = "=====================================================================================";
32+
33+
void printPWMInfo(SAMD_PWM* PWM_Instance)
34+
{
35+
Serial.println(dashLine);
36+
Serial.print("Actual data: pin = ");
37+
Serial.print(PWM_Instance->getPin());
38+
Serial.print(", PWM DC = ");
39+
Serial.print(PWM_Instance->getActualDutyCycle());
40+
Serial.print(", PWMPeriod = ");
41+
Serial.print(PWM_Instance->getPWMPeriod());
42+
Serial.print(", PWM Freq (Hz) = ");
43+
Serial.println(PWM_Instance->getActualFreq(), 4);
44+
Serial.println(dashLine);
45+
}
46+
47+
void setup()
48+
{
49+
Serial.begin(115200);
50+
51+
while (!Serial && millis() < 5000);
52+
53+
delay(500);
54+
55+
Serial.print(F("\nStarting PWM_DynamicDutyCycle_Int on "));
56+
Serial.println(BOARD_NAME);
57+
Serial.println(SAMD_PWM_VERSION);
58+
59+
frequency = 1000.0f;
60+
61+
PWM_Instance = new SAMD_PWM(pinToUse, frequency, 50);
62+
63+
if (PWM_Instance)
64+
{
65+
PWM_Instance->setPWM();
66+
}
67+
68+
Serial.println(dashLine);
69+
}
70+
71+
void loop()
72+
{
73+
delay(5000);
74+
75+
frequency = 5000.0f;
76+
77+
// 50% dutyCycle = (real_dutyCycle * 65536) / 100
78+
dutyCycle = 32768;
79+
80+
Serial.print(F("Change PWM DutyCycle to (%) "));
81+
Serial.println((float) dutyCycle * 100 / 65536);
82+
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);
83+
84+
printPWMInfo(PWM_Instance);
85+
86+
delay(5000);
87+
88+
// 20% dutyCycle = (real_dutyCycle * 65536) / 100
89+
dutyCycle = 13107;
90+
91+
Serial.print(F("Change PWM DutyCycle to (%) "));
92+
Serial.println((float) dutyCycle * 100 / 65536);
93+
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);
94+
printPWMInfo(PWM_Instance);
95+
}

0 commit comments

Comments
 (0)