I'm building a clock/calendar with an ATmega, a DS1307, and a 20*4 LCD (LM044L).
I have implemented the code showing the time correctly, I have initialized the RTC too, but for some reason when I try to print the date, the year is scrambled up into some nonsense number, although the day and month are shown correctly.
I have written 2 functions to give date and time a default value for the time being. I will put the parts of the code that I wrote, and also the circuit I have built in Proteus. Ignore the buttons and the keypad as I have not yet implemented any code for them.
PS. Please help me understand why this happened, I am not looking for suggestion to use other pieces of hardware, or changing the code to a whole other method of implementation, I'm trying to get what is going on with my code.
char year=0, month=0, day=0, dow=0; // dow=day of week
char date[];
char time[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, hour=0, min=0, sec=0;
void reset_clock(void);
void time_defaultinit();
void date_defaultinit();
void time_defaultinit()
{
hour=15;
min=18;
sec=55;
rtc_set_time(hour, min, sec);
}
void date_defaultinit()
{
rtc_set_date(3, 17, 05, 2002);
}
The part below is what is included in the main function:
time_defaultinit();
date_defaultinit();
while (1) {
if (!resetclock) {
reset_clock();
}
lcd_clear();
rtc_get_date(&dow, &day, &month, &year);
sprintf(date, "%d %04d/%02d/%02d", dow, year, month, day);
lcd_gotoxy(0, 0);
lcd_puts(date);
rtc_get_time(&hour, &min, &sec);
sprintf(time, "%02d:%02d:%02d", hour, min, sec);
lcd_gotoxy(5, 2);
lcd_puts(time);
delay_ms(850);
}
EDIT: The results were the same back when I used char variables for the manual initialization of the rtc_set_date
function as below:
void date_defaultinit()
{
year=2002; //char year
month=5; //char month
day=17; //char day
dow=3; //char dow
rtc_set_date(dow, day, month, year);
}