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

Commit 2017789

Browse files
authored
v1.0.0 for hardware-PWM on nRF52 boards
### Initial Releases v1.0.0 1. Initial coding to support **nRF52-based boards, such as AdaFruit Itsy-Bitsy nRF52840, Feather nRF52840 Express, Seeed XIAO nRF52840, Seeed XIAO nRF52840 Sense**, etc. using [`Adafruit nRF52 core`](https://github.com/adafruit/Adafruit_nRF52_Arduino) or [`Seeeduino nRF52 core`](https://github.com/Seeed-Studio/Adafruit_nRF52_Arduino)
1 parent 41012e0 commit 2017789

File tree

17 files changed

+2082
-0
lines changed

17 files changed

+2082
-0
lines changed

CONTRIBUTING.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## Contributing to nRF52_PWM
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in nRF52_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/nRF52_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/nRF52_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+
* `NRF52` Core Version (e.g. Adafruit NRF52 core v1.3.0, Seeed nRF52 core v1.0.0)
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+
Adafruit NRF52 Core Version 1.3.0
31+
NRF52840_ITSYBITSY
32+
OS: Ubuntu 20.04 LTS
33+
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
34+
35+
Context:
36+
I encountered a crash while using this library
37+
38+
Steps to reproduce:
39+
1. ...
40+
2. ...
41+
3. ...
42+
4. ...
43+
```
44+
45+
---
46+
47+
### Sending Feature Requests
48+
49+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
50+
51+
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/nRF52_PWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
52+
53+
---
54+
55+
### Sending Pull Requests
56+
57+
Pull Requests with changes and fixes are also welcome!
58+
59+
Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)
60+
61+
1. Change directory to the library GitHub
62+
63+
```
64+
xy@xy-Inspiron-3593:~$ cd Arduino/xy/nRF52_PWM_GitHub/
65+
xy@xy-Inspiron-3593:~/Arduino/xy/nRF52_PWM_GitHub$
66+
```
67+
68+
2. Issue astyle command
69+
70+
```
71+
xy@xy-Inspiron-3593:~/Arduino/xy/nRF52_PWM_GitHub$ bash utils/restyle.sh
72+
```
73+

changelog.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# nRF52_PWM Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/nRF52_PWM.svg?)](https://www.ardu-badge.com/nRF52_PWM)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/nRF52_PWM.svg)](https://github.com/khoih-prog/nRF52_PWM/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/nRF52_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/nRF52_PWM.svg)](http://github.com/khoih-prog/nRF52_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 **nRF52-based boards, such as AdaFruit Itsy-Bitsy nRF52840, Feather nRF52840 Express, Seeed XIAO nRF52840, Seeed XIAO nRF52840 Sense**, etc. using [`Adafruit nRF52 core`](https://github.com/adafruit/Adafruit_nRF52_Arduino) or [`Seeeduino nRF52 core`](https://github.com/Seeed-Studio/Adafruit_nRF52_Arduino)
25+
26+
27+
---
28+
---
29+
30+
## Copyright
31+
32+
Copyright 2022- Khoi Hoang
33+
34+

examples/PWM_Basic/PWM_Basic.ino

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/****************************************************************************************************************************
2+
PWM_Basic.ino
3+
4+
For nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_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 "nRF52_PWM.h"
17+
18+
// OK for Feather_nRF52840_Express (5, 6, 9-13, 14-21/A0-A7, etc.)
19+
// OK for ItsyBitsy_nRF52840_Express (5, 7, 9-13, 14-20/A0-A6, etc.)
20+
21+
#define pinToUse 5
22+
23+
//creates pwm instance
24+
nRF52_PWM* PWM_Instance;
25+
26+
float frequency = 1000.0f;
27+
28+
float dutyCycle = 0.0f;
29+
30+
void setup()
31+
{
32+
Serial.begin(115200);
33+
34+
while (!Serial && millis() < 5000);
35+
36+
delay(500);
37+
38+
Serial.print(F("\nStarting PWM_Basic using PWM on "));
39+
Serial.println(BOARD_NAME);
40+
Serial.println(NRF52_PWM_VERSION);
41+
42+
//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%
43+
PWM_Instance = new nRF52_PWM(pinToUse, frequency, dutyCycle);
44+
45+
if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
46+
{
47+
Serial.print(F("Stop here forever"));
48+
49+
while (true)
50+
delay(10000);
51+
}
52+
}
53+
54+
void loop()
55+
{
56+
// You can change frequency here, anytime
57+
frequency = 2000.0f;
58+
//frequency = 20.0f;
59+
dutyCycle = 20.0f;
60+
61+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
62+
63+
delay(10000);
64+
65+
// You can change frequency here, anytime
66+
frequency = 5000.0f;
67+
//frequency = 50.0f;
68+
dutyCycle = 90.0f;
69+
70+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
71+
72+
//while (1)
73+
delay(10000);
74+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/****************************************************************************************************************************
2+
PWM_DynamicDutyCycle.ino
3+
4+
For nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_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 "nRF52_PWM.h"
17+
18+
// OK for Feather_nRF52840_Express (5, 6, 9, 10, 14-21/A0-A7, etc.)
19+
20+
#define pinToUse 6
21+
22+
//creates pwm instance
23+
nRF52_PWM* PWM_Instance;
24+
25+
float frequency;
26+
float dutyCycle;
27+
28+
char dashLine[] = "=====================================================================================";
29+
30+
void printPWMInfo(nRF52_PWM* PWM_Instance)
31+
{
32+
Serial.println(dashLine);
33+
Serial.print("Actual data: pin = ");
34+
Serial.print(PWM_Instance->getPin());
35+
Serial.print(", PWM DC = ");
36+
Serial.print(PWM_Instance->getActualDutyCycle());
37+
Serial.print(", PWMPeriod = ");
38+
Serial.print(PWM_Instance->getPWMPeriod());
39+
Serial.print(", PWM Freq (Hz) = ");
40+
Serial.println(PWM_Instance->getActualFreq(), 4);
41+
Serial.println(dashLine);
42+
}
43+
44+
void setup()
45+
{
46+
Serial.begin(115200);
47+
48+
while (!Serial && millis() < 5000);
49+
50+
delay(500);
51+
52+
Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
53+
Serial.println(BOARD_NAME);
54+
Serial.println(NRF52_PWM_VERSION);
55+
56+
frequency = 5000.0f;
57+
58+
PWM_Instance = new nRF52_PWM(pinToUse, frequency, 0.0f);
59+
60+
if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
61+
{
62+
Serial.print(F("Stop here forever"));
63+
64+
while (true)
65+
delay(10000);
66+
}
67+
68+
Serial.println(dashLine);
69+
}
70+
71+
void loop()
72+
{
73+
dutyCycle = 90.0f;
74+
75+
Serial.print(F("Change PWM DutyCycle to "));
76+
Serial.println(dutyCycle);
77+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
78+
79+
printPWMInfo(PWM_Instance);
80+
81+
delay(5000);
82+
dutyCycle = 20.0f;
83+
84+
Serial.print(F("Change PWM DutyCycle to "));
85+
Serial.println(dutyCycle);
86+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
87+
printPWMInfo(PWM_Instance);
88+
89+
delay(5000);
90+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/****************************************************************************************************************************
2+
PWM_DynamicDutyCycle_Int.ino
3+
4+
For nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_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 "nRF52_PWM.h"
17+
18+
// OK for Feather_nRF52840_Express (5, 6, 9-13, 14-21/A0-A7, etc.)
19+
// OK for ItsyBitsy_nRF52840_Express (5, 7, 9-13, 14-20/A0-A6, etc.)
20+
21+
#define pinToUse 5
22+
23+
//creates pwm instance
24+
nRF52_PWM* PWM_Instance;
25+
26+
float frequency;
27+
uint16_t dutyCycle;
28+
29+
char dashLine[] = "=====================================================================================";
30+
31+
void printPWMInfo(nRF52_PWM* PWM_Instance)
32+
{
33+
Serial.println(dashLine);
34+
Serial.print("Actual data: pin = ");
35+
Serial.print(PWM_Instance->getPin());
36+
Serial.print(", PWM DC = ");
37+
Serial.print(PWM_Instance->getActualDutyCycle());
38+
Serial.print(", PWMPeriod = ");
39+
Serial.print(PWM_Instance->getPWMPeriod());
40+
Serial.print(", PWM Freq (Hz) = ");
41+
Serial.println(PWM_Instance->getActualFreq(), 4);
42+
Serial.println(dashLine);
43+
}
44+
45+
void setup()
46+
{
47+
Serial.begin(115200);
48+
49+
while (!Serial && millis() < 5000);
50+
51+
delay(500);
52+
53+
Serial.print(F("\nStarting PWM_DynamicDutyCycle_Int on "));
54+
Serial.println(BOARD_NAME);
55+
Serial.println(NRF52_PWM_VERSION);
56+
57+
frequency = 1000.0f;
58+
59+
PWM_Instance = new nRF52_PWM(pinToUse, frequency, 0.0f);
60+
61+
if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
62+
{
63+
Serial.print(F("Stop here forever"));
64+
65+
while (true)
66+
delay(10000);
67+
}
68+
69+
Serial.println(dashLine);
70+
}
71+
72+
void loop()
73+
{
74+
frequency = 5000.0f;
75+
76+
// 50% dutyCycle = (real_dutyCycle * 65536) / 100
77+
dutyCycle = 32768;
78+
79+
Serial.print(F("Change PWM DutyCycle to (%) "));
80+
Serial.println((float) dutyCycle * 100 / 65536);
81+
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);
82+
83+
printPWMInfo(PWM_Instance);
84+
85+
delay(5000);
86+
87+
// 20% dutyCycle = (real_dutyCycle * 65536) / 100
88+
dutyCycle = 13107;
89+
90+
Serial.print(F("Change PWM DutyCycle to (%) "));
91+
Serial.println((float) dutyCycle * 100 / 65536);
92+
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);
93+
printPWMInfo(PWM_Instance);
94+
95+
delay(5000);
96+
}

0 commit comments

Comments
 (0)