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 ATtiny boards
Browse files Browse the repository at this point in the history
### Initial Releases v1.0.0

1. Initial coding to support Arduino **AVR ATtiny-based boards (ATtiny3217, etc.)** using [**megaTinyCore**](https://github.com/SpenceKonde/megaTinyCore)
  • Loading branch information
khoih-prog committed Nov 9, 2022
1 parent 46bf5d6 commit 5e2c807
Show file tree
Hide file tree
Showing 20 changed files with 2,015 additions and 0 deletions.
74 changes: 74 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## Contributing to ATtiny_PWM

### Reporting Bugs

Please report bugs in ATtiny_PWM if you find them.

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

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

If you don't find anything, please [open a new issue](https://github.com/khoih-prog/ATtiny_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
* `megaTinyCore` Core Version (e.g. megaTinyCore core v2.6.3)
* Board (e.g. AVR_CuriosityNano3217, etc.)
* 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 megaTinyCore core v2.6.3
OS: Ubuntu 20.04 LTS
Board: AVR_CuriosityNano3217
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/ATtiny_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/ATtiny_PWM_GitHub/
xy@xy-Inspiron-3593:~/Arduino/xy/ATtiny_PWM_GitHub$
```

2. Issue astyle command

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

[![arduino-library-badge](https://www.ardu-badge.com/badge/ATtiny_PWM.svg?)](https://www.ardu-badge.com/ATtiny_PWM)
[![GitHub release](https://img.shields.io/github/release/khoih-prog/ATtiny_PWM.svg)](https://github.com/khoih-prog/ATtiny_PWM/releases)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/ATtiny_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/ATtiny_PWM.svg)](http://github.com/khoih-prog/ATtiny_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 Arduino **AVR ATtiny-based boards (ATtiny3217, etc.)** using [**megaTinyCore**](https://github.com/SpenceKonde/megaTinyCore)


---
---

## Copyright

Copyright 2022- Khoi Hoang


115 changes: 115 additions & 0 deletions examples/PWM_Basic/PWM_Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/****************************************************************************************************************************
PWM_Basic.ino
For Arduino AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ATtiny_PWM
Licensed under MIT license
*****************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 4

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

#include "ATtiny_PWM.h"

/*
const uint8_t digital_pin_to_timer[] = {
// Left side, top to bottom
TIMERA0, // 0 PA4 WO4 WOA
TIMERA0, // 1 PA5 WO5 WOB
#if defined(DAC0)
DACOUT, // 2 PA6
#else
NOT_ON_TIMER, // 2 PA6
#endif
NOT_ON_TIMER, // 3 PA7
NOT_ON_TIMER, // 4 PB7
NOT_ON_TIMER, // 5 PB6
NOT_ON_TIMER, // 6 PB5 WO2 Alt
NOT_ON_TIMER, // 7 PB4 WO1 Alt
NOT_ON_TIMER, // 8 PB3 WO0 Alt
TIMERA0, // 9 PB2 WO2
TIMERA0, // 10 PB1 WO1
// Right side, bottom to top
TIMERA0, // 11 PB0 WO0
#if (defined(TCD0) && defined(USE_TIMERD0_PWM))
TIMERD0, // 12 PC0 WOC
TIMERD0, // 13 PC1 WOD
#else
NOT_ON_TIMER, // 12 PC0
NOT_ON_TIMER, // 13 PC1
#endif
NOT_ON_TIMER, // 14 PC2
NOT_ON_TIMER, // 15 PC3 WO3 Alt
NOT_ON_TIMER, // 16 PC4 WO4 Alt
NOT_ON_TIMER, // 17 PC5 WO5 Alt
NOT_ON_TIMER, // 18 PA1
NOT_ON_TIMER, // 19 PA2
TIMERA0, // 20 PA3 WO3
NOT_ON_TIMER // 21 PA0
};
*/

// OK, only PIN_PA4-5:TCA0
// PIN_PC0-1: TCD0 => not OK yet for frequency
// Not OK, PIN_PA6, 7, PIN_PB0-2:TCA0

#define pinToUse PIN_PA5 //PIN_PB4

//creates pwm instance
ATtiny_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(AT_TINY_PWM_VERSION);

//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%
PWM_Instance = new ATtiny_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);
analogWrite(PIN_PA3, dutyCycle * (1 << 8) / 100);

delay(10000);

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

PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
analogWrite(PIN_PA3, dutyCycle * (1 << 8) / 100);

//while (1)
delay(10000);
}
130 changes: 130 additions & 0 deletions examples/PWM_DynamicDutyCycle/PWM_DynamicDutyCycle.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/****************************************************************************************************************************
PWM_DynamicDutyCycle.ino
For Arduino AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ATtiny_PWM
Licensed under MIT license
*****************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 4

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

#include "ATtiny_PWM.h"

/*
const uint8_t digital_pin_to_timer[] = {
// Left side, top to bottom
TIMERA0, // 0 PA4 WO4 WOA
TIMERA0, // 1 PA5 WO5 WOB
#if defined(DAC0)
DACOUT, // 2 PA6
#else
NOT_ON_TIMER, // 2 PA6
#endif
NOT_ON_TIMER, // 3 PA7
NOT_ON_TIMER, // 4 PB7
NOT_ON_TIMER, // 5 PB6
NOT_ON_TIMER, // 6 PB5 WO2 Alt
NOT_ON_TIMER, // 7 PB4 WO1 Alt
NOT_ON_TIMER, // 8 PB3 WO0 Alt
TIMERA0, // 9 PB2 WO2
TIMERA0, // 10 PB1 WO1
// Right side, bottom to top
TIMERA0, // 11 PB0 WO0
#if (defined(TCD0) && defined(USE_TIMERD0_PWM))
TIMERD0, // 12 PC0 WOC
TIMERD0, // 13 PC1 WOD
#else
NOT_ON_TIMER, // 12 PC0
NOT_ON_TIMER, // 13 PC1
#endif
NOT_ON_TIMER, // 14 PC2
NOT_ON_TIMER, // 15 PC3 WO3 Alt
NOT_ON_TIMER, // 16 PC4 WO4 Alt
NOT_ON_TIMER, // 17 PC5 WO5 Alt
NOT_ON_TIMER, // 18 PA1
NOT_ON_TIMER, // 19 PA2
TIMERA0, // 20 PA3 WO3
NOT_ON_TIMER // 21 PA0
};
*/

// OK, only PIN_PA4-5:TCA0
// PIN_PC0-1: TCD0 => not OK yet for frequency
// Not OK, PIN_PA6, 7, PIN_PB0-2:TCA0

#define pinToUse PIN_PA5 //PIN_PB4

//creates pwm instance
ATtiny_PWM* PWM_Instance;

float frequency;
float dutyCycle;

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

void printPWMInfo(ATtiny_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(AT_TINY_PWM_VERSION);

frequency = 5000.0f;

PWM_Instance = new ATtiny_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);
}

0 comments on commit 5e2c807

Please sign in to comment.