是不是可以使用dspic30f4011中的timer5获得一个24小时的定时器中断?

huangapple go评论67阅读模式
英文:

is it possible to get a 24 hour timer interrupt using timer5 in dspic30f4011?

问题

我正在尝试使用dspic30f4011的timer5,在1:256的预分频率下,在24小时后触发警报。

使用计时器的最大值(0xFFFF),我尝试了计数器值为125和250,对于这些值,它可以正常工作,但如果我输入250之后的任何值,我的计时器似乎会卡住,不会触发警报。

英文:

I am trying to trigger an alarm after 24 hours using timer5 of dspic30f4011 using 1:256 as prescale.

using maximum value of timer (0xFFFF) i tried iterating for counter of 125 and 250, for this values it works but if i enter any value after 250 my timer seems to be stuck and it doesnot trigger alarm.

答案1

得分: 1

根据我顶部的评论提到,我们需要一个MRE。

但是,24小时对于间隔计时器来说是一个_很长_的时间。因此,我认为纯粹使用计时器硬件可能无法完成,因为计时器比较寄存器据我所知仅限于16位。即使以每秒一个中断,这也是86400(0x15180)个中断,超出了16位。

我们将不得不在S/W中执行此操作[使用“tick”计数器]。这是大多数系统的做法。

以下是一个示例。它假设计时器ISR每毫秒调用一次[根据需要进行调整]:

#define TICKS_PER_SECOND	1000LL

uint64_t tick_count = 0;				// 单调的滴答计数器
uint64_t tick_interval = TICKS_PER_SECOND * 24 * 60 * 60; // 1天的滴答数
uint64_t tick_fire = tick_interval;		// 下一次回调的时间

void
timer_ISR(void)
{

	// 推进滴答计数器
	tick_count += 1;

	// 我们是否达到所需的间隔?
	if (tick_count >= tick_fire) {
		// 推进到下一个间隔
		tick_fire = tick_count + tick_interval;

		// 注意:首先执行此操作允许回调函数调整下一个事件的触发时间

		// 每24小时调用一次...
		timer_24_hours();
	}
}

希望这有所帮助。

英文:

As mentioned in my top comments, we want an MRE.

But, 24 hours is a long time for an interval timer. So, I don't think this can be done purely with the timer hardware as the timer compare registers are [AFAICT] limited to 16 bits. Even at one interrupt / second, this is 86400 (0x15180) interrupts which is beyond 16 bits

We'll have to do this in S/W [with a "tick" counter]. This is how most systems do it.

Here is an example. It assumes that the timer ISR is called once per millisecond [adjust to suit]:

#define TICKS_PER_SECOND	1000LL

uint64_t tick_count = 0;				// monotonic tick counter
uint64_t tick_interval = TICKS_PER_SECOND * 24 * 60 * 60; // ticks for 1 day
uint64_t tick_fire = tick_interval;		// time of next callback

void
timer_ISR(void)
{

	// advance tick counter
	tick_count += 1;

	// have we hit the required interval?
	if (tick_count >= tick_fire) {
		// advance to next interval
		tick_fire = tick_count + tick_interval;

		// NOTE: doing that first allows the callback function to adjust the
		// firing time of the next event

		// called once every 24 hours ...
		timer_24_hours();
	}
}

huangapple
  • 本文由 发表于 2023年6月8日 03:05:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定