Trying to figure out the timer setup in Atmel Studio 6 for an ATmega16 microcontroller. And I just don't get it. I am trying to create a TickCount similar to GetTickCount() Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.
Code looks like this:
#define F_CPU 8000000UL // 8MHz - prevents default 1MHz
// *** timer code necessary for handling lost coms, use volatile when variable is accessed from interrupts
volatile unsigned long TickCountSys; // Number of milliseconds that have elapsed since the system was started.
volatile unsigned long TickCountComs; // TickCountForLastComs
volatile unsigned long TCNT0_overflow_count = 0;
ISR(USART_RXC_vect) {
TickCountComs = TickCountSys; // Milliseconds since system started when received something.
//blah blah
}
void timer_init() {
TIMSK=(1<<TOIE0); // Enable timer overflow interrupt for timer0
TCNT0=0x00; // Set timer0 counter initial value to 0
TCCR0 = (1<<CS00); // Start timer0 without prescaler
}
ISR(TIMER0_OVF_vect) {
TickCountSys++;
}
int main(void) {
timer_init();
DDRC = 0xff; // Set all pins on PORTC for output
sei();
unsigned long TickCountENQ; // Tick count for sending enquiry character, 05dec, ^E
char coms = 0;
char ignore_coms = 0;
while(1) {
coms = (TickCountSys < TickCountComs + 50);
if (TickCountSys > TickCountENQ + 25) {
TickCountENQ = TickCountSys;
// If not ignore coms, that is need coms, and has coms, i.e. has not lost coms, then send new Enquiry char every 25 ms
// It stops asking for coms if coms is lost, and does nothing until new command received and then starts syncing again
if (!ignore_coms && coms) USARTWriteChar(0x05);
}
if (!coms && !ignore_coms) { // If not coms and not ignoring lost coms then stop all movement
PORTC = 0x00;
}
//blah blah...
}
}
Tools-->Device Programming-->Fuses says:
BODLEVEL 4V0
SUT_CKSEL EXTMEDFXTALRES_16KCK_64MS
As far as I know this is the correct settings (was delivered that way).
There is just something I do not understand by reading the timer tutorials.