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 2
/
Copy pathISR_Modify_PWM.ino
193 lines (143 loc) · 5.69 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
/****************************************************************************************************************************
ISR_Modify_PWM.ino
For RP2040-based boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RP2040_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 RP2040-based 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_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED) )
#error This code is intended to run on the non-mbed RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP32_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_ 3
#define USING_MICROS_RESOLUTION true //false
// Default is true, uncomment to false
//#define CHANGING_PWM_END_OF_CYCLE false
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "RP2040_Slow_PWM.h"
#define LED_OFF HIGH
#define LED_ON LOW
#ifndef LED_BUILTIN
#define LED_BUILTIN 25
#endif
#ifndef LED_BLUE
#define LED_BLUE 10
#endif
#ifndef LED_RED
#define LED_RED 11
#endif
#define HW_TIMER_INTERVAL_US 20L
volatile uint32_t startMicros = 0;
// Init RPI_PICO_Timer
RP2040_Timer ITimer(0);
// Init RP2040_Slow_PWM, each can service 16 different ISR-based PWM channels
RP2040_Slow_PWM ISR_PWM;
//////////////////////////////////////////////////////
bool TimerHandler(struct repeating_timer *t)
{
(void) t;
ISR_PWM.run();
return true;
}
//////////////////////////////////////////////////////
#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 = 200.0f; //1.0f;
// You can assign any interval for any timer here, in Hz
float PWM_Freq2 = 100.0f; //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 = 1.0f; //50.0f;
// You can assign any duty_cycle for any PWM here, from 0-100
float PWM_DutyCycle2 = 5.55f; //90.0f;
// 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(RP2040_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"));
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
// You can use up to 16 timer for each ISR_PWM
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
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;
float PWM_Freq;
float 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.print(F("modifyPWMChannel error for PWM_Period"));
}
}
////////////////////////////////////////////////
void changingPWM()
{
static ulong changingPWM_timeout = 0;
static ulong 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();
}