Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help with RTC DS1307

Status
Not open for further replies.
Joined
Jul 25, 2012
Messages
1,192
Helped
171
Reputation
342
Reaction score
162
Trophy points
1,343
Activity points
0
Hello!

Please check my project and tell me why it displays date and time only when I2C debugger is connected in proteus. Also I want to know why the time is not running. The time doesn't change on lcd.

Thanks
Jayanth D
 

Attachments

  • 4550.rar
    98.8 KB · Views: 87

Do you have pull-ups on your bus?

Not everyone has RAR deciphering programs. They don't exist for some devices. Can you just post a jpg?
 

This is the problem with proteus..
i am having the same problem few days back..

But everything works fine in real hardware..
 

@Sky_123

Here are the files.

rename the file dataloggerpic18f4550.rar to dataloggerpic18f4550.dsn

rename the file 4550 rtc.rar to 4550 rtc.hex

Here is the mikroC code.

Code:
#ifndef DS1307
        #define DS1307 0xD0
#endif

// LCD module connections
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at LATD1_bit;
sbit LCD_D4 at LATD2_bit;
sbit LCD_D5 at LATD3_bit;
sbit LCD_D6 at LATD4_bit;
sbit LCD_D7 at LATD5_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End LCD module connections

double temp;
char sTemp[23];

char colon[] = ":";

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

void Zero_Fill(char *value) {
        if (value[1] == 0) {
                value[1] = value[0];
                value[0] = 48;
                value[2] = 0;
        }
}

void Write_Time() {
        I2C1_Start();          // issue start signal
        I2C1_Wr(DS1307);       // address DS1307
        I2C1_Wr(0);            // start from word at address (REG0)
        I2C1_Wr(0x80);         // write $80 to REG0. (pause counter + 0 sec)
        I2C1_Wr(0);            // write 0 to minutes word to (REG1)
        I2C1_Wr(0x17);         // write 17 to hours word (24-hours mode)(REG2)
        I2C1_Wr(0x02);         // write 2 - Monday (REG3)
        I2C1_Wr(0x04);         // write 4 to date word (REG4)
        I2C1_Wr(0x05);         // write 5 (May) to month word (REG5)
        I2C1_Wr(0x01);         // write 01 to year word (REG6)
        I2C1_Stop();           // issue stop signal

        I2C1_Start();          // issue start signal
        I2C1_Wr(0xD0);       // address DS1307
        I2C1_Wr(0);            // start from word at address 0
        I2C1_Wr(0);            // write 0 to REG0 (enable counting + 0 sec)
        I2C1_Stop();           // issue stop signal
}

void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
        I2C1_Start();
        I2C1_Wr(DS1307);
        I2C1_Wr(0);
        I2C1_Repeated_Start();
        I2C1_Wr(0xD1);
        *sec =I2C1_Rd(2);
        *min =I2C1_Rd(2);
        *hr =I2C1_Rd(2);
        *week_day =I2C1_Rd(2);
        *day =I2C1_Rd(2);
        *mn =I2C1_Rd(2);
        *year =I2C1_Rd(2);
        I2C1_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);
}

void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
        Lcd_Out(1,1,"Date:");
        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_Out(1,7,txt);
        Lcd_Chr(1,11,(day / 10)   + 48);    // Print tens digit of day variable
        Lcd_Chr(1,12, (day % 10)   + 48);    // Print oness digit of day variable
        Lcd_Chr(1,13,'/');
        Lcd_Chr(1,14,(mn / 10) + 48);
        Lcd_Chr(1,15,(mn % 10) + 48);
        Lcd_Chr(1,16,'/');
        Lcd_Out(1,17,"2");
        Lcd_Out(1,18,"0");
        Lcd_Chr(1,19, (year / 10)  + 48);          // Print year vaiable + 8 (start from year 2008)
        Lcd_Chr(1,20, (year % 10)  + 48);

        if(hr > 12) {
           Lcd_Out(2,10,"PM");
        }
        else if(hr <= 12) {
           Lcd_Out(2,10,"AM");
        }
        
        Lcd_Chr(2,13,(hr / 10)  + 48);
        Lcd_Chr(2,14,(hr % 10)  + 48);
        Lcd_Out(2,15,":");
        Lcd_Chr(2,16,(min / 10) + 48);
        Lcd_Chr(2,17,(min % 10) + 48);
        Lcd_Out(2,18,":");
        Lcd_Chr(2,19,(sec / 10) + 48);
        Lcd_Chr(2,20,(sec % 10) + 48);
}

void main() {
     TRISA = 0b00000011;
     PORTA = 0x00;
     TRISB = 0b00000011;
     PORTB = 0x00;
     LATB = 0x00;
     TRISD = 0b00000000;
     PORTD = 0xFF;
     LATD = 0x00;
     ADCON0 = 0b00000000;
     ADCON1 = 0b00001101;
     CMCON = 0x07;

     Lcd_Init();
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Cmd(_LCD_CURSOR_OFF);
     Delay_ms(100);

     Lcd_Out(1,1,"RTC And Temperature");
     Lcd_Out(2,1,"Data Logger");
     Delay_ms(2000);
     Lcd_Cmd(_LCD_CLEAR);
     ADC_Init();
     I2C1_Init(100000);

     txt = "Time:";
     Lcd_Out(2,1,txt);
     Lcd_Out(3,1,"Temperature:");

     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);

         temp = ADC_Read(0);
         temp = temp * 0.39682550;    //18056883083831503659128
         FloatToStr(temp, sTemp);
         Lcd_Out(3,14,sTemp);
     }
}

