I'm using ATMEGA8 for my project. I want to make a LCD display a clock. The below code works fine and displays a code in proper format on the LCD.
But the problem is the timer/counter is not getting synced with real clock, it is having 2-3 second difference with the real time on my watch. I've been tried changing with the values of counter_32_ms variable to get it synced. In the starting, the initial value was 31 and was showing correct clock, then again, it started to wait 10 sec(in my watch) to count 1 second(on the display). Then I put counter_32_ms value to 5, it works fine,, but then again, it was having difference with real clock. Not sure what's happening.
Please advise and help me to fix it.
Below is the code i'm using:
Also, these are my fuse settings:
avrdude.exe: safemode: lfuse reads as E1
avrdude.exe: safemode: hfuse reads as D9
avrdude.exe: safemode: Fuses OK
But the problem is the timer/counter is not getting synced with real clock, it is having 2-3 second difference with the real time on my watch. I've been tried changing with the values of counter_32_ms variable to get it synced. In the starting, the initial value was 31 and was showing correct clock, then again, it started to wait 10 sec(in my watch) to count 1 second(on the display). Then I put counter_32_ms value to 5, it works fine,, but then again, it was having difference with real clock. Not sure what's happening.
Please advise and help me to fix it.
Below is the code i'm using:
C-like:
void init_timer ()
{
//Init timer0 overflow interrupt
TIMSK |= (1 << TOIE0);
//Start timer at 1024 prescaler
TCCR0 |= (1 << CS02) | (1 << CS00);
}
ISR(TIMER0_OVF_vect)
{
static uint8_t counter_32_ms = 0;
//Each overflow is 32.7 msec
counter_32_ms++;
if (counter_32_ms >= 3.7)
{
if (seconds<60)
{
seconds++;
}
if (seconds==60)
{
if (minutes<60)
{
minutes++;
}
seconds=0;
}
if (minutes==60)
{
if (hours<240)
{
hours++;
}
minutes=0;
}
if (hours==240)
{
hours=0;
}
}
}
void updateLCD() {
char timeString[9]; // HH:MM:SS\0
sprintf(timeString, "%02d:%02d:%02d", hours, minutes, seconds); // Convert hours, minutes, and seconds to strings
LCD_goto_XY(0,3); // Set the cursor position on the LCD
LCD_print(timeString); // Display the time on the LCD
}
avrdude.exe: safemode: lfuse reads as E1
avrdude.exe: safemode: hfuse reads as D9
avrdude.exe: safemode: Fuses OK
Last edited by a moderator: