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

Commit a5ac09f

Browse files
authored
v1.0.0 for hardware-PWM on SAM_DUE boards
### Initial Releases v1.0.0 1. Initial coding to support **SAM_DUE**, etc. using [`Arduino SAM core`](https://github.com/arduino/ArduinoCore-sam)
1 parent 1d6ff63 commit a5ac09f

File tree

18 files changed

+2101
-0
lines changed

18 files changed

+2101
-0
lines changed

CONTRIBUTING.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## Contributing to SAMDUE_PWM
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in SAMDUE_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/SAMDUE_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/SAMDUE_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+
* `SAMDUE` Core Version (e.g. Arduino SAMDUE_ core v1.6.12)
19+
* Contextual information (e.g. what you were trying to achieve)
20+
* Simplest possible steps to reproduce
21+
* Anything that might be relevant in your opinion, such as:
22+
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
23+
* Network configuration
24+
25+
26+
### Example
27+
28+
```
29+
Arduino IDE version: 1.8.19
30+
Arduino SAMDUE Core Version 1.6.12
31+
OS: Ubuntu 20.04 LTS
32+
Linux xy-Inspiron-3593 5.4.0-100-generic #113-Ubuntu SMP Thu Feb 3 18:43:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
33+
Context:
34+
I encountered a crash while trying to use the Timer Interrupt.
35+
36+
Steps to reproduce:
37+
1. ...
38+
2. ...
39+
3. ...
40+
4. ...
41+
```
42+
43+
44+
45+
### Additional context
46+
47+
Add any other context about the problem here.
48+
49+
---
50+
51+
### Sending Feature Requests
52+
53+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
54+
55+
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/SAMDUE_PWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
56+
57+
---
58+
59+
### Sending Pull Requests
60+
61+
Pull Requests with changes and fixes are also welcome!
62+
63+
Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)
64+
65+
1. Change directory to the library GitHub
66+
67+
```
68+
xy@xy-Inspiron-3593:~$ cd Arduino/xy/SAMDUE_PWM_GitHub/
69+
xy@xy-Inspiron-3593:~/Arduino/xy/SAMDUE_PWM_GitHub$
70+
```
71+
72+
2. Issue astyle command
73+
74+
```
75+
xy@xy-Inspiron-3593:~/Arduino/xy/SAMDUE_PWM_GitHub$ bash utils/restyle.sh
76+
```
77+

