【发布时间】:2011-03-06 22:54:45
【问题描述】:
我需要处理一个计数器,它可以为我的应用程序提供记号。计数器是 32 位的,所以我需要知道的是如何处理它。例如:
我有一个返回 (timestamp + shifttime) 的函数,我还有另一个函数将返回 1 或 0,具体取决于时间是否已过,但我的计数器有可能会换行,我该如何处理这个?
非常感谢大家的回复。我将在此编辑中提供更多详细信息。
我正在使用 STM32 Cortex-M3。我想使用 RTC 计数器将其用作我的应用程序的刻度,以安排需要在特定时间间隔发生的任务。 RTC 可以产生溢出中断,因此检测中断不是问题。我遇到的主要问题(或者至少我认为是一个问题)是某些任务获得(timestamp+shift) 即
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 位长以便于计算。
如果我获得时间戳时当前计数器为 250,这意味着 FlashLedTimeStamp = 14 和 StatusLedTimeStamp = 253 我将如何检查 FlashLedTimeStamp 是否已过期??
请记住,我不一定会一直检查以查看当前计数器是什么以及某些时间戳是否已过期。我希望这能说明我遇到的问题。
【问题讨论】:
-
你的时钟滴答率是多少,计数器的起始值是多少?此外,您能否编辑有关您正在运行的平台(操作系统、芯片组、主板制造商等)的更多详细信息。可能有一些特定于平台的解决方案。
标签: c embedded integer-overflow