change timer module to run @20Mhz.

timebase of 1ms can't be reached perfectly with Timer0 at 20MHz, unfortunatly.
This commit is contained in:
Stefan Rupp 2014-03-09 00:25:02 +01:00
parent ae83f35241
commit 598281a193

17
timer.c
View File

@ -39,16 +39,19 @@ void timer_init(void)
timer_time = 0;
// - Time accuracy: 1 millisecond (corresponding frequency: 1kHz)
// ==> F_CPU = 2Mhz
// ==> 16Mhz / 64 = 250 kHz
// ==> let timer count to 250 to get 1kHz frequency
// ==> F_CPU = 20Mhz
// ==> 20Mhz / 256 = 78.125 kHz
// ==> let timer count to 78 to get (almost) 1kHz frequency
// therefore:
// - Set Timer/Counter0 prescaler to 64 ==> (1<<CS01)|(1<<CS00)
// - Set OCR2 to 249
// - Set Timer/Counter0 prescaler to 256 ==> (1<<CS02)
// - Set OCR2 to 78
// - CTC ( i.e. clear counter, when COUNTER == OCR0A) ==> (1<<WGM01)
OCR0A = 249;
// unfortunately, due to the 20MHz and the coarse prescaler dividers
// provided, we can't get any closer to the desired frequency of
// 1kHz :(
OCR0A = 78;
TCCR0B = (1<<WGM02);
TCCR0A = (1<<CS01)|(1<<CS00);
TCCR0A = (1<<CS02);
// Interrupts setzen
TIMSK0 |= (1<<OCIE0A);