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

Commit 5e2c807

Browse files
authored
v1.0.0 for hardware-PWM on ATtiny boards
### Initial Releases v1.0.0 1. Initial coding to support Arduino **AVR ATtiny-based boards (ATtiny3217, etc.)** using [**megaTinyCore**](https://github.com/SpenceKonde/megaTinyCore)
1 parent 46bf5d6 commit 5e2c807

20 files changed

+2015
-0
lines changed

CONTRIBUTING.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## Contributing to ATtiny_PWM
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in ATtiny_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/ATtiny_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/ATtiny_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+
* `megaTinyCore` Core Version (e.g. megaTinyCore core v2.6.3)
19+
* Board (e.g. AVR_CuriosityNano3217, etc.)
20+
* Contextual information (e.g. what you were trying to achieve)
21+
* Simplest possible steps to reproduce
22+
* Anything that might be relevant in your opinion, such as:
23+
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
24+
* Network configuration
25+
26+
27+
### Example
28+
29+
```
30+
Arduino IDE version: 1.8.19
31+
Arduino megaTinyCore core v2.6.3
32+
OS: Ubuntu 20.04 LTS
33+
Board: AVR_CuriosityNano3217
34+
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
35+
36+
Context:
37+
I encountered a crash while using this library
38+
39+
Steps to reproduce:
40+
1. ...
41+
2. ...
42+
3. ...
43+
4. ...
44+
```
45+
46+
---
47+
48+
### Sending Feature Requests
49+
50+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
51+
52+
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.
53+
54+
---
55+
56+
### Sending Pull Requests
57+
58+
Pull Requests with changes and fixes are also welcome!
59+
60+
Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)
61+
62+
1. Change directory to the library GitHub
63+
64+
```
65+
xy@xy-Inspiron-3593:~$ cd Arduino/xy/ATtiny_PWM_GitHub/
66+
xy@xy-Inspiron-3593:~/Arduino/xy/ATtiny_PWM_GitHub$
67+
```
68+
69+
2. Issue astyle command
70+
71+
```
72+
xy@xy-Inspiron-3593:~/Arduino/xy/ATtiny_PWM_GitHub$ bash utils/restyle.sh
73+
```
74+

