This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ISR_Modify_PWM.ino
195 lines (144 loc) · 5.63 KB
/
ISR_Modify_PWM.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
/****************************************************************************************************************************
ISR_Modify_PWM.ino
For SAMD21/SAMD51 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_Slow_PWM
Licensed under MIT license
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one SAMD timer and avoid conflicting with other cores' tasks.
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
Therefore, their executions are not blocked by bad-behaving functions / tasks.
This important feature is absolutely necessary for mission-critical tasks.
*****************************************************************************************************************************/
#if !( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \
|| defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) \
|| defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \
|| defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21G18A__) )
#error This code is designed to run on SAMD21 platform! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "SAMD_Slow_PWM.h"
// _PWM_LOGLEVEL_ from 0 to 4
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define _PWM_LOGLEVEL_ 4
#define USING_MICROS_RESOLUTION true //false
// Default is true, uncomment to false
//#define CHANGING_PWM_END_OF_CYCLE false
#define MAX_SAMD_PWM_FREQ 1000
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "SAMD_Slow_PWM.h"
#define LED_OFF LOW
#define LED_ON HIGH
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
#ifndef LED_BLUE
#define LED_BLUE 2
#endif
#ifndef LED_RED
#define LED_RED 3
#endif
// Use 50uS for slow SAMD21
#define HW_TIMER_INTERVAL_US 50L
uint64_t startMicros = 0;
// Init SAMD timer TIMER_TC3
SAMDTimer ITimer(TIMER_TC3);
// Init SAMD timer TIMER_TCC
//SAMDTimer ITimer(TIMER_TCC);
// Init SAMD_Slow_PWM
SAMD_Slow_PWM ISR_PWM;
//////////////////////////////////////////////////////
void TimerHandler()
{
ISR_PWM.run();
}
//////////////////////////////////////////////////////
#define USING_PWM_FREQUENCY false //true
//////////////////////////////////////////////////////
// You can assign pins here. Be carefull to select good pin to use or crash
uint32_t PWM_Pin = LED_BUILTIN;
// You can assign any interval for any timer here, in Hz
float PWM_Freq1 = 1.0f;
// You can assign any interval for any timer here, in Hz
float PWM_Freq2 = 2.0f;
// You can assign any interval for any timer here, in microseconds
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
// You can assign any interval for any timer here, in microseconds
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
// You can assign any duty_cycle for any PWM here, from 0-100
float PWM_DutyCycle1 = 50.0;
// You can assign any duty_cycle for any PWM here, from 0-100
float PWM_DutyCycle2 = 90.0;
// Channel number used to identify associated channel
int channelNum;
////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(2000);
Serial.print(F("\nStarting ISR_Modify_PWM on ")); Serial.println(BOARD_NAME);
Serial.println(SAMD_SLOW_PWM_VERSION);
// Interval in microsecs
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler))
{
startMicros = micros();
Serial.print(F("Starting ITimer OK, micros() = ")); Serial.println(startMicros);
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
Serial.print(F("Using PWM Freq = ")); Serial.print(PWM_Freq1); Serial.print(F(", PWM DutyCycle = ")); Serial.println(PWM_DutyCycle1);
#if USING_PWM_FREQUENCY
// You can use this with PWM_Freq in Hz
ISR_PWM.setPWM(PWM_Pin, PWM_Freq1, PWM_DutyCycle1);
#else
#if USING_MICROS_RESOLUTION
// Or using period in microsecs resolution
channelNum = ISR_PWM.setPWM_Period(PWM_Pin, PWM_Period1, PWM_DutyCycle1);
#else
// Or using period in millisecs resolution
channelNum = ISR_PWM.setPWM_Period(PWM_Pin, PWM_Period1 / 1000, PWM_DutyCycle1);
#endif
#endif
}
////////////////////////////////////////////////
void changePWM()
{
static uint8_t count = 1;
double PWM_Freq;
uint32_t PWM_DutyCycle;
if (count++ % 2)
{
PWM_Freq = PWM_Freq2;
PWM_DutyCycle = PWM_DutyCycle2;
}
else
{
PWM_Freq = PWM_Freq1;
PWM_DutyCycle = PWM_DutyCycle1;
}
// You can use this with PWM_Freq in Hz
if (!ISR_PWM.modifyPWMChannel(channelNum, PWM_Pin, PWM_Freq, PWM_DutyCycle))
{
Serial.println(F("modifyPWMChannel error for PWM_Period"));
}
}
////////////////////////////////////////////////
void changingPWM()
{
static unsigned long changingPWM_timeout = 0;
static unsigned long current_millis;
#define CHANGING_PWM_INTERVAL 10000L
current_millis = millis();
// changePWM every CHANGING_PWM_INTERVAL (10) seconds.
if ( (current_millis > changingPWM_timeout) )
{
if (changingPWM_timeout > 0)
changePWM();
changingPWM_timeout = current_millis + CHANGING_PWM_INTERVAL;
}
}
////////////////////////////////////////////////
void loop()
{
changingPWM();
}