loading

why does the lcd shows the old value after I make the power off ?

Hello,
my project is a Real Time Digital Clock

I'm using LCD 16x 2 with DS1307 connected with pic18f4550
when i burn the code to the circuit it shows the time correctly but when i make the power off then make it ON it shows the previous code which is not correctly !

How can i fix this ? shall i use battery for the DS1307 ?
what do you suggest from me ?

this is the code written in mikroc  :

#ifndef DS1307
#define DS1307 0xD0
#endif

// Software I2C connections
sbit Soft_I2C_Scl at RB1_bit;
sbit Soft_I2C_Sda at RB0_bit;
sbit Soft_I2C_Scl_Direction at TRISB1_bit;
sbit Soft_I2C_Sda_Direction at TRISB0_bit;
// End Software I2C connections
sbit LCD_RS at RB3_bit;
sbit LCD_RW at RD0_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RD1_bit;
sbit LCD_D5 at RD2_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD4_bit;

sbit LCD_RS_Direction at TRISB3_bit;
sbit LCD_RW_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISD1_bit;
sbit LCD_D5_Direction at TRISD2_bit;
sbit LCD_D6_Direction at TRISD3_bit;
sbit LCD_D7_Direction at TRISD4_bit;

char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];

// for some other testing
char txtSec[10];
char txtMin[10];
char txtHour[10];
char txtWeekDay[10];
char txtDay[10];
char txtMn[10];
char txtYear[5];
char txtDisplay[39];

// for some testing
int intSec, intMin, intHour, intWeekDay, intMn, intDay, intYear;



void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
        Soft_I2C_Start();
        Soft_I2C_Write(DS1307);
        Soft_I2C_Write(0x00);

        Soft_I2C_Start();
        Soft_I2C_Write(0xD1);     //MAKE SURE D1 IS ADDRSS OF PIC18F4550
        *sec =Soft_I2C_Read(1);
        *min =Soft_I2C_Read(1);
        *hr =Soft_I2C_Read(1);
        *week_day =Soft_I2C_Read(1);
        *day =Soft_I2C_Read(1);
        *mn =Soft_I2C_Read(1);
        *year =Soft_I2C_Read(0);
        Soft_I2C_Stop();
}
/*
void Transform_Time(char  *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
        *sec  =  ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
        *min  =  ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
        *hr   =  ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
        *week_day =(*week_day & 0x07);
        *day  =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
        *mn   =  ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
        *year =  ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}  **/
//-------------------- Formats date and time
void Transform_Time() {
  *sec  =  ((*sec & 0xF0) >> 4)*10 + (*sec & 0x0F);  // Transform seconds
  *min  =  ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);  // Transform months
*hr    =  ((*hr & 0xF0)  >> 4)*10  + (*hr & 0x0F);    // Transform hours
  *year    =   (*year & 0xC0) >> 6;                             // Transform year
  *day     =  ((*day  & 0x30) >> 4)*10    + (*day  & 0x0F);       // Transform day
*mn   =  ((*mn & 0x10)  >> 4)*10 + (*mn & 0x0F);     // Transform month
}


void Write_Time() {
        Soft_I2C_Start();          // issue start signal
        Soft_I2C_Write(DS1307);       // address DS1307
        Soft_I2C_Write(0);            // start from word at address (REG0)
        Soft_I2C_Write(0x40);         // write 17 to hours word (24-hours mode)(REG2) (sec)*
        Soft_I2C_Write(0x23);         // write 2 - Monday (REG3)   ,( min ) *
        Soft_I2C_Write(0x14);         // write 4 to date word (REG4)  (hr) *
        Soft_I2C_Write(0x03);         // write 5 (May) to month word (REG5) (day)*
        Soft_I2C_Write(0x14);         // write 01 to year word (REG6) (year)*

        Soft_I2C_Write(0x05);
        Soft_I2C_Write(0x13);
        Soft_I2C_Stop();           // issue stop signal

        Soft_I2C_Start();          // issue start signal
        Soft_I2C_Write(DS1307);       // address DS1307
        Soft_I2C_Write(0);            // start from word at address 0
        Soft_I2C_Write(0);            // write 0 to REG0 (enable counting + 0 sec)
        Soft_I2C_Stop();          // issue stop signal
}

