【发布时间】:2014-10-11 17:18:22
【问题描述】:
我正在尝试在 Atmega8 上做一个时钟。 我有 8Mhz 石英。
我使用 timer0 中断作为时钟计时:
/* Settings */
#define TMR_RELOAD 80 - 5 /* 8 kHz / 80 = 100 Hz */
#define TMR_CNT_MAX 100 /* 1Hz internal counter */
/* internal variables */
static uint8_t tmr_cnt;
inline void TMR0_Init()
{
/* set clock source f_t0 = 8МHz/1024 = 8 kHz */
TCCR0 = _BV(CS00) | _BV(CS02);
TIMSK |= _BV(TOIE0); /* Enable TMR0 interrupt on overflow*/
}
ISR (TIMER0_OVF_vect)
{
if (tmr_cnt == 0)
Clock_Tick1s();
tmr_cnt++;
if (tmr_cnt >= TMR_CNT_MAX)
tmr_cnt = 0;
TCNT0 -= TMR_RELOAD;
}
问题是我的时钟跑得太快或太慢了。
我放入 TCNT0 寄存器的计算值是 80,但在这种情况下时钟运行得太慢了。 当我使用 80-4 时,时钟也运行得太慢了。我用80-5的时候太快了。
我不知道,怎么可能???
更新: 现在设置如下,但问题依然存在。
/* Settings */
#define TMR_RELOAD 125 /* 31.25 kHz / 125 = 250 Hz */
#define TMR_CNT_MAX 250 /* 1Hz internal counter */
inline void TMR0_Init()
{
/* set clock source f_t0 = 8МHz/256 = 31.25 kHz */
TCCR0 = _BV(CS02);
TIMSK |= _BV(TOIE0); /* Enable TMR0 interrupt on overflow*/
}
ISR (TIMER0_OVF_vect)
{
TCNT0 -= TMR_RELOAD;
if (tmr_cnt == 0)
Clock_Tick1s();
tmr_cnt++;
if (tmr_cnt >= TMR_CNT_MAX)
tmr_cnt = 0;
}
【问题讨论】:
-
“太快”是多少?例如每小时多少秒?有示波器吗?
-
每 3 小时大约 5 分钟。不,我没有示波器。
标签: microcontroller avr avr-gcc atmel atmega