如何处理嵌入式C中的包装计数器 [英] How to deal with a wrapping counter in embedded C

查看:123
本文介绍了如何处理嵌入式C中的包装计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理一个计数器,为我的应用程序打勾。柜台是32位,所以我需要知道的是如何处理它,当它包装。例如:

I need to deal with a counter that gives me ticks for my application. The counter is 32bits so what i need to know is how to deal with it when it wraps. for example:

我有一个函数返回一个(timestamp + shifttime),我有另一个函数将返回1或0,这取决于时间是否过去,但是我的柜台可能会包装怎么处理这个?

I have a function that returns a (timestamp + shifttime) and i have another function that will return 1 or 0 depending whether or not the time has elapsed, but there could a possibility that that my counter will wrap how do I deal with this ?.

感谢

非常感谢所有回复的人。我将在这个编辑中给出更多的细节。

Thanks a lot for all the responses guys. I will give more detail in this edit.

我正在使用STM32 Cortex-M3。我想使用RTC计数器将其用作我的应用程序的刻度来计划需要在某些时间间隔内发生的任务。 RTC可以产生溢出中断,因此检测中断不是问题。我有(或至少我认为是一个问题)的主要问题是某些任务得到(时间戳+移位)ie。

I am using the STM32 Cortex-M3. I wanna use the RTC counter to use it as the tick for my application to schedule tasks that need to happen at certain intervals. The RTC can generate an overflow interrupt so it's not a problem to detect the interrupt. the main problem that I have(or at least I think is a problem) is when certain tasks gets a (timestamp+shift) ie.


int main( void )
{
FlashLedTimeStamp = ReturnCounter( 20 );  // currentcounter value + a shift of 20
StatusLedTimeStamp = ReturnCounter( 3 );  // currentcounter value + a shift of 3

//then later on ....
while(1)
{
    /* other tasks could go here */

    if( HasTimeElapsed( FlashLedTimeStamp );
    {
       /* do something and get another timestamp value */
       FlashLedTimeStamp = ReturnCounter( 20 );  // currentcounter value + a shift of 20
    }

    if( HasTimeElapsed( StatusLedTimeStamp );
    {
       /* do something and get another timestamp value */
       FlashLedTimeStamp = StatusLedTimeStamp( 3 );  // currentcounter value + a shift of 3
    }
}   
}

假设我的RTC计数器只有8位长,使数学变得简单。

lets assume that my RTC counter is only 8 bits long to make the math easy.

如果我当前的计数器在250当我得到我的时间戳,这意味着FlashLedTimeStamp = 14和StatusLedTimeStamp = 253我将如何查看FlashLedTimeStamp已过期?

if my current counter is at 250 when I get my timestamps that means that FlashLedTimeStamp = 14 and StatusLedTimeStamp = 253 how would i check to see that FlashLedTimeStamp has expired ??

请记住,我不一定检查一直看到当前的计数器是什么,以及某些时间戳是否已经过期。我希望这清楚地说明了我所遇到的问题是感谢所有先进的。

keep in mind that I dont necessarily check all the time to see what the current counter is and whether or not certain timestamp has expired. I hope this makes it clear what the problem I have is thanks to all in advanced.

推荐答案

如果您采用两个时间戳读数,而您的第一读数比第二次更大,那么您的柜台已经包裹了。这是检测包装计数器的基本方法。

If you take two timestamp readings and your first reading is greater than the second, then your counter has wrapped. That is the basic way to detect a wrapping counter.

然而,这不会检测到一个计数器是否多次包装,或者一个计数器已经包装的情况恰好比一读大。既然你说这是一个嵌入式系统,你的描述使你的计数器听起来像一个时钟,看看当时钟达到零时,是否可以设置一个中断,以便每当时钟复位时都会产生一个中断。当该中断触发时,增加一个单独的计数器。这应该有效地为您的时钟增加额外的精度,并允许您的计数器包装,而不会导致问题。

This, however, won't detect if a counter has wrapped multiple times, or the case where a counter has wrapped and happens to be greater than the first reading. Since you said this was an embedded system and your description makes your "counter" sound like a clock, see if you can set an interrupt to fire whenever the clock reaches zero (so that you will get an interrupt every time the clock resets). When this interrupt fires, increment a separate counter. This should effectively add extra precision to your clock and allow your counter to wrap without causing problems.

这篇关于如何处理嵌入式C中的包装计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