changelog.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ATtiny_PWM Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/ATtiny_PWM.svg?)](https://www.ardu-badge.com/ATtiny_PWM)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/ATtiny_PWM.svg)](https://github.com/khoih-prog/ATtiny_PWM/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/ATtiny_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/ATtiny_PWM.svg)](http://github.com/khoih-prog/ATtiny_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 Arduino **AVR ATtiny-based boards (ATtiny3217, etc.)** using [**megaTinyCore**](https://github.com/SpenceKonde/megaTinyCore)
25+
26+
27+
---
28+
---
29+
30+
## Copyright
31+
32+
Copyright 2022- Khoi Hoang
33+
34+

examples/PWM_Basic/PWM_Basic.ino

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/****************************************************************************************************************************
2+
PWM_Basic.ino
3+
4+
For Arduino AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/ATtiny_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
#define _PWM_LOGLEVEL_ 4
12+
13+
// Select false to use PWM
14+
#define USING_TIMER false //true
15+
16+
#include "ATtiny_PWM.h"
17+
18+
/*
19+
const uint8_t digital_pin_to_timer[] = {
20+
// Left side, top to bottom
21+
TIMERA0, // 0 PA4 WO4 WOA
22+
TIMERA0, // 1 PA5 WO5 WOB
23+
#if defined(DAC0)
24+
DACOUT, // 2 PA6
25+
#else
26+
NOT_ON_TIMER, // 2 PA6
27+
#endif
28+
NOT_ON_TIMER, // 3 PA7
29+
NOT_ON_TIMER, // 4 PB7
30+
NOT_ON_TIMER, // 5 PB6
31+
NOT_ON_TIMER, // 6 PB5 WO2 Alt
32+
NOT_ON_TIMER, // 7 PB4 WO1 Alt
33+
NOT_ON_TIMER, // 8 PB3 WO0 Alt
34+
TIMERA0, // 9 PB2 WO2
35+
TIMERA0, // 10 PB1 WO1
36+
// Right side, bottom to top
37+
TIMERA0, // 11 PB0 WO0
38+
#if (defined(TCD0) && defined(USE_TIMERD0_PWM))
39+
TIMERD0, // 12 PC0 WOC
40+
TIMERD0, // 13 PC1 WOD
41+
#else
42+
NOT_ON_TIMER, // 12 PC0
43+
NOT_ON_TIMER, // 13 PC1
44+
#endif
45+
NOT_ON_TIMER, // 14 PC2
46+
NOT_ON_TIMER, // 15 PC3 WO3 Alt
47+
NOT_ON_TIMER, // 16 PC4 WO4 Alt
48+
NOT_ON_TIMER, // 17 PC5 WO5 Alt
49+
NOT_ON_TIMER, // 18 PA1
50+
NOT_ON_TIMER, // 19 PA2
51+
TIMERA0, // 20 PA3 WO3
52+
NOT_ON_TIMER // 21 PA0
53+
};
54+
*/
55+
56+
// OK, only PIN_PA4-5:TCA0
57+
// PIN_PC0-1: TCD0 => not OK yet for frequency
58+
// Not OK, PIN_PA6, 7, PIN_PB0-2:TCA0
59+
60+
#define pinToUse PIN_PA5 //PIN_PB4
61+
62+
//creates pwm instance
63+
ATtiny_PWM* PWM_Instance;
64+
65+
float frequency = 1000.0f;
66+
67+
float dutyCycle = 0.0f;
68+
69+
void setup()
70+
{
71+
Serial.begin(115200);
72+
73+
while (!Serial && millis() < 5000);
74+
75+
delay(500);
76+
77+
Serial.print(F("\nStarting PWM_Basic using PWM on "));
78+
Serial.println(BOARD_NAME);
79+
Serial.println(AT_TINY_PWM_VERSION);
80+
81+
//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%
82+
PWM_Instance = new ATtiny_PWM(pinToUse, frequency, dutyCycle);
83+
84+
if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
85+
{
86+
Serial.print(F("Stop here forever"));
87+
88+
while (true)
89+
delay(10000);
90+
}
91+
}
92+
93+
void loop()
94+
{
95+
// You can change frequency here, anytime
96+
frequency = 2000.0f;
97+
//frequency = 20.0f;
98+
dutyCycle = 20.0f;
99+
100+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
101+
analogWrite(PIN_PA3, dutyCycle * (1 << 8) / 100);
102+
103+
delay(10000);
104+
105+
// You can change frequency here, anytime
106+
frequency = 5000.0f;
107+
//frequency = 50.0f;
108+
dutyCycle = 90.0f;
109+
110+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
111+
analogWrite(PIN_PA3, dutyCycle * (1 << 8) / 100);
112+
113+
//while (1)
114+
delay(10000);
115+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/****************************************************************************************************************************
2+
PWM_DynamicDutyCycle.ino
3+
4+
For Arduino AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/ATtiny_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
#define _PWM_LOGLEVEL_ 4
12+
13+
// Select false to use PWM
14+
#define USING_TIMER false //true
15+
16+
#include "ATtiny_PWM.h"
17+
18+
/*
19+
const uint8_t digital_pin_to_timer[] = {
20+
// Left side, top to bottom
21+
TIMERA0, // 0 PA4 WO4 WOA
22+
TIMERA0, // 1 PA5 WO5 WOB
23+
#if defined(DAC0)
24+
DACOUT, // 2 PA6
25+
#else
26+
NOT_ON_TIMER, // 2 PA6
27+
#endif
28+
NOT_ON_TIMER, // 3 PA7
29+
NOT_ON_TIMER, // 4 PB7
30+
NOT_ON_TIMER, // 5 PB6
31+
NOT_ON_TIMER, // 6 PB5 WO2 Alt
32+
NOT_ON_TIMER, // 7 PB4 WO1 Alt
33+
NOT_ON_TIMER, // 8 PB3 WO0 Alt
34+
TIMERA0, // 9 PB2 WO2
35+
TIMERA0, // 10 PB1 WO1
36+
// Right side, bottom to top
37+
TIMERA0, // 11 PB0 WO0
38+
#if (defined(TCD0) && defined(USE_TIMERD0_PWM))
39+
TIMERD0, // 12 PC0 WOC
40+
TIMERD0, // 13 PC1 WOD
41+
#else
42+
NOT_ON_TIMER, // 12 PC0
43+
NOT_ON_TIMER, // 13 PC1
44+
#endif
45+
NOT_ON_TIMER, // 14 PC2
46+
NOT_ON_TIMER, // 15 PC3 WO3 Alt
47+
NOT_ON_TIMER, // 16 PC4 WO4 Alt
48+
NOT_ON_TIMER, // 17 PC5 WO5 Alt
49+
NOT_ON_TIMER, // 18 PA1
50+
NOT_ON_TIMER, // 19 PA2
51+
TIMERA0, // 20 PA3 WO3
52+
NOT_ON_TIMER // 21 PA0
53+
};
54+
*/
55+
56+
// OK, only PIN_PA4-5:TCA0
57+
// PIN_PC0-1: TCD0 => not OK yet for frequency
58+
// Not OK, PIN_PA6, 7, PIN_PB0-2:TCA0
59+
60+
#define pinToUse PIN_PA5 //PIN_PB4
61+
62+
//creates pwm instance
63+
ATtiny_PWM* PWM_Instance;
64+
65+
float frequency;
66+
float dutyCycle;
67+
68+
char dashLine[] = "=====================================================================================";
69+
70+
void printPWMInfo(ATtiny_PWM* PWM_Instance)
71+
{
72+
Serial.println(dashLine);
73+
Serial.print("Actual data: pin = ");
74+
Serial.print(PWM_Instance->getPin());
75+
Serial.print(", PWM DC = ");
76+
Serial.print(PWM_Instance->getActualDutyCycle());
77+
Serial.print(", PWMPeriod = ");
78+
Serial.print(PWM_Instance->getPWMPeriod());
79+
Serial.print(", PWM Freq (Hz) = ");
80+
Serial.println(PWM_Instance->getActualFreq(), 4);
81+
Serial.println(dashLine);
82+
}
83+
84+
void setup()
85+
{
86+
Serial.begin(115200);
87+
88+
while (!Serial && millis() < 5000);
89+
90+
delay(500);
91+
92+
Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
93+
Serial.println(BOARD_NAME);
94+
Serial.println(AT_TINY_PWM_VERSION);
95+
96+
frequency = 5000.0f;
97+
98+
PWM_Instance = new ATtiny_PWM(pinToUse, frequency, 0.0f);
99+
100+
if ( (!PWM_Instance) || !PWM_Instance->isPWMEnabled())
101+
{
102+
Serial.print(F("Stop here forever"));
103+
104+
while (true)
105+
delay(10000);
106+
}
107+
108+
Serial.println(dashLine);
109+
}
110+
111+
void loop()
112+
{
113+
dutyCycle = 90.0f;
114+
115+
Serial.print(F("Change PWM DutyCycle to "));
116+
Serial.println(dutyCycle);
117+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
118+
119+
printPWMInfo(PWM_Instance);
120+
121+
delay(5000);
122+
dutyCycle = 20.0f;
123+
124+
Serial.print(F("Change PWM DutyCycle to "));
125+
Serial.println(dutyCycle);
126+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
127+
printPWMInfo(PWM_Instance);
128+
129+
delay(5000);
130+
}

0 commit comments

Comments
 (0)