changelog.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SAMDUE_PWM Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/SAMDUE_PWM.svg?)](https://www.ardu-badge.com/SAMDUE_PWM)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/SAMDUE_PWM.svg)](https://github.com/khoih-prog/SAMDUE_PWM/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/SAMDUE_PWM/blob/master/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/SAMDUE_PWM.svg)](http://github.com/khoih-prog/SAMDUE_PWM/issues)
8+
9+
---
10+
---
11+
12+
## Table of Contents
13+
14+
* [Changelog](#changelog)
15+
* [Initial Releases v1.0.0](#Initial-Releases-v100)
16+
17+
---
18+
---
19+
20+
## Changelog
21+
22+
### Initial Releases v1.0.0
23+
24+
1. Initial coding to support **SAM_DUE**, etc. using [`Arduino SAM core`](https://github.com/arduino/ArduinoCore-sam)
25+
26+
27+
28+

examples/PWM_Basic/PWM_Basic.ino

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/****************************************************************************************************************************
2+
PWM_Basic.ino
3+
4+
For SAM_DUE boards using hardware-based PWM
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/SAMDUE_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
#define _PWM_LOGLEVEL_ 4
12+
13+
// Select false to use PWM
14+
#define USING_TIMER false //true
15+
16+
#include "SAMDUE_PWM.h"
17+
18+
// SAM_DUE:
19+
// PWM pins: 6, 7, 8, 9
20+
// Timer pins: 2-5, 10-13.
21+
// pin2: TC0_CHA0, pin3: TC2_CHA7, pin4: TC2_CHB6, pin5: TC2_CHA6
22+
// pin 10: TC2_CHB7, pin11: TC2_CHA8, pin12: TC2_CHB8, pin13: TC0_CHB0
23+
24+
#if USING_TIMER
25+
#define pinToUse 5
26+
#else
27+
#define pinToUse 6
28+
#endif
29+
30+
//creates pwm instance
31+
SAMDUE_PWM* PWM_Instance;
32+
33+
float frequency = 1000.0f;
34+
35+
float dutyCycle = 0.0f;
36+
37+
void setup()
38+
{
39+
Serial.begin(115200);
40+
41+
while (!Serial && millis() < 5000);
42+
43+
delay(500);
44+
45+
#if USING_TIMER
46+
Serial.print(F("\nStarting PWM_Basic using Timer on "));
47+
#else
48+
Serial.print(F("\nStarting PWM_Basic using PWM on "));
49+
#endif
50+
51+
Serial.println(BOARD_NAME);
52+
Serial.println(SAMDUE_PWM_VERSION);
53+
54+
//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%
55+
PWM_Instance = new SAMDUE_PWM(pinToUse, frequency, dutyCycle);
56+
57+
if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
58+
{
59+
Serial.print(F("Stop here forever"));
60+
61+
while (true)
62+
delay(10000);
63+
}
64+
}
65+
66+
void loop()
67+
{
68+
// You can change frequency here, anytime
69+
frequency = 2000.0f;
70+
dutyCycle = 20.0f;
71+
72+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
73+
74+
delay(10000);
75+
76+
// You can change frequency here, anytime
77+
frequency = 5000.0f;
78+
dutyCycle = 90.0f;
79+
80+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
81+
82+
//while (1)
83+
delay(10000);
84+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/****************************************************************************************************************************
2+
PWM_DynamicDutyCycle.ino
3+
4+
For SAM_DUE boards using hardware-based PWM
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/SAMDUE_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
#define _PWM_LOGLEVEL_ 4
12+
13+
// Select false to use PWM
14+
#define USING_TIMER true
15+
16+
#include "SAMDUE_PWM.h"
17+
18+
// SAM_DUE:
19+
// PWM pins: 6, 7, 8, 9
20+
// Timer pins: 2-5, 10-13.
21+
// pin2: TC0_CHA0, pin3: TC2_CHA7, pin4: TC2_CHB6, pin5: TC2_CHA6
22+
// pin 10: TC2_CHB7, pin11: TC2_CHA8, pin12: TC2_CHB8, pin13: TC0_CHB0
23+
24+
#if USING_TIMER
25+
#define pinToUse 5
26+
#else
27+
#define pinToUse 6
28+
#endif
29+
30+
//creates pwm instance
31+
SAMDUE_PWM* PWM_Instance;
32+
33+
float frequency;
34+
float dutyCycle;
35+
36+
char dashLine[] = "=====================================================================================";
37+
38+
void printPWMInfo(SAMDUE_PWM* PWM_Instance)
39+
{
40+
Serial.println(dashLine);
41+
Serial.print("Actual data: pin = ");
42+
Serial.print(PWM_Instance->getPin());
43+
Serial.print(", PWM DC = ");
44+
Serial.print(PWM_Instance->getActualDutyCycle());
45+
Serial.print(", PWMPeriod = ");
46+
Serial.print(PWM_Instance->getPWMPeriod());
47+
Serial.print(", PWM Freq (Hz) = ");
48+
Serial.println(PWM_Instance->getActualFreq(), 4);
49+
Serial.println(dashLine);
50+
}
51+
52+
void setup()
53+
{
54+
Serial.begin(115200);
55+
56+
while (!Serial && millis() < 5000);
57+
58+
delay(500);
59+
60+
#if USING_TIMER
61+
Serial.print(F("\nStarting PWM_DynamicDutyCycle using Timer on "));
62+
#else
63+
Serial.print(F("\nStarting PWM_DynamicDutyCycle using PWM on "));
64+
#endif
65+
66+
Serial.println(BOARD_NAME);
67+
Serial.println(SAMDUE_PWM_VERSION);
68+
69+
frequency = 5000.0f;
70+
71+
PWM_Instance = new SAMDUE_PWM(pinToUse, frequency, 0.0f);
72+
73+
if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
74+
{
75+
Serial.print(F("Stop here forever"));
76+
77+
while (true)
78+
delay(10000);
79+
}
80+
81+
Serial.println(dashLine);
82+
}
83+
84+
void loop()
85+
{
86+
dutyCycle = 90.0f;
87+
88+
Serial.print(F("Change PWM DutyCycle to "));
89+
Serial.println(dutyCycle);
90+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
91+
92+
printPWMInfo(PWM_Instance);
93+
94+
delay(5000);
95+
dutyCycle = 20.0f;
96+
97+
Serial.print(F("Change PWM DutyCycle to "));
98+
Serial.println(dutyCycle);
99+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
100+
printPWMInfo(PWM_Instance);
101+
102+
delay(5000);
103+
}

0 commit comments

Comments
 (0)