khoih-prog / megaAVR_TimerInterrupt

This library enables you to use Interrupt from Hardware Timers on an ATmega4809-based board, such as Arduino UNO WiFi Rev2, AVR_NANO_EVERY, etc. It now supports 16 ISR-based timers, while consuming only 1 hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks.
MIT License
5 stars 1 forks source link

TimerB0 does not trigger interrupt #4

Closed cattledogGH closed 1 year ago

cattledogGH commented 1 year ago

TimerB0 will not trigger an interrupt. I think there is a bug in TimerInterrupt::setFrequency()

 //if ((_timer <= 0) || (callback == NULL) || ((frequencyLimit) < 1) )
  if ((_timer < 0) || (callback == NULL) || ((frequencyLimit) < 1) )

If I make this change to the source code, TimerB0 appears to be working properly.

Simple Blink sketch using TimerB0 will not blink LED. TimerB1 blinks the LED as expected.

#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 0

#define USING_16MHZ true
#define USING_8MHZ false
#define USING_250KHZ false

#define USE_TIMER_0 true  
//#define USE_TIMER_1 true  

#include "megaAVR_TimerInterrupt.h"

#define FREQUENCY 1

volatile uint32_t TimerCount = 0;

void printResult(uint32_t currTime)
{
  Serial.print(F("Time = "));
  Serial.print(currTime);
  Serial.print(F(", TimerCount = "));
  Serial.println(TimerCount);
}

void TimerHandler()
{
  TimerCount++;
  digitalWrite(13, !digitalRead(13));  //blink led
}

void setup()
{
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  Serial.begin(115200);
  while (!Serial);

  ITimer0.init();
  ITimer0.attachInterrupt(FREQUENCY, TimerHandler);
  //ITimer1.init();
  //ITimer1.attachInterrupt(FREQUENCY, TimerHandler);  
}

#define CHECK_INTERVAL_MS 10000L

void loop()
{

  static uint32_t lastTime = 0;
  static uint32_t currTime;

  currTime = millis();

  if (currTime - lastTime > CHECK_INTERVAL_MS)
  {
    printResult(currTime);
    lastTime = currTime;
  }
}

Expected behavior

TimerB0 triggers interrupt to blink led and increase count.

Actual behavior

No interrupt is triggered. Count ==0 and led does not blink

Debug and AT-command log (if applicable)

[TISR] setFrequency error

Information

khoih-prog commented 1 year ago

HI @cattledogGH

Thanks for your real bug spot. I'll fix the mistake and will quickly release a new version.

Best Regards,

khoih-prog commented 1 year ago

Hi @cattledogGH

Thanks to your bug spot, the new megaAVR_TimerInterrupt v1.7.0 has just been released to fix bug disabling TCB0. Your contribution is noted in Contributions and Thanks.

Looking forward to receiving more of your valuable bug reports.

Best Regards,


Release v1.7.0

  1. Fix bug disabling TCB0. Check TimerB0 does not trigger interrupt #4
  2. Use allman astyle and add utils