void Transform_Time(char  *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
        *sec  =  ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
        *min  =  ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
        *hr   =  ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
        *week_day =(*week_day & 0x07);
        *day  =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
        *mn   =  ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
        *year =  ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
/*
void Display_Time() {
        switch(week_day)
        {
                case 1: txt="Sun"; break;
                case 2: txt="Mon"; break;
                case 3: txt="Tue"; break;
                case 4: txt="Wed"; break;
                case 5: txt="Thu"; break;
                case 6: txt="Fri"; break;
                case 7: txt="Sat"; break;
        }   **/
        Lcd_Chr(1, 6, (day / 10)   + 48);    // Print tens digit of day variable
   Lcd_Chr(1, 7, (*day % 10)   + 48);    // Print oness digit of day variable
   Lcd_Chr(1, 9, (*mn / 10) + 48);
   Lcd_Chr(1,10, (month % 10) + 48);
   Lcd_Chr(1,15,  year        + 48);    // Print year variable  (start from year 2010)


   Lcd_Chr(2, 6, (hours / 10)   + 48);
   Lcd_Chr(2, 7, (hours % 10)   + 48);
   Lcd_Chr(2, 9, (minutes / 10) + 48);
   Lcd_Chr(2,10, (minutes % 10) + 48);
   Lcd_Chr(2,12, (seconds / 10) + 48);
   Lcd_Chr(2,13, (seconds % 10) + 48);
}


        Lcd_Init();
        Lcd_Cmd(_LCD_CLEAR);               // Clear display
        Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

        Lcd_Out(1,1,txt);
        Lcd_Chr(1, 6, (day / 10)   + 48);    // Print tens digit of day variable
        Lcd_Chr(1, 7, (day % 10)   + 48);    // Print oness digit of day variable
        Lcd_Chr(1,8,'.');
        Lcd_Chr(1, 9, (mn / 10) + 48);
        Lcd_Chr(1,10, (mn % 10) + 48);
        Lcd_Chr(1,11,'.');
        Lcd_Chr(1,12,  (year / 10)  + 48);          // Print year vaiable + 8 (start from year 2008)
        Lcd_Chr(1,13,  (year % 10)  + 48);

        txt = "Time";
        Lcd_Out(2,1,txt);
        Lcd_Chr(2, 6, (hr / 10)   + 48);
        Lcd_Chr(2, 7, (hr % 10)   + 48);
        Lcd_Chr(2,8,':');
        Lcd_Chr(2, 9, (min / 10) + 48);
        Lcd_Chr(2,10, (min % 10) + 48);
        Lcd_Chr(2,11,':');
        Lcd_Chr(2,12, (sec / 10) + 48);
        Lcd_Chr(2,13, (sec % 10) + 48);

        Lcd_Cmd(_LCD_CURSOR_OFF);
}
void Init_Main_Simple() {
        ADCON1 = 0x0F; // page 268, disable analaog
       // CMCON = 0x07;
       // INTCON2 = 0x80; // disable pull up in port b

        // clears internal latches
        LATB = 0x03; // enable internal pull ups
        LATA = 0x00;
        LATC = 0x00;
        LATD = 0x00;
        LATE = 0x00;

        // Make all outputs
        TRISA = 0x00;
        TRISB = 0x03;
        TRISC = 0x00;
        TRISD = 0x00;

        TRISE = 0x00;

        Lcd_Init();                // Initialize LCD
        Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display
        Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off

        Soft_I2C_Init();                        // initialize I2C
        Lcd_Out(1,1,"Time & Date");
}
void main() {
           Delay_ms(500);
       Init_Main_Simple();
       //Write_Time();
        //Delay_ms(100);



        while (1) {
                Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);      // read time from RTC(DS1307)
                Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // format date and time
                Display_Time(sec, min1, hr, week_day, day, mn, year);
                Delay_ms(1000);

        }
}



Thanks

Beyond having it reference an RTC did you really expect us to follow that much code?

RTC a real time clock, you do know your circuit is going to display the last remembered time right.

An RTC tells the clock the time has changed.
+1
iceng4 years ago
Use a low FWD voltage schottky diode and a 0.047 Farad capacitor
for 1 Week of memory, or a half farad for over a Month of retention.

A
iceng iceng4 years ago
And some more data for you.