【问题标题】:Timer counter 0 and make a real second problem计时器计数器 0 并提出真正的第二个问题
【发布时间】:2019-09-14 07:51:37
【问题描述】:

我想在 portuse 中模拟一个代码。 我有一个 LED 连接到一个 2MHZ 频率的微型引脚。 我想用 presclare 64 在 timer0 中编码。 我计算它需要在计时器中计数 31128。 但是 timer0 只能计算到 255。 所以我想使用溢出中断并定义一个变量来计算溢出。 如果超跌达到 122.07 一秒通过。 我使用 122 溢出中断。但是对于剩下的 0.07,我需要在 tcnt 中数到 18。所以我想先使用 OCR。

我的问题是:这个方法是真的还是假的? 我应该如何编写这段代码?

【问题讨论】:

  • 您有问题吗,您没有得到准确的秒数?

标签: c timer avr atmelstudio


【解决方案1】:

问题是:您是否希望您的计数器长期计算时间间隔? 或者您希望某些事件每秒钟都发生一次?有什么容忍度?

例如,如果您想计算 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2023-03-22
    相关资源
    最近更新 更多