This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.0.0 for hardware-PWM on megaAVR boards
### Initial Releases v1.0.0 1. Initial coding to support **megaAVR boards, such as UNO WiFi Rev2, AVR_Nano_Every, etc.**, using `Arduino megaAVR` or `MegaCoreX` core 2. The hardware-based PWM channels using TimerB can generate very high frequencies.
- Loading branch information
1 parent
25c67e4
commit 7c73a23
Showing
17 changed files
with
1,984 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
## Contributing to megaAVR_PWM | ||
|
||
### Reporting Bugs | ||
|
||
Please report bugs in megaAVR_PWM if you find them. | ||
|
||
However, before reporting a bug please check through the following: | ||
|
||
* [Existing Open Issues](https://github.com/khoih-prog/megaAVR_PWM/issues) - someone might have already encountered this. | ||
|
||
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/megaAVR_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 | ||
* `Arduino megaAVR` or `MegaCoreX` Core Version (e.g. Arduino megaAVR core v1.8.7 or MegaCoreX core v1.1.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 | ||
Arduino megaAVR Core Version 1.8.7 | ||
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 trying to use the Timer Interrupt. | ||
Steps to reproduce: | ||
1. ... | ||
2. ... | ||
3. ... | ||
4. ... | ||
``` | ||
|
||
### Additional context | ||
|
||
Add any other context about the problem here. | ||
|
||
--- | ||
|
||
### 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/megaAVR_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/megaAVR_PWM_GitHub/ | ||
xy@xy-Inspiron-3593:~/Arduino/xy/megaAVR_PWM_GitHub$ | ||
``` | ||
|
||
2. Issue astyle command | ||
|
||
``` | ||
xy@xy-Inspiron-3593:~/Arduino/xy/megaAVR_PWM_GitHub$ bash utils/restyle.sh | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# megaAVR_PWM Library | ||
|
||
[![arduino-library-badge](https://www.ardu-badge.com/badge/megaAVR_PWM.svg?)](https://www.ardu-badge.com/megaAVR_PWM) | ||
[![GitHub release](https://img.shields.io/github/release/khoih-prog/megaAVR_PWM.svg)](https://github.com/khoih-prog/megaAVR_PWM/releases) | ||
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/megaAVR_PWM/blob/main/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/megaAVR_PWM.svg)](http://github.com/khoih-prog/megaAVR_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 **megaAVR boards, such as UNO WiFi Rev2, AVR_Nano_Every, etc.**, using `Arduino megaAVR` or `MegaCoreX` core | ||
2. The hardware-based PWM channels using TimerB can generate very high frequencies. | ||
|
||
--- | ||
--- | ||
|
||
## Copyright | ||
|
||
Copyright 2022- Khoi Hoang | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/**************************************************************************************************************************** | ||
PWM_Basic.ino | ||
For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. ) | ||
Written by Khoi Hoang | ||
Built by Khoi Hoang https://github.com/khoih-prog/megaAVR_PWM | ||
Licensed under MIT license | ||
This is pure hardware-based PWM | ||
*****************************************************************************************************************************/ | ||
/****************************************************************************************************************************** | ||
Pins can be used for hardware-PWM | ||
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.) | ||
TCA0 (16-bit) used by PWM generation on pins 5, 9 and 10 | ||
TCB0 (16-bit) used by PWM generation on pin 6 | ||
TCB1 (16-bit) used by PWM generation on pin 3 | ||
TCB2 (16-bit) | ||
TCB3 (16-bit) | ||
//////////////////////////////////////////// | ||
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.) | ||
Pin 3 => TIMERB1, // 3 PF5, 8-bit PWM, 16-bit counter | ||
Pin 5 => TIMERA0, // 5 PB2, 16-bit PWM, 16-bit counter | ||
Pin 6 => TIMERB0, // 6 PF4, 8-bit PWM, 16-bit counter | ||
Pin 9 => TIMERA0, // 9 PB0, 16-bit PWM, 16-bit counter | ||
Pin 10 => TIMERA0, // 10 PB1, 16-bit PWM, 16-bit counter | ||
//////////////////////////////////////////// | ||
******************************************************************************************************************************/ | ||
|
||
#define _PWM_LOGLEVEL_ 4 | ||
|
||
#include "megaAVR_PWM.h" | ||
|
||
#define USING_TIMERB true | ||
|
||
#if USING_TIMERB | ||
// Pins tested OK in Nano Every ATmega4809 | ||
#define pinToUse 3 // TimerB1, for higher frequencies, up to 100KHz | ||
//#define pinToUse 6 // TimerB0, for higher frequencies, up to 100KHz | ||
#elif USING_ARDUINO_MEGA_AVR_CORE | ||
// Pins tested OK in Nano Every ATmega4809 using Arduino megaAVR core | ||
// TimerA0 somehow can't be used with MegaCoreX | ||
#define pinToUse 5 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock | ||
//#define pinToUse 9 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock | ||
//#define pinToUse 10 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock | ||
#else | ||
#error TimerA0 to be used with Arduino megaAVR Core | ||
#endif | ||
|
||
//////////////////////////////////////////// | ||
|
||
//creates pwm instance | ||
megaAVR_PWM* PWM_Instance; | ||
|
||
float frequency; | ||
float dutyCycle; | ||
|
||
//////////////////////////////////////////// | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
|
||
while (!Serial && millis() < 5000); | ||
|
||
//delay(1000); | ||
|
||
Serial.print(F("\nStarting PWM_Basic on ")); | ||
Serial.println(BOARD_NAME); | ||
Serial.println(MEGA_AVR_PWM_VERSION); | ||
|
||
//assigns PWM frequency of 1KHz and a duty cycle of 0% | ||
PWM_Instance = new megaAVR_PWM(pinToUse, 1000, 0); | ||
} | ||
|
||
void loop() | ||
{ | ||
#if USING_TIMERB | ||
// Using higher Hz for TimerB, min is 980.34 Hz | ||
frequency = 40000; | ||
//frequency = 1000; | ||
#else | ||
// Using much lower Hz for TimerA | ||
frequency = 1000; | ||
#endif | ||
|
||
dutyCycle = 30; | ||
|
||
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle); | ||
|
||
delay(5000); | ||
dutyCycle = 90; | ||
|
||
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle); | ||
|
||
//while (true) | ||
delay(5000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/**************************************************************************************************************************** | ||
PWM_DynamicDutyCycle.ino | ||
For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. ) | ||
Written by Khoi Hoang | ||
Built by Khoi Hoang https://github.com/khoih-prog/megaAVR_PWM | ||
Licensed under MIT license | ||
This is pure hardware-based PWM | ||
*****************************************************************************************************************************/ | ||
/****************************************************************************************************************************** | ||
Pins can be used for hardware-PWM | ||
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.) | ||
TCA0 (16-bit) used by PWM generation on pins 5, 9 and 10 | ||
TCB0 (16-bit) used by PWM generation on pin 6 | ||
TCB1 (16-bit) used by PWM generation on pin 3 | ||
TCB2 (16-bit) | ||
TCB3 (16-bit) | ||
//////////////////////////////////////////// | ||
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.) | ||
Pin 3 => TIMERB1, // 3 PF5, 8-bit PWM, 16-bit counter | ||
Pin 5 => TIMERA0, // 5 PB2, 16-bit PWM, 16-bit counter | ||
Pin 6 => TIMERB0, // 6 PF4, 8-bit PWM, 16-bit counter | ||
Pin 9 => TIMERA0, // 9 PB0, 16-bit PWM, 16-bit counter | ||
Pin 10 => TIMERA0, // 10 PB1, 16-bit PWM, 16-bit counter | ||
//////////////////////////////////////////// | ||
******************************************************************************************************************************/ | ||
|
||
#define _PWM_LOGLEVEL_ 4 | ||
|
||
#include "megaAVR_PWM.h" | ||
|
||
#define USING_TIMERB true | ||
|
||
#if USING_TIMERB | ||
// Pins tested OK in Nano Every ATmega4809 | ||
#define pinToUse 3 // TimerB1, for higher frequencies, up to 100KHz | ||
//#define pinToUse 6 // TimerB0, for higher frequencies, up to 100KHz | ||
#elif USING_ARDUINO_MEGA_AVR_CORE | ||
// Pins tested OK in Nano Every ATmega4809 using Arduino megaAVR core | ||
// TimerA0 somehow can't be used with MegaCoreX | ||
#define pinToUse 5 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock | ||
//#define pinToUse 9 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock | ||
//#define pinToUse 10 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock | ||
#else | ||
#error TimerA0 to be used with Arduino megaAVR Core | ||
#endif | ||
|
||
//////////////////////////////////////////// | ||
|
||
//creates pwm instance | ||
megaAVR_PWM* PWM_Instance; | ||
|
||
float frequency; | ||
float dutyCycle; | ||
|
||
char dashLine[] = "====================================================================================="; | ||
|
||
void printPWMInfo(megaAVR_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(1000); | ||
|
||
Serial.print(F("\nStarting PWM_DynamicDutyCycle on ")); | ||
Serial.println(BOARD_NAME); | ||
Serial.println(MEGA_AVR_PWM_VERSION); | ||
|
||
frequency = 5000; | ||
|
||
PWM_Instance = new megaAVR_PWM(pinToUse, frequency, 50); | ||
|
||
if (PWM_Instance) | ||
{ | ||
if (!PWM_Instance->setPWM()) | ||
{ | ||
Serial.println(F("Stop here")); | ||
|
||
// stop here | ||
while (true) | ||
delay(1000); | ||
} | ||
} | ||
|
||
Serial.println(dashLine); | ||
} | ||
|
||
void loop() | ||
{ | ||
//frequency = 200000.0f; | ||
frequency = 1000.0f; | ||
|
||
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 = 10.0f; | ||
|
||
Serial.print(F("Change PWM DutyCycle to ")); | ||
Serial.println(dutyCycle); | ||
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle); | ||
printPWMInfo(PWM_Instance); | ||
|
||
delay(5000); | ||
} |
Oops, something went wrong.