@arunsharma

OK. I will try on real hardware.

------------Update----------------

OK. I changed the code a little and now the time runs.

Here is the code.

Code:
#ifndef DS1307
        #define DS1307 0xD0
#endif

// LCD module connections
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at LATD1_bit;
sbit LCD_D4 at LATD2_bit;
sbit LCD_D5 at LATD3_bit;
sbit LCD_D6 at LATD4_bit;
sbit LCD_D7 at LATD5_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End LCD module connections

double temp;
char sTemp[23];

char colon[] = ":";

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

void Zero_Fill(char *value) {
        if (value[1] == 0) {
                value[1] = value[0];
                value[0] = 48;
                value[2] = 0;
        }
}

void Write_Time() {
        I2C1_Start();          // issue start signal
        I2C1_Wr(DS1307);       // address DS1307
        I2C1_Wr(0);            // start from word at address (REG0)
        I2C1_Wr(0x80);         // write $80 to REG0. (pause counter + 0 sec)
        I2C1_Wr(0);            // write 0 to minutes word to (REG1)
        I2C1_Wr(0x17);         // write 17 to hours word (24-hours mode)(REG2)
        I2C1_Wr(0x02);         // write 2 - Monday (REG3)
        I2C1_Wr(0x04);         // write 4 to date word (REG4)
        I2C1_Wr(0x05);         // write 5 (May) to month word (REG5)
        I2C1_Wr(0x01);         // write 01 to year word (REG6)
        I2C1_Stop();           // issue stop signal

        I2C1_Start();          // issue start signal
        I2C1_Wr(0xD0);       // address DS1307
        I2C1_Wr(0);            // start from word at address 0
        I2C1_Wr(0);            // write 0 to REG0 (enable counting + 0 sec)
        I2C1_Stop();           // issue stop signal
}

void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
        I2C1_Start();
        I2C1_Wr(DS1307);
        I2C1_Wr(0);
        I2C1_Repeated_Start();
        I2C1_Wr(0xD1);
        *sec =I2C1_Rd(1);
        *min =I2C1_Rd(1);
        *hr =I2C1_Rd(1);
        *week_day =I2C1_Rd(1);
        *day =I2C1_Rd(1);
        *mn =I2C1_Rd(1);
        *year =I2C1_Rd(0);
        I2C1_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);
}

void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
        Lcd_Out(1,1,"Date:");
        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_Out(1,7,txt);
        Lcd_Chr(1,11,(day / 10)   + 48);    // Print tens digit of day variable
        Lcd_Chr(1,12, (day % 10)   + 48);    // Print oness digit of day variable
        Lcd_Chr(1,13,'/');
        Lcd_Chr(1,14,(mn / 10) + 48);
        Lcd_Chr(1,15,(mn % 10) + 48);
        Lcd_Chr(1,16,'/');
        Lcd_Out(1,17,"2");
        Lcd_Out(1,18,"0");
        Lcd_Chr(1,19, (year / 10)  + 48);          // Print year vaiable + 8 (start from year 2008)
        Lcd_Chr(1,20, (year % 10)  + 48);

        if(hr > 12) {
           Lcd_Out(2,10,"PM");
        }
        else if(hr <= 12) {
           Lcd_Out(2,10,"AM");
        }
        
        Lcd_Chr(2,13,(hr / 10)  + 48);
        Lcd_Chr(2,14,(hr % 10)  + 48);
        Lcd_Out(2,15,":");
        Lcd_Chr(2,16,(min / 10) + 48);
        Lcd_Chr(2,17,(min % 10) + 48);
        Lcd_Out(2,18,":");
        Lcd_Chr(2,19,(sec / 10) + 48);
        Lcd_Chr(2,20,(sec % 10) + 48);
}

void main() {
     TRISA = 0b00000011;
     PORTA = 0x00;
     //TRISB = 0b00000011;
     //PORTB = 0x00;
     //LATB = 0x00;
     TRISD = 0b00000000;
     PORTD = 0xFF;
     LATD = 0x00;
     ADCON0 = 0b00000000;
     ADCON1 = 0b00001101;
     CMCON = 0x07;

     Lcd_Init();
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Cmd(_LCD_CURSOR_OFF);
     Delay_ms(100);

     Lcd_Out(1,1,"RTC And Temperature");
     Lcd_Out(2,1,"Data Logger");
     Delay_ms(2000);
     Lcd_Cmd(_LCD_CLEAR);
     ADC_Init();
     I2C1_Init(100000);

     txt = "Time:";
     Lcd_Out(2,1,txt);
     Lcd_Out(3,1,"Temperature:");

     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);
         temp = ADC_Read(0);
         temp = temp * 0.39682550;    //18056883083831503659128
         FloatToStr(temp, sTemp);
         Lcd_Out(3,14,sTemp);
     }
}

I want to know why it doesn't display date and time if I2C debugger is not connected in proteus. I saw a PIC16F877A project which displays the date and time even if the I2C debugger is not connected in proteus. Please help me with the issue.

RTC.rar runs without I2C debugger. rename MyProject.rar to MyProject.hex and RTC.rar to RTC.dsn
 

Attachments

  • 4550 rtc.jpg
    4550 rtc.jpg
    353.6 KB · Views: 105
  • dataloggerpic18f4550.rar
    152.2 KB · Views: 88
  • 4550 rtc.rar
    13.2 KB · Views: 89
  • RTC.rar
    75.4 KB · Views: 83
  • MyProject.rar
    6.3 KB · Views: 91
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top