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

Commit ff713bf

Browse files
authored
v1.0.0 for hardware-PWM on AVR boards
### Initial Releases v1.0.0 1. Initial coding to support **AVR boards, such as Mega-2560, UNO, Nano, Leonardo, etc.**, etc. using AVR core 2. The hardware-based PWM channels can generate very high frequencies.
1 parent c71fc7f commit ff713bf

File tree

17 files changed

+3171
-0
lines changed

17 files changed

+3171
-0
lines changed

CONTRIBUTING.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## Contributing to AVR_PWM
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in AVR_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/AVR_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/AVR_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 / Adafruit / Sparkfun `AVR` Core Version (e.g. Arduino AVR core v1.8.5)
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 AVR core v1.8.5
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 using this library
36+
Steps to reproduce:
37+
1. ...
38+
2. ...
39+
3. ...
40+
4. ...
41+
```
42+
43+
### Additional context
44+
45+
Add any other context about the problem here.
46+
47+
---
48+
49+
### Sending Feature Requests
50+
51+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
52+
53+
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/AVR_PWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
54+
55+
---
56+
57+
### Sending Pull Requests
58+
59+
Pull Requests with changes and fixes are also welcome!
60+
61+
Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)
62+
63+
1. Change directory to the library GitHub
64+
65+
```
66+
xy@xy-Inspiron-3593:~$ cd Arduino/xy/AVR_PWM_GitHub/
67+
xy@xy-Inspiron-3593:~/Arduino/xy/AVR_PWM_GitHub$
68+
```
69+
70+
2. Issue astyle command
71+
72+
```
73+
xy@xy-Inspiron-3593:~/Arduino/xy/AVR_PWM_GitHub$ bash utils/restyle.sh
74+
```
75+
76+

changelog.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# AVR_PWM Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/AVR_PWM.svg?)](https://www.ardu-badge.com/AVR_PWM)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/AVR_PWM.svg)](https://github.com/khoih-prog/AVR_PWM/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/AVR_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/AVR_PWM.svg)](http://github.com/khoih-prog/AVR_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 **AVR boards, such as Mega-2560, UNO, Nano, Leonardo, etc.**, etc. using AVR core
25+
2. The hardware-based PWM channels 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

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/****************************************************************************************************************************
2+
PWM_Basic.ino
3+
4+
For AVR-based boards (UNO, Nano, Mega, 32U4, 16U4, etc. )
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/AVR_PWM
8+
Licensed under MIT license
9+
10+
This is pure hardware-based PWM
11+
*****************************************************************************************************************************/
12+
/******************************************************************************************************************************
13+
// For UNO / Nano
14+
Timer0 ( 8-bit) used by delay(), millis() and micros(), and PWM generation on pins 5 (6 not usable)
15+
Timer1 (16-bit) used by the Servo.h library and PWM generation on pins 9 and 10
16+
Timer2 ( 8-bit) used by Tone() and PWM generation on pins 3 and 11
17+
// For Mega
18+
Timer0 ( 8-bit) used by delay(), millis() and micros(), and PWM generation on pins 4 (13 not usable)
19+
Timer1 (16-bit) used by the Servo.h library and PWM generation on pins 11, 12
20+
Timer2 ( 8-bit) used by Tone() and PWM generation on pins 9 and 10
21+
Timer3 (16-bit) used by PWM generation on pins 2, 3 and 5
22+
Timer4 (16-bit) used by PWM generation on pins 6, 7 and 8
23+
Timer5 (16-bit) used by PWM generation on pins 44, 45 and 46
24+
25+
////////////////////////////////////////////
26+
// For Mega (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46)
27+
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
28+
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
29+
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
30+
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
31+
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
32+
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
33+
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
34+
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
35+
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
36+
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
37+
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
38+
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
39+
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
40+
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
41+
Pin 46 => TIMER5A // PL 3 ** 46 ** D46
42+
////////////////////////////////////////////
43+
// For 32u4 (3, 5, 6, 9, 10, 11, 13)
44+
Pin 3 => TIMER0B
45+
Pin 5 => TIMER3A
46+
Pin 6 => TIMER4D
47+
Pin 9 => TIMER1A
48+
Pin 10 => TIMER1B
49+
Pin 11 => TIMER0A
50+
Pin 13 => TIMER4A
51+
////////////////////////////////////////////
52+
// For UNO, Nano (3, 5, 6, 9, 10, 11)
53+
Pin 3 => TIMER2B,
54+
Pin 5 => TIMER0B
55+
Pin 6 => TIMER0A
56+
Pin 9 => TIMER1A
57+
Pin 10 => TIMER1B
58+
Pin 11 => TIMER2(A)
59+
******************************************************************************************************************************/
60+
61+
#define _PWM_LOGLEVEL_ 4
62+
63+
#include "AVR_PWM.h"
64+
65+
#if ( PWM_USING_ATMEGA2560 )
66+
// Pins tested OK in Mega
67+
//#define pinToUse 12 // Timer1B on Mega
68+
//#define pinToUse 11 // Timer1A on Mega
69+
//#define pinToUse 9 // Timer2B on Mega
70+
//#define pinToUse 2 // Timer3B on Mega
71+
//#define pinToUse 3 // Timer3C on Mega
72+
//#define pinToUse 5 // Timer3A on Mega
73+
//#define pinToUse 6 // Timer4A on Mega
74+
//#define pinToUse 7 // Timer4B on Mega
75+
#define pinToUse 8 // Timer4C on Mega
76+
//#define pinToUse 46 // Timer5A on Mega
77+
//#define pinToUse 45 // Timer5B on Mega
78+
//#define pinToUse 44 // Timer5C on Mega
79+
80+
#elif ( PWM_USING_ATMEGA_32U4 )
81+
// Pins tested OK on 32u4
82+
//#define pinToUse 5 // Timer3A on 32u4
83+
#define pinToUse 9 // Timer1A on 32u4
84+
//#define pinToUse 10 // Timer1B on 32u4
85+
86+
#else
87+
88+
// Pins tested OK on Nano / UNO
89+
//#define pinToUse 9 // Timer1A on UNO, Nano, etc
90+
#define pinToUse 10 // Timer1B on UNO, Nano, etc
91+
//#define pinToUse 5 // Timer0B on UNO, Nano, e
92+
//#define pinToUse 3 // Timer2B on UNO, Nano, etc
93+
#endif
94+
95+
//creates pwm instance
96+
AVR_PWM* PWM_Instance;
97+
98+
float frequency;
99+
float dutyCycle;
100+
101+
void setup()
102+
{
103+
Serial.print(F("\nStarting PWM_Basic on "));
104+
Serial.println(BOARD_NAME);
105+
Serial.println(AVR_PWM_VERSION);
106+
107+
//assigns PWM frequency of 20 KHz and a duty cycle of 0%
108+
PWM_Instance = new AVR_PWM(pinToUse, 20000, 0);
109+
}
110+
111+
void loop()
112+
{
113+
delay(10000);
114+
frequency = 20000;
115+
dutyCycle = 90;
116+
117+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
118+
119+
delay(10000);
120+
dutyCycle = 10;
121+
122+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
123+
}

0 commit comments

Comments
 (0)