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

Commit 7c73a23

Browse files
authored
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.
1 parent 25c67e4 commit 7c73a23

File tree

17 files changed

+1984
-0
lines changed

17 files changed

+1984
-0
lines changed

CONTRIBUTING.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## Contributing to megaAVR_PWM
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in megaAVR_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/megaAVR_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/megaAVR_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+
* `Arduino megaAVR` or `MegaCoreX` Core Version (e.g. Arduino megaAVR core v1.8.7 or MegaCoreX core v1.1.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+
Arduino megaAVR Core Version 1.8.7
31+
OS: Ubuntu 20.04 LTS
32+
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
33+
34+
Context:
35+
I encountered a crash while trying to use the Timer Interrupt.
36+
37+
Steps to reproduce:
38+
1. ...
39+
2. ...
40+
3. ...
41+
4. ...
42+
```
43+
44+
### Additional context
45+
46+
Add any other context about the problem here.
47+
48+
---
49+
50+
### Sending Feature Requests
51+
52+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
53+
54+
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.
55+
56+
---
57+
58+
### Sending Pull Requests
59+
60+
Pull Requests with changes and fixes are also welcome!
61+
62+
Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)
63+
64+
1. Change directory to the library GitHub
65+
66+
```
67+
xy@xy-Inspiron-3593:~$ cd Arduino/xy/megaAVR_PWM_GitHub/
68+
xy@xy-Inspiron-3593:~/Arduino/xy/megaAVR_PWM_GitHub$
69+
```
70+
71+
2. Issue astyle command
72+
73+
```
74+
xy@xy-Inspiron-3593:~/Arduino/xy/megaAVR_PWM_GitHub$ bash utils/restyle.sh
75+
```

changelog.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# megaAVR_PWM Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/megaAVR_PWM.svg?)](https://www.ardu-badge.com/megaAVR_PWM)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/megaAVR_PWM.svg)](https://github.com/khoih-prog/megaAVR_PWM/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/megaAVR_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/megaAVR_PWM.svg)](http://github.com/khoih-prog/megaAVR_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 **megaAVR boards, such as UNO WiFi Rev2, AVR_Nano_Every, etc.**, using `Arduino megaAVR` or `MegaCoreX` core
25+
2. The hardware-based PWM channels using TimerB can generate very high frequencies.
26+
27+
---
28+
---
29+
30+
## Copyright
31+
32+
Copyright 2022- Khoi Hoang
33+
34+

examples/PWM_Basic/PWM_Basic.ino

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/****************************************************************************************************************************
2+
PWM_Basic.ino
3+
4+
For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. )
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/megaAVR_PWM
8+
Licensed under MIT license
9+
10+
This is pure hardware-based PWM
11+
*****************************************************************************************************************************/
12+
/******************************************************************************************************************************
13+
Pins can be used for hardware-PWM
14+
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.)
15+
TCA0 (16-bit) used by PWM generation on pins 5, 9 and 10
16+
TCB0 (16-bit) used by PWM generation on pin 6
17+
TCB1 (16-bit) used by PWM generation on pin 3
18+
TCB2 (16-bit)
19+
TCB3 (16-bit)
20+
////////////////////////////////////////////
21+
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.)
22+
Pin 3 => TIMERB1, // 3 PF5, 8-bit PWM, 16-bit counter
23+
Pin 5 => TIMERA0, // 5 PB2, 16-bit PWM, 16-bit counter
24+
Pin 6 => TIMERB0, // 6 PF4, 8-bit PWM, 16-bit counter
25+
Pin 9 => TIMERA0, // 9 PB0, 16-bit PWM, 16-bit counter
26+
Pin 10 => TIMERA0, // 10 PB1, 16-bit PWM, 16-bit counter
27+
////////////////////////////////////////////
28+
******************************************************************************************************************************/
29+
30+
#define _PWM_LOGLEVEL_ 4
31+
32+
#include "megaAVR_PWM.h"
33+
34+
#define USING_TIMERB true
35+
36+
#if USING_TIMERB
37+
// Pins tested OK in Nano Every ATmega4809
38+
#define pinToUse 3 // TimerB1, for higher frequencies, up to 100KHz
39+
//#define pinToUse 6 // TimerB0, for higher frequencies, up to 100KHz
40+
#elif USING_ARDUINO_MEGA_AVR_CORE
41+
// Pins tested OK in Nano Every ATmega4809 using Arduino megaAVR core
42+
// TimerA0 somehow can't be used with MegaCoreX
43+
#define pinToUse 5 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
44+
//#define pinToUse 9 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
45+
//#define pinToUse 10 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
46+
#else
47+
#error TimerA0 to be used with Arduino megaAVR Core
48+
#endif
49+
50+
////////////////////////////////////////////
51+
52+
//creates pwm instance
53+
megaAVR_PWM* PWM_Instance;
54+
55+
float frequency;
56+
float dutyCycle;
57+
58+
////////////////////////////////////////////
59+
60+
void setup()
61+
{
62+
Serial.begin(115200);
63+
64+
while (!Serial && millis() < 5000);
65+
66+
//delay(1000);
67+
68+
Serial.print(F("\nStarting PWM_Basic on "));
69+
Serial.println(BOARD_NAME);
70+
Serial.println(MEGA_AVR_PWM_VERSION);
71+
72+
//assigns PWM frequency of 1KHz and a duty cycle of 0%
73+
PWM_Instance = new megaAVR_PWM(pinToUse, 1000, 0);
74+
}
75+
76+
void loop()
77+
{
78+
#if USING_TIMERB
79+
// Using higher Hz for TimerB, min is 980.34 Hz
80+
frequency = 40000;
81+
//frequency = 1000;
82+
#else
83+
// Using much lower Hz for TimerA
84+
frequency = 1000;
85+
#endif
86+
87+
dutyCycle = 30;
88+
89+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
90+
91+
delay(5000);
92+
dutyCycle = 90;
93+
94+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
95+
96+
//while (true)
97+
delay(5000);
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/****************************************************************************************************************************
2+
PWM_DynamicDutyCycle.ino
3+
For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. )
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/megaAVR_PWM
7+
Licensed under MIT license
8+
9+
This is pure hardware-based PWM
10+
*****************************************************************************************************************************/
11+
/******************************************************************************************************************************
12+
Pins can be used for hardware-PWM
13+
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.)
14+
TCA0 (16-bit) used by PWM generation on pins 5, 9 and 10
15+
TCB0 (16-bit) used by PWM generation on pin 6
16+
TCB1 (16-bit) used by PWM generation on pin 3
17+
TCB2 (16-bit)
18+
TCB3 (16-bit)
19+
////////////////////////////////////////////
20+
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.)
21+
Pin 3 => TIMERB1, // 3 PF5, 8-bit PWM, 16-bit counter
22+
Pin 5 => TIMERA0, // 5 PB2, 16-bit PWM, 16-bit counter
23+
Pin 6 => TIMERB0, // 6 PF4, 8-bit PWM, 16-bit counter
24+
Pin 9 => TIMERA0, // 9 PB0, 16-bit PWM, 16-bit counter
25+
Pin 10 => TIMERA0, // 10 PB1, 16-bit PWM, 16-bit counter
26+
////////////////////////////////////////////
27+
******************************************************************************************************************************/
28+
29+
#define _PWM_LOGLEVEL_ 4
30+
31+
#include "megaAVR_PWM.h"
32+
33+
#define USING_TIMERB true
34+
35+
#if USING_TIMERB
36+
// Pins tested OK in Nano Every ATmega4809
37+
#define pinToUse 3 // TimerB1, for higher frequencies, up to 100KHz
38+
//#define pinToUse 6 // TimerB0, for higher frequencies, up to 100KHz
39+
#elif USING_ARDUINO_MEGA_AVR_CORE
40+
// Pins tested OK in Nano Every ATmega4809 using Arduino megaAVR core
41+
// TimerA0 somehow can't be used with MegaCoreX
42+
#define pinToUse 5 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
43+
//#define pinToUse 9 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
44+
//#define pinToUse 10 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
45+
#else
46+
#error TimerA0 to be used with Arduino megaAVR Core
47+
#endif
48+
49+
////////////////////////////////////////////
50+
51+
//creates pwm instance
52+
megaAVR_PWM* PWM_Instance;
53+
54+
float frequency;
55+
float dutyCycle;
56+
57+
char dashLine[] = "=====================================================================================";
58+
59+
void printPWMInfo(megaAVR_PWM* PWM_Instance)
60+
{
61+
Serial.println(dashLine);
62+
Serial.print("Actual data: pin = ");
63+
Serial.print(PWM_Instance->getPin());
64+
Serial.print(", PWM DC = ");
65+
Serial.print(PWM_Instance->getActualDutyCycle());
66+
Serial.print(", PWMPeriod = ");
67+
Serial.print(PWM_Instance->getPWMPeriod());
68+
Serial.print(", PWM Freq (Hz) = ");
69+
Serial.println(PWM_Instance->getActualFreq(), 4);
70+
Serial.println(dashLine);
71+
}
72+
73+
void setup()
74+
{
75+
Serial.begin(115200);
76+
77+
while (!Serial && millis() < 5000);
78+
79+
//delay(1000);
80+
81+
Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
82+
Serial.println(BOARD_NAME);
83+
Serial.println(MEGA_AVR_PWM_VERSION);
84+
85+
frequency = 5000;
86+
87+
PWM_Instance = new megaAVR_PWM(pinToUse, frequency, 50);
88+
89+
if (PWM_Instance)
90+
{
91+
if (!PWM_Instance->setPWM())
92+
{
93+
Serial.println(F("Stop here"));
94+
95+
// stop here
96+
while (true)
97+
delay(1000);
98+
}
99+
}
100+
101+
Serial.println(dashLine);
102+
}
103+
104+
void loop()
105+
{
106+
//frequency = 200000.0f;
107+
frequency = 1000.0f;
108+
109+
dutyCycle = 90.0f;
110+
111+
Serial.print(F("Change PWM DutyCycle to "));
112+
Serial.println(dutyCycle);
113+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
114+
115+
printPWMInfo(PWM_Instance);
116+
117+
delay(5000);
118+
dutyCycle = 10.0f;
119+
120+
Serial.print(F("Change PWM DutyCycle to "));
121+
Serial.println(dutyCycle);
122+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
123+
printPWMInfo(PWM_Instance);
124+
125+
delay(5000);
126+
}

0 commit comments

Comments
 (0)