microcontroller - Clock on Atmega8 -
microcontroller - Clock on Atmega8 -
i trying clock on atmega8. have 8mhz quartz.
i utilize timer0 interrupt clock timeticks:
/* 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; }
the problem clock running fast or slow.
calculated value set in tcnt0 register 80, in case clock running slow. when utilize 80-4 clock running slow. when utilize 80-5, it's fast.
i don't know, how be???
update: settings following, problem still exists.
/* 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; }
note when phone call
tcnt0 -= tmr_reload;
the tcnt0 register 0. tcnt0 = 131 requires 124 ticks overflow. means clock
1/31250 * 250 = 8ms
faster per second, results in
8ms * 60 x 60 x3 = 86.4s
per 3 hours. not 5 minutes mentioned in comments error there. there potentially additional error coming 8mhz crystal. depends on crystal quality , how resonant circuit designed.
i advice utilize output compare interrupt of timer2 instead of using overflow interrupt , reloading tcnt value.
"the output compare register contains 8-bit value continuously compared counter value (tcnt2). match can used generate output compare interrupt..."
its easier handle , don't have deal "reloading" timer.
you consider using 32.768khz clock crystal.
microcontroller avr avr-gcc atmel atmega
Comments
Post a Comment