定时器每 4ms 产生一次溢出

发布于 2024-12-11 07:04:39 字数 213 浏览 4 评论 0原文

在atmega2560(STK600板)上,我使用Timer0,它是8位定时器。我想每 4 毫秒生成一次溢出...如何将计时器设置为 4 毫秒? (时钟速度是8MHz,我知道通过潜水(时钟速度)/(预分频器)来设置定时器,当计数器重置时,它会产生溢出中断。但不确定是否设置为4ms!

其次,一旦定时器溢出并生成 OVERFlOW 中断,计算 1 秒..如何做到这一点?

提前致谢!

On atmega2560 (STK600 board), I am using Timer0 which is 8-bit timer. I want to generate an overflow every 4ms...how do I set the timer for 4ms? (the clock speed is 8MHz, I am aware of setting up a timer by diving (clock speed)/(prescaler) and when the counter resets, it generates an overflow interrupt. But not sure about setting for 4ms!

Secondly, once the timer overflows and generates an OVERFlOW interrupt, calculate 1 second..How to do that?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2024-12-18 07:04:39

OVERFLOW 中断不可能达到 4ms:

F_CPU = 8,000,000hz

F_OVERFLOW = 1/0.004s = 250hz

CYCLES_PER_OVERFLOW = F_CPU / F_OVERFLOW = 32,000,即:每 32,000 个 CPU 时钟周期应发生一次溢出

CYCLES_PER_TIMER_TICK = 32,000 / 256 = 125 = 预分频器值

125 不可用作预分频器,但如果该值足够接近您的目的 (4.096ms),您可以使用 128。
如果没有,可以考虑使用定时器比较中断来代替溢出int。 - 或者更改 F_CPU...

一旦每 4ms 发生一次中断,您可以在每次中断时递增一个全局变量,每次该变量的值达到 250 时,又过去了一秒,该变量可以重新设置为 0 以计入下一秒。

4ms won't be possible with the OVERFLOW interrupt:

F_CPU = 8,000,000hz

F_OVERFLOW = 1/0.004s = 250hz

CYCLES_PER_OVERFLOW = F_CPU / F_OVERFLOW = 32,000, i.e.: An overflow should occur each 32,000 CPU clock cycles

CYCLES_PER_TIMER_TICK = 32,000 / 256 = 125 = prescaler value

125 is not available as prescaler, but you can use 128 if that's close enough for your purpose (4.096ms).
If not, you can consider to use a timer compare interrupt instead of the overflow int. - Or change F_CPU...

Once you have your interrupt each 4ms, you can increment a global variable on each interrupt and each time the variable's value reaches 250, another second has passed and the variable can be re-set to 0 to count towards the next second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文