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

Commit

Permalink
v1.0.0 for hardware-PWM on nRF52 boards
Browse files Browse the repository at this point in the history
### 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)
  • Loading branch information
khoih-prog authored Nov 5, 2022
1 parent 41012e0 commit 2017789
Show file tree
Hide file tree
Showing 17 changed files with 2,082 additions and 0 deletions.
73 changes: 73 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## Contributing to nRF52_PWM

### Reporting Bugs

Please report bugs in nRF52_PWM if you find them.

However, before reporting a bug please check through the following:

* [Existing Open Issues](https://github.com/khoih-prog/nRF52_PWM/issues) - someone might have already encountered this.

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

### How to submit a bug report

Please ensure to specify the following:

* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `NRF52` Core Version (e.g. Adafruit NRF52 core v1.3.0, Seeed nRF52 core v1.0.0)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
* Anything that might be relevant in your opinion, such as:
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
* Network configuration


### Example

```
Arduino IDE version: 1.8.19
Adafruit NRF52 Core Version 1.3.0
NRF52840_ITSYBITSY
OS: Ubuntu 20.04 LTS
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
Context:
I encountered a crash while using this library
Steps to reproduce:
1. ...
2. ...
3. ...
4. ...
```

---

### Sending Feature Requests

Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.

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.

---

### Sending Pull Requests

Pull Requests with changes and fixes are also welcome!

Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)

1. Change directory to the library GitHub

```
xy@xy-Inspiron-3593:~$ cd Arduino/xy/nRF52_PWM_GitHub/
xy@xy-Inspiron-3593:~/Arduino/xy/nRF52_PWM_GitHub$
```

2. Issue astyle command

```
xy@xy-Inspiron-3593:~/Arduino/xy/nRF52_PWM_GitHub$ bash utils/restyle.sh
```

34 changes: 34 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# nRF52_PWM Library

