This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PWM_Basic.ino
109 lines (82 loc) · 2.38 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
99
100
101
102
103
104
105
106
107
108
109
/****************************************************************************************************************************
PWM_Basic.ino
For Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/Dx_PWM
Licensed under MIT license
*****************************************************************************************************************************/
#define _PWM_LOGLEVEL_ 4
#if defined(__AVR_AVR128DA48__)
#define SerialDebug Serial1
#elif defined(__AVR_AVR128DB48__)
#define SerialDebug Serial3
#else
// standard Serial
#define SerialDebug Serial
#endif
#define PWM_GENERIC_DEBUG_PORT SerialDebug
#include "Dx_PWM.h"
#ifdef LED_BUILTIN
#undef LED_BUILTIN
// To modify according to your board
// For Curiosity Nano AVR128DA48 => PIN_PC6
// For Curiosity Nano AVR128DB48 => PIN_PB3
#if defined(__AVR_AVR128DA48__)
#define LED_BUILTIN PIN_PC6 // PIN_PB3, 13
#elif defined(__AVR_AVR128DB48__)
#define LED_BUILTIN PIN_PB3 // PIN_PC6, 13
#else
// standard Arduino pin 13
#define LED_BUILTIN 13
#endif
#endif
// On DX AVR128DB48
// PA0-3: Not PWM
// PA4-7: TD0 => not supported yet
// PB0-5: TCA1
// PC0-5: TCA0
// PC6-7: Not PWM
// PD0-7: Not PWM
// PE0-3: Not PWM
// PF0-3: Not PWM
// PF4-5: TCB
#if defined(PIN_PF5)
#define pinToUse PIN_PF5
#else
#define pinToUse PIN_PC0
#endif
//creates pwm instance
Dx_PWM* PWM_Instance;
float frequency = 1000.0f;
float dutyCycle = 0.0f;
void setup()
{
SerialDebug.begin(115200);
while (!Serial && millis() < 5000);
delay(500);
SerialDebug.print(F("\nStarting PWM_Basic on "));
SerialDebug.println(BOARD_NAME);
SerialDebug.println(DX_PWM_VERSION);
//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%
PWM_Instance = new Dx_PWM(pinToUse, frequency, dutyCycle);
if ( (!PWM_Instance) /*|| !PWM_Instance->isPWMEnabled()*/)
{
SerialDebug.print(F("Stop here forever"));
while (true)
delay(10000);
}
}
void loop()
{
// You can change frequency here, anytime
frequency = 2000.0f;
dutyCycle = 20.0f;
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
delay(5000);
// You can change frequency here, anytime
frequency = 4000.0f;
dutyCycle = 80.0f;
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
//while (1)
delay(5000);
}