问题是:您是否希望您的计数器长期计算时间间隔?
或者您希望某些事件每秒钟都发生一次?有什么容忍度?
例如,如果您想计算 10 分钟,在显示屏上显示秒计数器,则接受 10-20 毫秒的抖动,即如果您的显示屏上的第二个计数器将提前或推迟 10 毫秒更新, 没关系。在这种情况下,您可以使用 Bresenham 的算法来纠正错误。您通过计时器溢出间隔(256)的数量增加了一些变量。当变量超过一秒的值(即 2000000 / 64 = 31250)时,您将其减少它的值并处理每秒事件。从长远来看,第二个计数器将精确计算秒数。显然,您可以将两个数字除以相同的数量。 IE。分别使用 128 和 15625。
uint16_t second_counter = 0;
uint16_t counter_error = 0;
ISR(TIMER0_OVF_vect) {
counter_error += 128; // Increase the error counter;
if (counter_error >= 15625) { // On overflow
counter_error -= 15625; // Decrease the error counter;
second_counter++; // Increase the second counter, or do any other once-per-second action
}
}
在 122 或 123 定时器溢出后,条件内的每秒事件将不会完全在秒边界触发。但平均而言,它将恰好每秒一次。此方法适用于定时器同时用于其他用途,且无法更改分辨率的情况。
但是,如果您希望每秒准确地发生第二个事件,则在 CTC 模式下使用计数器,以每秒溢出整数次的方式限制它的最大值。
假设您可以选择 64 位预分频器,并将 TOP 值设置为 249(从而使计时器周期等于 250),这使您每秒恰好有 125 次溢出。
uint8_t overflow_counter = 0;
uint16_t second_counter = 0;
ISR(TIMER0_OVF_vect) {
overflow_counter++;
if (overflow_counter >= 125) { // waiting for 125th overflow;
overflow_counter = 0; // reset the counter;
second_counter++; // Increase the second counter, or do any other once-per-second action
}
}
OCR0A = 249; // from 0 to 249 = 250 counts.
TIMSK0 = (1 << TOIE0); // Allow overflow interrupt
TCCR0A = (1 << WGM01); // Enable CTC mode. OCRA defines the top value
TCCR0B = (1 << CS01) | (1 << CS00); // Start with 1:64 prescaler
sei(); // Enable interrupts