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
/
Copy pathPWM_Waveform.ino
202 lines (166 loc) · 4.08 KB
/
PWM_Waveform.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/****************************************************************************************************************************
PWM_Waveform.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;
typedef struct
{
uint16_t level;
} PWD_Data;
// Data for 0-100%
PWD_Data PWM_data[] =
{
{ 0 },
{ 5 },
{ 10 },
{ 15 },
{ 20 },
{ 25 },
{ 30 },
{ 35 },
{ 40 },
{ 45 },
{ 50 },
{ 55 },
{ 60 },
{ 65 },
{ 70 },
{ 75 },
{ 80 },
{ 85 },
{ 90 },
{ 95 },
{ 100 },
{ 95 },
{ 90 },
{ 85 },
{ 80 },
{ 75 },
{ 70 },
{ 65 },
{ 60 },
{ 55 },
{ 50 },
{ 45 },
{ 40 },
{ 35 },
{ 30 },
{ 25 },
{ 20 },
{ 15 },
{ 10 },
{ 5 },
{ 0 },
};
#define NUM_PWM_POINTS ( sizeof(PWM_data) / sizeof(PWD_Data) )
float frequency = 2000.0f;
float dutyCycle = 0.0f;
uint8_t channel = 0;
// You can select any value
PWD_Data PWM_data_idle = PWM_data[0];
char dashLine[] = "============================================================================================";
void printPWMInfo(Dx_PWM* PWM_Instance)
{
SerialDebug.println(dashLine);
SerialDebug.print("Actual data: pin = ");
SerialDebug.print(PWM_Instance->getPin());
SerialDebug.print(", PWM DutyCycle = ");
SerialDebug.print(PWM_Instance->getActualDutyCycle());
SerialDebug.print(", PWMPeriod = ");
SerialDebug.print(PWM_Instance->getPWMPeriod());
SerialDebug.print(", PWM Freq (Hz) = ");
SerialDebug.println(PWM_Instance->getActualFreq(), 4);
SerialDebug.println(dashLine);
}
void setup()
{
SerialDebug.begin(115200);
while (!Serial && millis() < 5000);
delay(500);
SerialDebug.print(F("\nStarting PWM_Waveform on "));
SerialDebug.println(BOARD_NAME);
SerialDebug.println(DX_PWM_VERSION);
// Create a dummy instance
PWM_Instance = new Dx_PWM(pinToUse, frequency, dutyCycle);
if (PWM_Instance)
{
// setPWM_manual(uint8_t pin, uint16_t level)
//PWM_Instance->setPWM_manual(pinToUse, 0);
PWM_Instance->setPWM(pinToUse, frequency, 0);
printPWMInfo(PWM_Instance);
}
else
{
SerialDebug.print(F("Stop here forever"));
while (true)
delay(10000);
}
}
void updateDC()
{
static uint16_t index = 0;
// Mapping data to any other frequency from original data 0-100 to actual 16-bit Dutycycle
PWM_Instance->setPWM_manual(pinToUse, ( (uint32_t) PWM_data[index].level * MAX_16BIT ) / 100 );
// Use at low freq to check
//printPWMInfo(PWM_Instance);
index = (index + 1) % NUM_PWM_POINTS;
}
void check_status()
{
#define UPDATE_INTERVAL 100L
static unsigned long update_timeout = UPDATE_INTERVAL;
// Update DC every UPDATE_INTERVAL (100) milliseconds
if (millis() > update_timeout)
{
updateDC();
update_timeout = millis() + UPDATE_INTERVAL;
}
}
void loop()
{
check_status();
}