2
2
FakeAnalogWrite.ino
3
3
For Arduino AVR boards (UNO, Nano, Mega, etc. )
4
4
Written by Khoi Hoang
5
-
5
+
6
6
TCNTx - Timer/Counter Register. The actual timer value is stored here.
7
7
OCRx - Output Compare Register
8
8
ICRx - Input Capture Register (only for 16bit timer)
9
9
TIMSKx - Timer/Counter Interrupt Mask Register. To enable/disable timer interrupts.
10
- TIFRx - Timer/Counter Interrupt Flag Register. Indicates a pending timer interrupt.
10
+ TIFRx - Timer/Counter Interrupt Flag Register. Indicates a pending timer interrupt.
11
11
12
12
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
13
13
unsigned long miliseconds), you just consume only one Hardware timer and avoid conflicting with other cores' tasks.
21
21
22
22
Based on BlynkTimer.h
23
23
Author: Volodymyr Shymanskyy
24
-
24
+
25
25
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic
26
26
Licensed under MIT license
27
27
*****************************************************************************************************************************/
50
50
defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || \
51
51
defined(ARDUINO_AVR_MINI) || defined(ARDUINO_AVR_ETHERNET) || defined(ARDUINO_AVR_FIO) || defined(ARDUINO_AVR_BT) || \
52
52
defined(ARDUINO_AVR_LILYPAD) || defined(ARDUINO_AVR_PRO) || defined(ARDUINO_AVR_NG) || defined(ARDUINO_AVR_UNO_WIFI_DEV_ED))
53
- #error This is designed only for Arduino AVR board! Please check your Tools->Board setting.
53
+ #error This is designed only for Arduino AVR board! Please check your Tools->Board setting.
54
54
#endif
55
-
55
+
56
56
#define TIMER_INTERRUPT_DEBUG 0
57
57
58
58
#define USE_TIMER_1 false
66
66
#include " TimerInterrupt_Generic.h"
67
67
68
68
#ifndef LED_BUILTIN
69
- #define LED_BUILTIN 13
69
+ #define LED_BUILTIN 13
70
70
#endif
71
71
72
72
// For PWM_Value from 0-255.You can change to 1024 or 2048
@@ -107,7 +107,7 @@ void TimerHandler(void)
107
107
108
108
// Toggle LED every LED_TOGGLE_INTERVAL_MS = 500ms = 0.5s
109
109
if (++timeRun == ((LED_TOGGLE_INTERVAL_MS * TIMER2_FREQUENCY_HZ) / 1000 ) )
110
- {
110
+ {
111
111
timeRun = 0 ;
112
112
113
113
// timer interrupt toggles pin LED_BUILTIN
@@ -207,7 +207,7 @@ void setup()
207
207
if (ITimer2.attachInterrupt (TIMER2_FREQUENCY_HZ, TimerHandler))
208
208
Serial.println (" Starting ITimer2 OK, millis() = " + String (millis ()));
209
209
else
210
- Serial.println (" Can't set ITimer2. Select another freq., duration or timer" );
210
+ Serial.println (" Can't set ITimer2. Select another freq., duration or timer" );
211
211
212
212
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
213
213
// You can use up to 16 timer for each ISR_Timer
@@ -221,6 +221,8 @@ void setup()
221
221
}
222
222
}
223
223
224
+ #define USING_MAPPING_TABLE false
225
+
224
226
void fakeAnalogWrite (uint16_t pin, uint16_t value)
225
227
{
226
228
uint16_t localValue;
@@ -232,20 +234,20 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
232
234
if ( (curISRTimerData[i].beingUsed ) && (curISRTimerData[i].pin == pin) )
233
235
{
234
236
localValue = (value < MAX_PWM_VALUE) ? value : MAX_PWM_VALUE;
235
-
237
+
236
238
if (curISRTimerData[i].PWM_PremapValue == localValue)
237
239
{
238
- #if (LOCAL_DEBUG > 0)
240
+ #if (LOCAL_DEBUG > 0)
239
241
Serial.print (" Ignore : Same Value for index = " );
240
242
Serial.println (i);
241
243
#endif
242
-
244
+
243
245
return ;
244
246
}
245
247
else if (curISRTimerData[i].PWM_Value >= 0 )
246
- {
248
+ {
247
249
curISRTimerData[i].PWM_PremapValue = localValue;
248
-
250
+
249
251
// Mapping to corect value
250
252
if ( ( localValue == 0 ) || ( localValue == MAX_PWM_VALUE - 1 ) )
251
253
{
@@ -254,6 +256,9 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
254
256
}
255
257
else
256
258
{
259
+
260
+ #if USING_MAPPING_TABLE
261
+
257
262
// Get the mapping index
258
263
for (int j = 0 ; j < MAPPING_TABLE_SIZE; j++)
259
264
{
@@ -272,18 +277,22 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
272
277
// Can use map() function
273
278
// Can use map() function
274
279
curISRTimerData[i].PWM_Value = (uint16_t ) ( (localIndex * 10 ) +
275
- ( (value - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1 ] - mappingTable[localIndex]) );
280
+ ( (localValue - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1 ] - mappingTable[localIndex]) );
281
+
282
+ #else
283
+ curISRTimerData[i].PWM_Value = localValue;
284
+ #endif
276
285
277
286
#if (LOCAL_DEBUG > 0)
278
- Serial.print (" Update index = " );
279
- Serial.print (i);
280
- Serial.print (" , pin = " );
281
- Serial.print (pin);
282
- Serial.print (" , input PWM_Value = " );
283
- Serial.print (value);
284
- Serial.print (" , mapped PWM_Value = " );
285
- Serial.println (curISRTimerData[i].PWM_Value );
286
- #endif
287
+ Serial.print (" Update index = " );
288
+ Serial.print (i);
289
+ Serial.print (" , pin = " );
290
+ Serial.print (pin);
291
+ Serial.print (" , input PWM_Value = " );
292
+ Serial.print (value);
293
+ Serial.print (" , mapped PWM_Value = " );
294
+ Serial.println (curISRTimerData[i].PWM_Value );
295
+ #endif
287
296
}
288
297
}
289
298
else
@@ -317,6 +326,10 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
317
326
}
318
327
else
319
328
{
329
+ curISRTimerData[i].PWM_PremapValue = localValue;
330
+
331
+ #if USING_MAPPING_TABLE
332
+
320
333
// Get the mapping index
321
334
for (int j = 0 ; j < MAPPING_TABLE_SIZE; j++)
322
335
{
@@ -334,7 +347,10 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
334
347
// Can use map() function
335
348
// Can use map() function
336
349
curISRTimerData[i].PWM_Value = (uint16_t ) ( (localIndex * 10 ) +
337
- ( (value - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1 ] - mappingTable[localIndex]) );
350
+ ( (localValue - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1 ] - mappingTable[localIndex]) );
351
+ #else
352
+ curISRTimerData[i].PWM_Value = localValue;
353
+ #endif
338
354
}
339
355
340
356
curISRTimerData[i].countPWM = 0 ;
@@ -412,7 +428,7 @@ void loop()
412
428
Serial.print (" , max = " );
413
429
Serial.println (MAX_PWM_VALUE - 1 );
414
430
#endif
415
-
431
+
416
432
delay (DELAY_BETWEEN_CHANGE_MS);
417
433
}
418
434
0 commit comments