@@ -44,6 +44,10 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
4444---
4545---
4646
47+ ### Releases v1.0.2
48+
49+ 1 . Add example [ ** ISR_16_Timers_Array_Complex** ] ( examples/ISR_16_Timers_Array_Complex ) and optimize example [ ** ISR_16_Timers_Array** ] ( examples/ISR_16_Timers_Array )
50+
4751### Releases v1.0.1
4852
49531 . Initial coding for Nano-33-BLE and sync with [ ** NRF52_TimerInterrupt Library** ] ( https://github.com/khoih-prog/NRF52_TimerInterrupt )
@@ -314,15 +318,16 @@ void setup()
314318
315319 1 . [ Argument_None] ( examples/Argument_None )
316320 2 . [ ISR_16_Timers_Array] ( examples/ISR_16_Timers_Array )
317- 3 . [ SwitchDebounce] ( examples/SwitchDebounce )
318- 4 . [ TimerInterruptTest] ( examples/TimerInterruptTest )
319- 5 . [ TimerInterruptLEDDemo] ( examples/TimerInterruptLEDDemo )
321+ 3 . [ ISR_16_Timers_Array_Complex] ( examples/ISR_16_Timers_Array_Complex )
322+ 4 . [ SwitchDebounce] ( examples/SwitchDebounce )
323+ 5 . [ TimerInterruptTest] ( examples/TimerInterruptTest )
324+ 6 . [ TimerInterruptLEDDemo] ( examples/TimerInterruptLEDDemo )
320325
321326
322327---
323328---
324329
325- ### Example [ ISR_16_Timers_Array ] ( examples/ISR_16_Timers_Array )
330+ ### Example [ ISR_16_Timers_Array_Complex ] ( examples/ISR_16_Timers_Array_Complex )
326331
327332```
328333#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE )
@@ -385,161 +390,181 @@ void TimerHandler(void)
385390 }
386391}
387392
393+ /////////////////////////////////////////////////
394+
388395#define NUMBER_ISR_TIMERS 16
389396
390- // You can assign any interval for any timer here, in milliseconds
391- uint32_t TimerInterval[NUMBER_ISR_TIMERS] =
392- {
393- 5000L, 10000L, 15000L, 20000L, 25000L, 30000L, 35000L, 40000L,
394- 45000L, 50000L, 55000L, 60000L, 65000L, 70000L, 75000L, 80000L
395- };
397+ typedef void (*irqCallback) (void);
396398
397- volatile unsigned long deltaMillis [NUMBER_ISR_TIMERS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
398- volatile unsigned long previousMillis [NUMBER_ISR_TIMERS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
399+ /////////////////////////////////////////////////
399400
400- typedef void (*irqCallback) (void);
401+ #define USE_COMPLEX_STRUCT false
402+
403+ #if USE_COMPLEX_STRUCT
404+
405+ typedef struct
406+ {
407+ irqCallback irqCallbackFunc;
408+ uint32_t TimerInterval;
409+ unsigned long deltaMillis;
410+ unsigned long previousMillis;
411+ } ISRTimerData;
412+
413+ // In NRF52, avoid doing something fancy in ISR, for example Serial.print()
414+ // The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
415+ // Or you can get this run-time error / crash
416+
417+ void doingSomething(int index);
418+
419+ #else
420+
421+ volatile unsigned long deltaMillis [NUMBER_ISR_TIMERS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
422+ volatile unsigned long previousMillis [NUMBER_ISR_TIMERS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
423+
424+ // You can assign any interval for any timer here, in milliseconds
425+ uint32_t TimerInterval[NUMBER_ISR_TIMERS] =
426+ {
427+ 5000L, 10000L, 15000L, 20000L, 25000L, 30000L, 35000L, 40000L,
428+ 45000L, 50000L, 55000L, 60000L, 65000L, 70000L, 75000L, 80000L
429+ };
430+
431+ void doingSomething(int index)
432+ {
433+ unsigned long currentMillis = millis();
434+
435+ deltaMillis[index] = currentMillis - previousMillis[index];
436+ previousMillis[index] = currentMillis;
437+ }
438+
439+ #endif
440+
441+ ////////////////////////////////////
442+ // Shared
443+ ////////////////////////////////////
401444
402- // In NRF52, avoid doing something fancy in ISR, for example Serial.print()
403- // The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
404- // Or you can get this run-time error / crash
405445void doingSomething0()
406446{
407- unsigned long currentMillis = millis();
408-
409- deltaMillis[0] = currentMillis - previousMillis[0];
410- previousMillis[0] = currentMillis;
447+ doingSomething(0);
411448}
412449
413450void doingSomething1()
414451{
415- unsigned long currentMillis = millis();
416-
417- deltaMillis[1] = currentMillis - previousMillis[1];
418- previousMillis[1] = currentMillis;
452+ doingSomething(1);
419453}
420454
421455void doingSomething2()
422456{
423- unsigned long currentMillis = millis();
424-
425- deltaMillis[2] = currentMillis - previousMillis[2];
426- previousMillis[2] = currentMillis;
457+ doingSomething(2);
427458}
428459
429460void doingSomething3()
430461{
431- unsigned long currentMillis = millis();
432-
433- deltaMillis[3] = currentMillis - previousMillis[3];
434- previousMillis[3] = currentMillis;
462+ doingSomething(3);
435463}
436464
437465void doingSomething4()
438466{
439- unsigned long currentMillis = millis();
440-
441- deltaMillis[4] = currentMillis - previousMillis[4];
442- previousMillis[4] = currentMillis;
467+ doingSomething(4);
443468}
444469
445470void doingSomething5()
446471{
447- unsigned long currentMillis = millis();
448-
449- deltaMillis[5] = currentMillis - previousMillis[5];
450- previousMillis[5] = currentMillis;
472+ doingSomething(5);
451473}
452474
453475void doingSomething6()
454476{
455- unsigned long currentMillis = millis();
456-
457- deltaMillis[6] = currentMillis - previousMillis[6];
458- previousMillis[6] = currentMillis;
477+ doingSomething(6);
459478}
460479
461480void doingSomething7()
462481{
463- unsigned long currentMillis = millis();
464-
465- deltaMillis[7] = currentMillis - previousMillis[7];
466- previousMillis[7] = currentMillis;
482+ doingSomething(7);
467483}
468484
469485void doingSomething8()
470486{
471- unsigned long currentMillis = millis();
472-
473- deltaMillis[8] = currentMillis - previousMillis[8];
474- previousMillis[8] = currentMillis;
487+ doingSomething(8);
475488}
476489
477490void doingSomething9()
478491{
479- unsigned long currentMillis = millis();
480-
481- deltaMillis[9] = currentMillis - previousMillis[9];
482- previousMillis[9] = currentMillis;
492+ doingSomething(9);
483493}
484494
485495void doingSomething10()
486496{
487- unsigned long currentMillis = millis();
488-
489- deltaMillis[10] = currentMillis - previousMillis[10];
490- previousMillis[10] = currentMillis;
497+ doingSomething(10);
491498}
492499
493500void doingSomething11()
494501{
495- unsigned long currentMillis = millis();
496-
497- deltaMillis[11] = currentMillis - previousMillis[11];
498- previousMillis[11] = currentMillis;
502+ doingSomething(11);
499503}
500504
501505void doingSomething12()
502506{
503- unsigned long currentMillis = millis();
504-
505- deltaMillis[12] = currentMillis - previousMillis[12];
506- previousMillis[12] = currentMillis;
507+ doingSomething(12);
507508}
508509
509510void doingSomething13()
510511{
511- unsigned long currentMillis = millis();
512-
513- deltaMillis[13] = currentMillis - previousMillis[13];
514- previousMillis[13] = currentMillis;
512+ doingSomething(13);
515513}
516514
517515void doingSomething14()
518516{
519- unsigned long currentMillis = millis();
520-
521- deltaMillis[14] = currentMillis - previousMillis[14];
522- previousMillis[14] = currentMillis;
517+ doingSomething(14);
523518}
524519
525520void doingSomething15()
526521{
527- unsigned long currentMillis = millis();
528-
529- deltaMillis[15] = currentMillis - previousMillis[15];
530- previousMillis[15] = currentMillis;
522+ doingSomething(15);
531523}
532524
533- irqCallback irqCallbackFunc[NUMBER_ISR_TIMERS] =
534- {
535- doingSomething0, doingSomething1, doingSomething2, doingSomething3,
536- doingSomething4, doingSomething5, doingSomething6, doingSomething7,
537- doingSomething8, doingSomething9, doingSomething10, doingSomething11,
538- doingSomething12, doingSomething13, doingSomething14, doingSomething15
539- };
525+ #if USE_COMPLEX_STRUCT
526+
527+ ISRTimerData curISRTimerData[NUMBER_ISR_TIMERS] =
528+ {
529+ //irqCallbackFunc, TimerInterval, deltaMillis, previousMillis
530+ { doingSomething0, 5000L, 0, 0 },
531+ { doingSomething1, 10000L, 0, 0 },
532+ { doingSomething2, 15000L, 0, 0 },
533+ { doingSomething3, 20000L, 0, 0 },
534+ { doingSomething4, 25000L, 0, 0 },
535+ { doingSomething5, 30000L, 0, 0 },
536+ { doingSomething6, 35000L, 0, 0 },
537+ { doingSomething7, 40000L, 0, 0 },
538+ { doingSomething8, 45000L, 0, 0 },
539+ { doingSomething9, 50000L, 0, 0 },
540+ { doingSomething10, 55000L, 0, 0 },
541+ { doingSomething11, 60000L, 0, 0 },
542+ { doingSomething12, 65000L, 0, 0 },
543+ { doingSomething13, 70000L, 0, 0 },
544+ { doingSomething14, 75000L, 0, 0 },
545+ { doingSomething15, 80000L, 0, 0 }
546+ };
547+
548+ void doingSomething(int index)
549+ {
550+ unsigned long currentMillis = millis();
551+
552+ curISRTimerData[index].deltaMillis = currentMillis - curISRTimerData[index].previousMillis;
553+ curISRTimerData[index].previousMillis = currentMillis;
554+ }
555+
556+ #else
540557
541- ////////////////////////////////////////////////
558+ irqCallback irqCallbackFunc[NUMBER_ISR_TIMERS] =
559+ {
560+ doingSomething0, doingSomething1, doingSomething2, doingSomething3,
561+ doingSomething4, doingSomething5, doingSomething6, doingSomething7,
562+ doingSomething8, doingSomething9, doingSomething10, doingSomething11,
563+ doingSomething12, doingSomething13, doingSomething14, doingSomething15
564+ };
542565
566+ #endif
567+ ///////////////////////////////////////////
543568
544569#define SIMPLE_TIMER_MS 2000L
545570
@@ -556,11 +581,15 @@ void simpleTimerDoingSomething2s()
556581
557582 unsigned long currMillis = millis();
558583
559- Serial.printf("SimpleTimer : %ds , ms = %lu, Dms : %lu\n", SIMPLE_TIMER_MS / 1000, currMillis, currMillis - previousMillis);
584+ Serial.printf("SimpleTimer : %lus , ms = %lu, Dms : %lu\n", SIMPLE_TIMER_MS / 1000, currMillis, currMillis - previousMillis);
560585
561586 for (int i = 0; i < NUMBER_ISR_TIMERS; i++)
562587 {
588+ #if USE_COMPLEX_STRUCT
589+ Serial.printf("Timer : %d, programmed : %lu, actual : %lu\n", i, curISRTimerData[i].TimerInterval, curISRTimerData[i].deltaMillis);
590+ #else
563591 Serial.printf("Timer : %d, programmed : %lu, actual : %lu\n", i, TimerInterval[i], deltaMillis[i]);
592+ #endif
564593 }
565594
566595 previousMillis = currMillis;
@@ -573,7 +602,7 @@ void setup()
573602 Serial.begin(115200);
574603 while (!Serial);
575604
576- Serial.printf("\nStarting ISR_16_Timers_Array on %s\n", BOARD_NAME);
605+ Serial.printf("\nStarting ISR_16_Timers_Array_Complex on %s\n", BOARD_NAME);
577606 Serial.printf("Version : v%s\n", NRF52_MBED_TIMER_INTERRUPT_VERSION);
578607
579608 // Interval in microsecs
@@ -589,8 +618,13 @@ void setup()
589618 // You can use up to 16 timer for each ISR_Timer
590619 for (int i = 0; i < NUMBER_ISR_TIMERS; i++)
591620 {
621+ #if USE_COMPLEX_STRUCT
622+ curISRTimerData[i].previousMillis = startMillis;
623+ ISR_Timer.setInterval(curISRTimerData[i].TimerInterval, curISRTimerData[i].irqCallbackFunc);
624+ #else
592625 previousMillis[i] = startMillis;
593626 ISR_Timer.setInterval(TimerInterval[i], irqCallbackFunc[i]);
627+ #endif
594628 }
595629
596630 // You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
@@ -617,13 +651,13 @@ void loop()
617651
618652### Debug Terminal Output Samples
619653
620- 1 . The following is the sample terminal output when running example [ ISR_16_Timers_Array ] ( examples/ISR_16_Timers_Array ) on ** Nano 33 BLE** to demonstrate the accuracy of ISR Hardware Timer, ** especially when system is very busy** . The ISR timer is ** programmed for 2s, is activated exactly after 2.000s !!!**
654+ 1 . The following is the sample terminal output when running example [ ISR_16_Timers_Array_Complex ] ( examples/ISR_16_Timers_Array_Complex ) on ** Nano 33 BLE** to demonstrate the accuracy of ISR Hardware Timer, ** especially when system is very busy** . The ISR timer is ** programmed for 2s, is activated exactly after 2.000s !!!**
621655
622656While software timer, ** programmed for 2s, is activated after more than 3.000s in loop().
623657
624658```
625- Starting ISR_16_Timers_Array on Nano 33 BLE
626- Version : 1.0.1
659+ Starting ISR_16_Timers_Array_Complex on Nano 33 BLE
660+ Version : 1.0.2
627661NRF52_MBED_TimerInterrupt: Timer = NRF_TIMER3
628662NRF52_MBED_TimerInterrupt: _fre = 1000000.00, _count = 10000
629663Starting ITimer OK, millis() = 714
@@ -1095,7 +1129,7 @@ Timer : 15, programmed : 80000, actual : 80009
10951129
10961130```
10971131Starting TimerInterruptTest on Nano 33 BLE
1098- Version : v1.0.1
1132+ Version : v1.0.2
10991133NRF52_MBED_TimerInterrupt: Timer = NRF_TIMER3
11001134NRF52_MBED_TimerInterrupt: _fre = 1000000.00, _count = 1000000
11011135Starting ITimer0 OK, millis() = 5660
@@ -1126,7 +1160,7 @@ Start ITimer0, millis() = 60680
11261160
11271161```
11281162Starting Argument_None on Nano 33 BLE
1129- Version : 1.0.1
1163+ Version : 1.0.2
11301164NRF52_MBED_TimerInterrupt: Timer = NRF_TIMER1
11311165NRF52_MBED_TimerInterrupt: _fre = 1000000.00, _count = 500000
11321166Starting ITimer0 OK, millis() = 1519
@@ -1155,7 +1189,7 @@ In this example, 16 independent ISR Timers are used, yet utilized just one Hardw
11551189
11561190```
11571191Starting ISR_16_Timers_Array on NRF52840_ITSYBITSY
1158- Version : 1.0.1
1192+ Version : 1.0.2
11591193CPU Frequency = 64 MHz
11601194NRF52TimerInterrupt: F_CPU (MHz) = 64, Timer = NRF_TIMER2
11611195NRF52TimerInterrupt: _fre = 1000000.00, _count = 1000
@@ -1197,6 +1231,10 @@ simpleTimer2s:Dms=10004
11971231---
11981232---
11991233
1234+ ### Releases v1.0.2
1235+
1236+ 1 . Add example [ ISR_16_Timers_Array_Complex] ( examples/ISR_16_Timers_Array_Complex ) and optimize example [ ISR_16_Timers_Array] ( examples/ISR_16_Timers_Array )
1237+
12001238### Releases v1.0.1
12011239
120212401 . Initial coding for Nano-33-BLE and sync with [ ** NRF52_TimerInterrupt Library** ] ( https://github.com/khoih-prog/NRF52_TimerInterrupt )
0 commit comments