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()
    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:


while (1) {
    if (!resetclock) {
    rtc_get_date(&dow, &day, &month, &year);
    sprintf(date, "%d %04d/%02d/%02d", dow, year, month, day);
    lcd_gotoxy(0, 0);
    rtc_get_time(&hour, &min, &sec);
    sprintf(time, "%02d:%02d:%02d", hour, min, sec);
    lcd_gotoxy(5, 2);

The Circuit in Proteus

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);
  • 4
    \$\begingroup\$ Would you please explain what does it do then if it does not do what you expect? Besides a lot of the code is missing, so we can't know how you initialize it. The chip can't store year as 2002 either, it can only store two digits like 02 so maybe that is your issue? You are also not using a 16-bit integer to store the year either, only an 8-bit char. \$\endgroup\$
    – Justme
    Jun 1, 2021 at 19:23
  • 1
    \$\begingroup\$ What RTC library are you using? Would help to see the function prototypes... \$\endgroup\$
    – Ron Beyer
    Jun 1, 2021 at 20:10
  • \$\begingroup\$ The ds1307 library \$\endgroup\$
    – Hitman2847
    Jun 1, 2021 at 20:29
  • \$\begingroup\$ It does not print 2002 . Well that might be the problem . I can try using an int to see if the result changes. As for the code. The rtc was initialized by the default initialization function . Basically run the wizard in code vision and enable i2c then ds1307 on portC and enable alphanumeric lcd on portA and you would have the rest of the code that i have not included . Since it was the default code generated by codevision \$\endgroup\$
    – Hitman2847
    Jun 1, 2021 at 20:30

1 Answer 1


You should have said in the first post that you use a paid compiler and libraries that come with it.

The problem is solved by downloading and reading the compiler manual.

The manual documents that the date setting and getting functions consider year as char, so you can't initialize it to 2002 and it won't read back as 2002.

The manual also happens to provide an example project that initializes time and date and prints it on LCD, from where it is also obvious how to use the years.

The wrong usage of the functions should have caused compiler warnings so either you don't have the relevant compiler warnings enabled or chose to ignore them. You might want to let the compiler warn you when it sees that something is not right and fix the warnings.

  • \$\begingroup\$ I did tag Codevision. And as far as the char type i used a char that had 2002 as the value. I used the same method for month and day and it worked. Also the compiler warnings were only a couple referring to unused functions and variables. There are no warnings. And as it is seen every variable was initialized as char so it seems you did not read my code completely. Also the libraries are available online for free as they are avr libraries not codevision libraries. The only part of your answer that might make sense is the example. I will look it up but as I said before \$\endgroup\$
    – Hitman2847
    Jun 2, 2021 at 7:35
  • \$\begingroup\$ Also the results were the same when I put 2002 in a char variable (char year) and passed that on to the rtc_set_date function \$\endgroup\$
    – Hitman2847
    Jun 2, 2021 at 7:52
  • \$\begingroup\$ also as far as I have seen working examples of the function , passing int to this function which accepts char as parameters was done and the program was working just fine , as there is a implementation of the clock using this function and chip that has passed int to the function , so that is not the problem, also compiler did not send warnings or errors in regard to this \$\endgroup\$
    – Hitman2847
    Jun 2, 2021 at 7:57
  • \$\begingroup\$ and I looked the example folders of the codevision , none of what I had and looked into was about clocks or RTC or even contained the rtc_set_date() function \$\endgroup\$
    – Hitman2847
    Jun 2, 2021 at 8:36
  • 1
    \$\begingroup\$ The char type can store only 8-bit values ranging from 0 to 255. You can't store numbers like 570 or 1970 into a char as they are cut to 8-bits values of 58 (valid) or 178 (invalìd). You can write an invalid date or time values to RTC but the results are undefined like the datasheet says. Simplest solution, assume the year is 20XX and store XX into RTC. The RTC is intended to work up to year 2099 only after which it fails with leap year calculations. There's plenty of time for that to happen. \$\endgroup\$
    – Justme
    Jun 2, 2021 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.