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.

[PIC] random values from DS1307 to PIC uC during Proteus simulation

Status
Not open for further replies.

MrSingh

Newbie level 5
Joined
Mar 21, 2014
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
69
Hi i am interfacing PIC18f4550 with DS1307 RTC .Firstly it was showing Correct values on Proteus Simulation however after few minutes it showed a random value then switched back to correct values now after some time it again showed random value without switching back to correct one.
when the display was showing random value the data in clock panel was 155-13-51 sumthing like this
here is my code
Code:
        #define DS1307 0xD0

//end definition
//Begin Pin assignment

 //i2c pin assignment
sbit Soft_I2C_Scl           at RB3_bit;
sbit Soft_I2C_Sda           at RB4_bit;
sbit Soft_I2C_Scl_Direction at TRISB3_bit;
sbit Soft_I2C_Sda_Direction at TRISB4_bit;
 

 // LCD pin connection
sbit LCD_RS at RE0_bit;
sbit LCD_EN at RE2_bit;
sbit LCD_D0 at RD0_bit;
sbit LCD_D1 at RD1_bit;
sbit LCD_D2 at RD2_bit;
sbit LCD_D3 at RD3_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;
// LCD  Pin direction
sbit LCD_RS_Direction at TRISE0_bit;
sbit LCD_EN_Direction at TRISE2_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D3_Direction at TRISD3_bit;
sbit LCD_D2_Direction at TRISD2_bit;
sbit LCD_D1_Direction at TRISD1_bit;
sbit LCD_D0_Direction at TRISD0_bit;
//End Pin assignment
//Start variable declaration

char colon[] = ":";
char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];
unsigned short myDate,myHour,myMin,myYear,myMon,myDay;

//End variable declaration

//Start PRogram
   //LCD library
void ibusy()   //lcd busy function check
{
TRISD=0xFF;
   LATE2_bit=1;
   LATE0_bit=0;
      LATE1_bit=1;
      while(LCD_D7)
      {
          LATE2_bit=0;

    //Delay_ms(50);
        LATE2_bit=1;
      }
      TRISD=0x00;
}

void lcmd(char xy)    //lcd command o/p
 {     ibusy();
  LATD=xy;
      LATE0_bit=0;
      LATE1_bit=0;
       LATE2_bit=1;

 Delay_ms(10);
        LATE2_bit=0;
     Delay_ms(10);
    // Delay_ms(100);
 }
  void ldata(char xy)       //lcd data o/p
 {   ibusy();
 LATD=xy;
      LATE0_bit=1;
      LATE1_bit=0;
       LATE2_bit=1;

  Delay_ms(10);      //     orig20ms
        LATE2_bit=0;
     Delay_ms(10);       //orig 40ms
  // Delay_ms(100);
 }

 void lcdinit()             // not used initialization sequence
  {    Delay_ms(10000);
  lcmd(0x38);  //
  Delay_ms(1000);
lcmd (0x0C);     //
 Delay_ms(1000);
lcmd(0x01);        //
Delay_ms(1000);
lcmd(0x06);       //
 Delay_ms(1000);
lcmd(0x80);     //
 Delay_ms(1000);
 }
   void icmd(char xy)     // used 4 linitailization commands
 {     //ibusy();
  LATD=xy;
      LATE0_bit=0;
      LATE1_bit=0;
       LATE2_bit=1;

 Delay_ms(20);
        LATE2_bit=0;
     Delay_ms(40);
    // Delay_ms(100);
 }
 void init()           //lcd initialization code
  {   //
   Delay_ms(10000); // comment for testing
  icmd(0x0F);  //
  Delay_ms(1000);
  icmd(0x38);  //  is necessary 4 2line operation
  Delay_ms(1000);
   /* icmd(0x38);  //
  Delay_ms(1000);
lcmd (0x08);     //
 Delay_ms(1000);
lcmd(0x01);        //
Delay_ms(1000);
lcmd(0x06);       //
 Delay_ms(1000);
lcmd(0x80);     //
 Delay_ms(1000);*/
 }
 void lcursor(char row, char column)     // set cursor position
{
        switch (row) {
                case 1: lcmd(0x80 + column - 1); break;
                case 2: lcmd(0xc0 + column - 1); break;
                default: break;
        }
}
 void lout(char row, char column, char *text)     // o/p a string of data
 {    lcursor(row,column)  ;
        while(*text)                  // Write data to LCD up to null
        {    // Wait while LCD is busy
                ldata(*text); // Write character to LCD
                text++;        // Increment buffer

        }
        return;
 }
 void lchr(char row, char column, char text)   //o/p a letter
 {    lcursor(row,column)  ;
                ldata(text); // Write character to LCD

        return;
 }