[![arduino-library-badge](https://www.ardu-badge.com/badge/nRF52_PWM.svg?)](https://www.ardu-badge.com/nRF52_PWM)
[![GitHub release](https://img.shields.io/github/release/khoih-prog/nRF52_PWM.svg)](https://github.com/khoih-prog/nRF52_PWM/releases)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/nRF52_PWM/blob/master/LICENSE)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/nRF52_PWM.svg)](http://github.com/khoih-prog/nRF52_PWM/issues)

---
---

## Table of Contents

* [Changelog](#changelog)
* [Initial Releases v1.0.0](#Initial-Releases-v100)

---
---

## Changelog

### 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)


---
---

## Copyright

Copyright 2022- Khoi Hoang


74 changes: 74 additions & 0 deletions examples/PWM_Basic/PWM_Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/****************************************************************************************************************************
PWM_Basic.ino
For nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_PWM
Licensed under MIT license
*****************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 4

// Select false to use PWM
#define USING_TIMER false //true

#include "nRF52_PWM.h"

// OK for Feather_nRF52840_Express (5, 6, 9-13, 14-21/A0-A7, etc.)
// OK for ItsyBitsy_nRF52840_Express (5, 7, 9-13, 14-20/A0-A6, etc.)

#define pinToUse 5

//creates pwm instance
nRF52_PWM* PWM_Instance;

float frequency = 1000.0f;

float dutyCycle = 0.0f;

void setup()
{
Serial.begin(115200);

while (!Serial && millis() < 5000);

delay(500);

Serial.print(F("\nStarting PWM_Basic using PWM on "));
Serial.println(BOARD_NAME);
Serial.println(NRF52_PWM_VERSION);

//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%
PWM_Instance = new nRF52_PWM(pinToUse, frequency, dutyCycle);

if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
{
Serial.print(F("Stop here forever"));

while (true)
delay(10000);
}
}

void loop()
{
// You can change frequency here, anytime
frequency = 2000.0f;
//frequency = 20.0f;
dutyCycle = 20.0f;

PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);

delay(10000);

// You can change frequency here, anytime
frequency = 5000.0f;
//frequency = 50.0f;
dutyCycle = 90.0f;

PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);

//while (1)
delay(10000);
}
90 changes: 90 additions & 0 deletions examples/PWM_DynamicDutyCycle/PWM_DynamicDutyCycle.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/****************************************************************************************************************************
PWM_DynamicDutyCycle.ino
For nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_PWM
Licensed under MIT license
*****************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 4

// Select false to use PWM
#define USING_TIMER false //true

#include "nRF52_PWM.h"

// OK for Feather_nRF52840_Express (5, 6, 9, 10, 14-21/A0-A7, etc.)

#define pinToUse 6

//creates pwm instance
nRF52_PWM* PWM_Instance;

float frequency;
float dutyCycle;

char dashLine[] = "=====================================================================================";

void printPWMInfo(nRF52_PWM* PWM_Instance)
{
Serial.println(dashLine);
Serial.print("Actual data: pin = ");
Serial.print(PWM_Instance->getPin());
Serial.print(", PWM DC = ");
Serial.print(PWM_Instance->getActualDutyCycle());
Serial.print(", PWMPeriod = ");
Serial.print(PWM_Instance->getPWMPeriod());
Serial.print(", PWM Freq (Hz) = ");
Serial.println(PWM_Instance->getActualFreq(), 4);
Serial.println(dashLine);
}

void setup()
{
Serial.begin(115200);

while (!Serial && millis() < 5000);

delay(500);

Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
Serial.println(BOARD_NAME);
Serial.println(NRF52_PWM_VERSION);

frequency = 5000.0f;

PWM_Instance = new nRF52_PWM(pinToUse, frequency, 0.0f);

if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
{
Serial.print(F("Stop here forever"));

while (true)
delay(10000);
}

Serial.println(dashLine);
}

void loop()
{
dutyCycle = 90.0f;

Serial.print(F("Change PWM DutyCycle to "));
Serial.println(dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);

printPWMInfo(PWM_Instance);

delay(5000);
dutyCycle = 20.0f;

Serial.print(F("Change PWM DutyCycle to "));
Serial.println(dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
printPWMInfo(PWM_Instance);

delay(5000);
}
96 changes: 96 additions & 0 deletions examples/PWM_DynamicDutyCycle_Int/PWM_DynamicDutyCycle_Int.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/****************************************************************************************************************************
PWM_DynamicDutyCycle_Int.ino
For nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_PWM
Licensed under MIT license
*****************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 4

// Select false to use PWM
#define USING_TIMER false //true

#include "nRF52_PWM.h"

// OK for Feather_nRF52840_Express (5, 6, 9-13, 14-21/A0-A7, etc.)
// OK for ItsyBitsy_nRF52840_Express (5, 7, 9-13, 14-20/A0-A6, etc.)

#define pinToUse 5

//creates pwm instance
nRF52_PWM* PWM_Instance;

float frequency;
uint16_t dutyCycle;

char dashLine[] = "=====================================================================================";

void printPWMInfo(nRF52_PWM* PWM_Instance)
{
Serial.println(dashLine);
Serial.print("Actual data: pin = ");
Serial.print(PWM_Instance->getPin());
Serial.print(", PWM DC = ");
Serial.print(PWM_Instance->getActualDutyCycle());
Serial.print(", PWMPeriod = ");
Serial.print(PWM_Instance->getPWMPeriod());
Serial.print(", PWM Freq (Hz) = ");
Serial.println(PWM_Instance->getActualFreq(), 4);
Serial.println(dashLine);
}

void setup()
{
Serial.begin(115200);

while (!Serial && millis() < 5000);

delay(500);

Serial.print(F("\nStarting PWM_DynamicDutyCycle_Int on "));
Serial.println(BOARD_NAME);
Serial.println(NRF52_PWM_VERSION);

frequency = 1000.0f;

PWM_Instance = new nRF52_PWM(pinToUse, frequency, 0.0f);

if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
{
Serial.print(F("Stop here forever"));

while (true)
delay(10000);
}

Serial.println(dashLine);
}

void loop()
{
frequency = 5000.0f;

// 50% dutyCycle = (real_dutyCycle * 65536) / 100
dutyCycle = 32768;

Serial.print(F("Change PWM DutyCycle to (%) "));
Serial.println((float) dutyCycle * 100 / 65536);
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);

printPWMInfo(PWM_Instance);

delay(5000);

// 20% dutyCycle = (real_dutyCycle * 65536) / 100
dutyCycle = 13107;

Serial.print(F("Change PWM DutyCycle to (%) "));
Serial.println((float) dutyCycle * 100 / 65536);
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);
printPWMInfo(PWM_Instance);

delay(5000);
}
Loading

0 comments on commit 2017789

Please sign in to comment.