What is the easiest and simplest way to create a decrementing timer in seconds?
在几秒钟内创建递减计时器的最简单最简单的方法是什么?
Im using an 8bit microcontroller and cannot use float points or intensive processes. I need a very efficient timer.
我使用8位微控制器,不能使用浮点或密集过程。我需要一个非常有效的计时器。
2 个解决方案
#1
3
time(2)
is a standard C function for obtaining the current time in seconds since the epoch (midnight on January 1, 1970 UTC). To make a decrementing timer, you could do something like this:
time(2)是用于获得自纪元(1970年1月1日午夜)以来以秒为单位的当前时间的标准C函数。要制作递减计时器,您可以执行以下操作:
time_t startTime = time(NULL);
time_t timerDuration = 10; // 10 second timer
time_t endTime = startTime + timerDuration;
while(1)
{
time_t currTime = time(NULL);
time_t timeLeft = endTime - currTime;
if(timeLeft < 0)
{
// timer has finished
break;
}
// do stuff - preferably don't spin at 100% CPU
}
If you need more precision than 1-second increments, you can use a platform-specific function such as gettimeofday(3)
(POSIX) or GetLocalTime
(Windows).
如果您需要更高的精度而不是1秒的增量,则可以使用特定于平台的函数,例如gettimeofday(3)(POSIX)或GetLocalTime(Windows)。
#2
1
It looks like you want an asynchronous timer. On a 8-bit controller I suppose you don't have multithreading. But you probably have direct access to hardware timers; most 8-bit controllers have several. Either allocate one of the timers for your needs, or use an existing one with known period. It is as efficient as you can get.
看起来你想要一个异步计时器。在8位控制器上,我想你没有多线程。但是你可能直接访问硬件定时器;大多数8位控制器都有几个。根据需要分配其中一个计时器,或使用已知周期的现有计时器。它尽可能高效。
Every time the timer ticks, you receive an interrupt. This may be every second or more frequently. Update a variable that keeps time, and return from the interrupt handler (or pass control to a chained handler if you hook on a timer used for other needs).
每次定时器滴答时,都会收到中断。这可能是每隔一秒或更频繁。更新一个保留时间的变量,并从中断处理程序返回(或者如果挂钩用于其他需要的计时器,则将控制传递给链式处理程序)。
If the time-keeping variable is over the threshold, that's your event; take your action. Be sure not to make this action take too long, and consult your controller reference regarding actions not allowed within interrupt handlers.
如果计时变量超过阈值,那就是你的事件;采取你的行动。请确保不要将此操作花费太长时间,并查阅控制器参考有关中断处理程序中不允许的操作。
#1
3
time(2)
is a standard C function for obtaining the current time in seconds since the epoch (midnight on January 1, 1970 UTC). To make a decrementing timer, you could do something like this:
time(2)是用于获得自纪元(1970年1月1日午夜)以来以秒为单位的当前时间的标准C函数。要制作递减计时器,您可以执行以下操作:
time_t startTime = time(NULL);
time_t timerDuration = 10; // 10 second timer
time_t endTime = startTime + timerDuration;
while(1)
{
time_t currTime = time(NULL);
time_t timeLeft = endTime - currTime;
if(timeLeft < 0)
{
// timer has finished
break;
}
// do stuff - preferably don't spin at 100% CPU
}
If you need more precision than 1-second increments, you can use a platform-specific function such as gettimeofday(3)
(POSIX) or GetLocalTime
(Windows).
如果您需要更高的精度而不是1秒的增量,则可以使用特定于平台的函数,例如gettimeofday(3)(POSIX)或GetLocalTime(Windows)。
#2
1
It looks like you want an asynchronous timer. On a 8-bit controller I suppose you don't have multithreading. But you probably have direct access to hardware timers; most 8-bit controllers have several. Either allocate one of the timers for your needs, or use an existing one with known period. It is as efficient as you can get.
看起来你想要一个异步计时器。在8位控制器上,我想你没有多线程。但是你可能直接访问硬件定时器;大多数8位控制器都有几个。根据需要分配其中一个计时器,或使用已知周期的现有计时器。它尽可能高效。
Every time the timer ticks, you receive an interrupt. This may be every second or more frequently. Update a variable that keeps time, and return from the interrupt handler (or pass control to a chained handler if you hook on a timer used for other needs).
每次定时器滴答时,都会收到中断。这可能是每隔一秒或更频繁。更新一个保留时间的变量,并从中断处理程序返回(或者如果挂钩用于其他需要的计时器,则将控制传递给链式处理程序)。
If the time-keeping variable is over the threshold, that's your event; take your action. Be sure not to make this action take too long, and consult your controller reference regarding actions not allowed within interrupt handlers.
如果计时变量超过阈值,那就是你的事件;采取你的行动。请确保不要将此操作花费太长时间,并查阅控制器参考有关中断处理程序中不允许的操作。