//All functions related to RTC interfacing with MCU using sw-i2c
void Write_Time() {
     Delay_ms(2000);
        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(0x80);         // write $80 to REG0. (pause counter + 0 sec)
        Soft_I2C_Write(myMin);            // write 0 to minutes word to (REG1)
        Soft_I2C_Write(myHour);         // write 17 to hours word (24-hours mode)(REG2)
        Soft_I2C_Write(myDay);         // write 2 - Monday (REG3)
     Soft_I2C_Write(myDate);         // write 4 to date word (REG4)
        Soft_I2C_Write(myMon);         // write 5 (May) to month word (REG5)
        Soft_I2C_Write(myYear);         // write 01 to year word (REG6)
        Soft_I2C_Stop();           // issue stop signal
          Delay_ms(2000);
        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 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(0);
        Soft_I2C_Start();
        Soft_I2C_Write(0xD1);
        *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);
}

void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
        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;
        }

        /*init();
        lcmd(0x01);               // Clear display
        lcmd(0x0C);          // Cursor off*/



        lchr(1, 1, (hr / 10)   + 48);
       
        lchr(1, 2, (hr % 10)   + 48);
     
        lchr(1,3,':')  ;
      
        lchr(1, 4, (min / 10) + 48);
   
        lchr(1,5, (min % 10) + 48);
     
        lchr(1,6,':')  ;

        lchr(1,7, (sec / 10) + 48);
    
        lchr(1,8, (sec % 10) + 48);
      //
         //Lcd_Out(1,1,txt);
    
        lchr(1, 10, (day / 10)   + 48);  
      // Print tens digit of day variable
        lchr(1, 11, (day % 10)   + 48);  
     // Print oness digit of day variable
        lchr(1,12,'/')  ;
    
     //  Lcd_Chr(1,13, (mn / 10) + 48);
   
        lchr(1,13, (mn % 10) + 48);
 
        lchr(1,14,'/')  ;
     
        lchr(1,15,  (year / 10)  + 48);    
         // Print year vaiable
       lchr(1,16,  (year % 10)  + 48);


}
void Set_Time()
{
              myDate=0x27;
                myMon=0x04;
                   myYear=0x14;
              myHour=0x12;
              myMin=0x55;
              myDay= 0x01;
              Write_Time();
}
    void  WhatIsTime()
    {
          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);

    }

void main()   //Main program flow
 {     

 
 

 
    ADCON1 = 0x0F; // page 268, disable analaog
   ///  CMCON = 0x07;            //originallly uncommented
        INTCON2 = 0x80; // disable pull up in port b

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

        // Make all outputs
        TRISA = 0x00;
       TRISB = 0x00;
        TRISC = 0x00;
       TRISD = 0x00;
       TRISE = 0x00;
              PORTC=0x33;
              TRISC=0x33;

        init();                // Initialize LCD
        lcmd(0x01);       // Clear LCD display
        lcmd(0x0C);  // Turn cursor off

        Soft_I2C_Init();                        // initialize I2C
          lout(1,1,"Software I2C");lout(2,1,"Initialized") ;  
           Delay_ms(1000);
           lcmd(0x01);       // Clear LCD display
        lcmd(0x0C);  // Turn cursor off
      lout(1,1,"Clock using");lout(2,1,"RTC") ;  
       Delay_ms(1000);
               lcmd(0x01);       // Clear LCD display
        lcmd(0x0C);  // Turn cursor off
   
    
    
    
    
    
    
    
    
    
    
    while(1)

    {   
     WhatIsTime();          // Answers the question asked by the MCU
      

    }

}
my circuit diagram is ckt.jpg
also the values of RTC
1.PNG12.PNG
 

You are not pulling up I2C bus directly to VCC, but through 10K resistor of MCLR circuit.
 

I corrected that but the ckt behaves same with random o/p values,
What is the problem ? is it in hardware(connections) or Software(The code)?
 

Hi i made changes as suggested by andre_teprom but still the behaviour of circuit simulation has not changed
 

Which version of mikroC are you using? If mikroC PRO then why don't you use 4-bit LCD library?

See if these changes help.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//i2c pin assignment
sbit Soft_I2C_Scl           at LATB3_bit;
sbit Soft_I2C_Sda           at LATB4_bit;
 
 
// LCD pin connection
sbit LCD_RS at LATE0_bit;
sbit LCD_EN at LATE2_bit;
sbit LCD_D0 at LATD0_bit;
sbit LCD_D1 at LATD1_bit;
sbit LCD_D2 at LATD2_bit;
sbit LCD_D3 at LATD3_bit;
sbit LCD_D7 at LATD7_bit;
sbit LCD_D6 at LATD6_bit;
sbit LCD_D5 at LATD5_bit;
sbit LCD_D4 at LATD4_bit;




Edit:

Add this before while(1) loop


Code C - [expand]
1
Soft_I2C_Sda_Direction = 1;

 
Last edited:

i am using mikroC PRO for PIC v 5.61
btw i implemented this
Code:
Soft_I2C_Sda_Direction = 1;
& again the o/p is same
i am uploading my project file so that you can understand my problem which is the rtc gives normal values when started but after 15 minutes it gives random values as you an see in images of the first post

View attachment rtc p roblem.zip
 

See if attached code works.
 

Attachments

  • RTC DS1307.rar
    75.6 KB · Views: 46

Have you tried this in real hardware ?
I found this out with Proteus spent ages debugging then thout I'd sort that bit out later but when I tried it in real time it worked perfect and its never played up since but it still does in Proteus well that's what I found
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top