Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit c9838ac

Browse files
authored
Add FakeAnalogWrite example
Add FakeAnalogWrite example
1 parent 56a27f4 commit c9838ac

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

examples/AVR/FakeAnalogWrite/FakeAnalogWrite.ino

+41-25
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
FakeAnalogWrite.ino
33
For Arduino AVR boards (UNO, Nano, Mega, etc. )
44
Written by Khoi Hoang
5-
5+
66
TCNTx - Timer/Counter Register. The actual timer value is stored here.
77
OCRx - Output Compare Register
88
ICRx - Input Capture Register (only for 16bit timer)
99
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.
1111
1212
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
1313
unsigned long miliseconds), you just consume only one Hardware timer and avoid conflicting with other cores' tasks.
@@ -21,7 +21,7 @@
2121
2222
Based on BlynkTimer.h
2323
Author: Volodymyr Shymanskyy
24-
24+
2525
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic
2626
Licensed under MIT license
2727
*****************************************************************************************************************************/
@@ -50,9 +50,9 @@
5050
defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || \
5151
defined(ARDUINO_AVR_MINI) || defined(ARDUINO_AVR_ETHERNET) || defined(ARDUINO_AVR_FIO) || defined(ARDUINO_AVR_BT) || \
5252
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.
5454
#endif
55-
55+
5656
#define TIMER_INTERRUPT_DEBUG 0
5757

5858
#define USE_TIMER_1 false
@@ -66,7 +66,7 @@
6666
#include "TimerInterrupt_Generic.h"
6767

6868
#ifndef LED_BUILTIN
69-
#define LED_BUILTIN 13
69+
#define LED_BUILTIN 13
7070
#endif
7171

7272
// For PWM_Value from 0-255.You can change to 1024 or 2048
@@ -107,7 +107,7 @@ void TimerHandler(void)
107107

108108
// Toggle LED every LED_TOGGLE_INTERVAL_MS = 500ms = 0.5s
109109
if (++timeRun == ((LED_TOGGLE_INTERVAL_MS * TIMER2_FREQUENCY_HZ) / 1000) )
110-
{
110+
{
111111
timeRun = 0;
112112

113113
//timer interrupt toggles pin LED_BUILTIN
@@ -207,7 +207,7 @@ void setup()
207207
if (ITimer2.attachInterrupt(TIMER2_FREQUENCY_HZ, TimerHandler))
208208
Serial.println("Starting ITimer2 OK, millis() = " + String(millis()));
209209
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");
211211

212212
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
213213
// You can use up to 16 timer for each ISR_Timer
@@ -221,6 +221,8 @@ void setup()
221221
}
222222
}
223223

224+
#define USING_MAPPING_TABLE false
225+
224226
void fakeAnalogWrite(uint16_t pin, uint16_t value)
225227
{
226228
uint16_t localValue;
@@ -232,20 +234,20 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
232234
if ( (curISRTimerData[i].beingUsed) && (curISRTimerData[i].pin == pin) )
233235
{
234236
localValue = (value < MAX_PWM_VALUE) ? value : MAX_PWM_VALUE;
235-
237+
236238
if (curISRTimerData[i].PWM_PremapValue == localValue)
237239
{
238-
#if (LOCAL_DEBUG > 0)
240+
#if (LOCAL_DEBUG > 0)
239241
Serial.print("Ignore : Same Value for index = ");
240242
Serial.println(i);
241243
#endif
242-
244+
243245
return;
244246
}
245247
else if (curISRTimerData[i].PWM_Value >= 0)
246-
{
248+
{
247249
curISRTimerData[i].PWM_PremapValue = localValue;
248-
250+
249251
// Mapping to corect value
250252
if ( ( localValue == 0) || ( localValue == MAX_PWM_VALUE - 1) )
251253
{
@@ -254,6 +256,9 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
254256
}
255257
else
256258
{
259+
260+
#if USING_MAPPING_TABLE
261+
257262
// Get the mapping index
258263
for (int j = 0; j < MAPPING_TABLE_SIZE; j++)
259264
{
@@ -272,18 +277,22 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
272277
// Can use map() function
273278
// Can use map() function
274279
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
276285

277286
#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
287296
}
288297
}
289298
else
@@ -317,6 +326,10 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
317326
}
318327
else
319328
{
329+
curISRTimerData[i].PWM_PremapValue = localValue;
330+
331+
#if USING_MAPPING_TABLE
332+
320333
// Get the mapping index
321334
for (int j = 0; j < MAPPING_TABLE_SIZE; j++)
322335
{
@@ -334,7 +347,10 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
334347
// Can use map() function
335348
// Can use map() function
336349
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
338354
}
339355

340356
curISRTimerData[i].countPWM = 0;
@@ -412,7 +428,7 @@ void loop()
412428
Serial.print(", max = ");
413429
Serial.println(MAX_PWM_VALUE - 1);
414430
#endif
415-
431+
416432
delay(DELAY_BETWEEN_CHANGE_MS);
417433
}
418434

0 commit comments

Comments
 (0)