This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPWM_Basic.ino
98 lines (76 loc) · 3.25 KB
/
PWM_Basic.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/****************************************************************************************************************************
PWM_Basic.ino
For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. )
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/megaAVR_PWM
Licensed under MIT license
This is pure hardware-based PWM
*****************************************************************************************************************************/
/******************************************************************************************************************************
Pins can be used for hardware-PWM
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.)
TCA0 (16-bit) used by PWM generation on pins 5, 9 and 10
TCB0 (16-bit) used by PWM generation on pin 6
TCB1 (16-bit) used by PWM generation on pin 3
TCB2 (16-bit)
TCB3 (16-bit)
////////////////////////////////////////////
// For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.)
Pin 3 => TIMERB1, // 3 PF5, 8-bit PWM, 16-bit counter
Pin 5 => TIMERA0, // 5 PB2, 16-bit PWM, 16-bit counter
Pin 6 => TIMERB0, // 6 PF4, 8-bit PWM, 16-bit counter
Pin 9 => TIMERA0, // 9 PB0, 16-bit PWM, 16-bit counter
Pin 10 => TIMERA0, // 10 PB1, 16-bit PWM, 16-bit counter
////////////////////////////////////////////
******************************************************************************************************************************/
#define _PWM_LOGLEVEL_ 4
#include "megaAVR_PWM.h"
#define USING_TIMERB true
#if USING_TIMERB
// Pins tested OK in Nano Every ATmega4809
#define pinToUse 3 // TimerB1, for higher frequencies, up to 100KHz
//#define pinToUse 6 // TimerB0, for higher frequencies, up to 100KHz
#elif USING_ARDUINO_MEGA_AVR_CORE
// Pins tested OK in Nano Every ATmega4809 using Arduino megaAVR core
// TimerA0 somehow can't be used with MegaCoreX
#define pinToUse 5 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
//#define pinToUse 9 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
//#define pinToUse 10 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock
#else
#error TimerA0 to be used with Arduino megaAVR Core
#endif
////////////////////////////////////////////
//creates pwm instance
megaAVR_PWM* PWM_Instance;
float frequency;
float dutyCycle;
////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
//delay(1000);
Serial.print(F("\nStarting PWM_Basic on "));
Serial.println(BOARD_NAME);
Serial.println(MEGA_AVR_PWM_VERSION);
//assigns PWM frequency of 1KHz and a duty cycle of 0%
PWM_Instance = new megaAVR_PWM(pinToUse, 1000, 0);
}
void loop()
{
#if USING_TIMERB
// Using higher Hz for TimerB, min is 980.34 Hz
frequency = 40000;
//frequency = 1000;
#else
// Using much lower Hz for TimerA
frequency = 1000;
#endif
dutyCycle = 30;
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
delay(5000);
dutyCycle = 90;
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
//while (true)
delay(5000);
}