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 AVR boards
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
khoih-prog authored Oct 29, 2022
1 parent c71fc7f commit ff713bf
Show file tree
Hide file tree
Showing 17 changed files with 3,171 additions and 0 deletions.
76 changes: 76 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## Contributing to AVR_PWM

### Reporting Bugs

Please report bugs in AVR_PWM if you find them.

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

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

If you don't find anything, please [open a new issue](https://github.com/khoih-prog/AVR_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 / Adafruit / Sparkfun `AVR` Core Version (e.g. Arduino AVR core v1.8.5)
* 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 AVR core v1.8.5
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. ...
```

### 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/AVR_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/AVR_PWM_GitHub/
xy@xy-Inspiron-3593:~/Arduino/xy/AVR_PWM_GitHub$
```

2. Issue astyle command

```
xy@xy-Inspiron-3593:~/Arduino/xy/AVR_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 @@
# AVR_PWM Library

[![arduino-library-badge](https://www.ardu-badge.com/badge/AVR_PWM.svg?)](https://www.ardu-badge.com/AVR_PWM)
[![GitHub release](https://img.shields.io/github/release/khoih-prog/AVR_PWM.svg)](https://github.com/khoih-prog/AVR_PWM/releases)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/AVR_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/AVR_PWM.svg)](http://github.com/khoih-prog/AVR_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 **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.

---
---

## Copyright

Copyright 2022- Khoi Hoang


123 changes: 123 additions & 0 deletions examples/PWM_Basic/PWM_Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/****************************************************************************************************************************
PWM_Basic.ino
For AVR-based boards (UNO, Nano, Mega, 32U4, 16U4, etc. )
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/AVR_PWM
Licensed under MIT license
This is pure hardware-based PWM
*****************************************************************************************************************************/
/******************************************************************************************************************************
// For UNO / Nano
Timer0 ( 8-bit) used by delay(), millis() and micros(), and PWM generation on pins 5 (6 not usable)
Timer1 (16-bit) used by the Servo.h library and PWM generation on pins 9 and 10
Timer2 ( 8-bit) used by Tone() and PWM generation on pins 3 and 11
// For Mega
Timer0 ( 8-bit) used by delay(), millis() and micros(), and PWM generation on pins 4 (13 not usable)
Timer1 (16-bit) used by the Servo.h library and PWM generation on pins 11, 12
Timer2 ( 8-bit) used by Tone() and PWM generation on pins 9 and 10
Timer3 (16-bit) used by PWM generation on pins 2, 3 and 5
Timer4 (16-bit) used by PWM generation on pins 6, 7 and 8
Timer5 (16-bit) used by PWM generation on pins 44, 45 and 46
////////////////////////////////////////////
// For Mega (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46)
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 46 => TIMER5A // PL 3 ** 46 ** D46
////////////////////////////////////////////
// For 32u4 (3, 5, 6, 9, 10, 11, 13)
Pin 3 => TIMER0B
Pin 5 => TIMER3A
Pin 6 => TIMER4D
Pin 9 => TIMER1A
Pin 10 => TIMER1B
Pin 11 => TIMER0A
Pin 13 => TIMER4A
////////////////////////////////////////////
// For UNO, Nano (3, 5, 6, 9, 10, 11)
Pin 3 => TIMER2B,
Pin 5 => TIMER0B
Pin 6 => TIMER0A
Pin 9 => TIMER1A
Pin 10 => TIMER1B
Pin 11 => TIMER2(A)
******************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 4

#include "AVR_PWM.h"

#if ( PWM_USING_ATMEGA2560 )
// Pins tested OK in Mega
//#define pinToUse 12 // Timer1B on Mega
//#define pinToUse 11 // Timer1A on Mega
//#define pinToUse 9 // Timer2B on Mega
//#define pinToUse 2 // Timer3B on Mega
//#define pinToUse 3 // Timer3C on Mega
//#define pinToUse 5 // Timer3A on Mega
//#define pinToUse 6 // Timer4A on Mega
//#define pinToUse 7 // Timer4B on Mega
#define pinToUse 8 // Timer4C on Mega
//#define pinToUse 46 // Timer5A on Mega
//#define pinToUse 45 // Timer5B on Mega
//#define pinToUse 44 // Timer5C on Mega

#elif ( PWM_USING_ATMEGA_32U4 )
// Pins tested OK on 32u4
//#define pinToUse 5 // Timer3A on 32u4
#define pinToUse 9 // Timer1A on 32u4
//#define pinToUse 10 // Timer1B on 32u4

#else

// Pins tested OK on Nano / UNO
//#define pinToUse 9 // Timer1A on UNO, Nano, etc
#define pinToUse 10 // Timer1B on UNO, Nano, etc
//#define pinToUse 5 // Timer0B on UNO, Nano, e
//#define pinToUse 3 // Timer2B on UNO, Nano, etc
#endif

//creates pwm instance
AVR_PWM* PWM_Instance;

float frequency;
float dutyCycle;

void setup()
{
Serial.print(F("\nStarting PWM_Basic on "));
Serial.println(BOARD_NAME);
Serial.println(AVR_PWM_VERSION);

//assigns PWM frequency of 20 KHz and a duty cycle of 0%
PWM_Instance = new AVR_PWM(pinToUse, 20000, 0);
}

void loop()
{
delay(10000);
frequency = 20000;
dutyCycle = 90;

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

delay(10000);
dutyCycle = 10;

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

0 comments on commit ff713bf

Please sign in to comment.