Help! Clock not working.....

Thread Starter

PIYUSH SONI

Joined Nov 15, 2013
32
Hello everyone.
I am working with ds1307 RTC and pic 18f4520 mcu.
Compiler - MikroC Pro for PIC. Oscillator - 20 MHz & 32kHz(for driving RTC).
My clock is not working & also not displaying given time & date value.
It's showing some random values & sometimes week is also displaying but not everytime. Also "seconds" value doesn't increments & i have also connected battery for RTC. I have attached my code , Kindly Help me.:)


Rich (BB code):
// Lcd module connections
sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB2_bit;
sbit LCD_D5 at LATB3_bit;
sbit LCD_D6 at LATB4_bit;
sbit LCD_D7 at LATB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;

// Lcd constants
char txt1[] = "Hellow";
char txt2[] = "RTC2 click";
char txt3[] = "DATE:";
char txt4[] = "TIME:";

// Module variables
unsigned char hour, minute, second, days, weeks, months, years,hours, minutes, seconds,day,week, month, year,*txt;    // Global date/time variables
char oldstate = 0;



// RTC Definitions
#define RTC_ADDR  0xD0

/**************************************************************************************************
* DS1307 Functions
**************************************************************************************************/

/**************************************************************************************************
* Read data from RTC DS1307
* input : addres of RTC register
* output: value of of RTC register
**************************************************************************************************/
unsigned char RTC_Read(unsigned char addr){
  unsigned char value;

  I2C1_Start();                   // Issue start signal
  I2C1_Wr(RTC_ADDR);              // Address DS1307, see DS1307 datasheet
  I2C1_Wr(addr);                  // Start from address
  I2C1_Start();                   // Issue repeated start signal
  I2C1_Wr(RTC_ADDR);              // Address DS1307 for reading R/W=1

  value = I2C1_Rd(0);             // Read seconds byte
  I2C1_Stop();                    // Issue stop signal

  return value;
}

/**************************************************************************************************
* Write data from DS1307
* input : addres of RTC register, value of of RTC register
**************************************************************************************************/
void RTC_Write(unsigned char addr, unsigned char value){
  I2C1_Start();         // Issue start signal
  I2C1_Wr(RTC_ADDR);    // Address DS1307
  I2C1_Wr(addr);        // Start from address
  I2C1_Wr(value);       // Write value to RTC register
  I2C1_Stop();          // Issue stop signal
}

/**************************************************************************************************
* Read time from RTC DS1307
* input : pointer to variables where RTC data will be stored
* output: variables with RTC data
**************************************************************************************************/
void Read_Time(char *seconds,char *minutes,char *hours,char *week,char *day,char *month,char *year){
  I2C1_Start();                // Issue start signal
  I2C1_Wr(RTC_ADDR);           // Address DS1307, see DS1307 datasheet
  I2C1_Wr(2);                  // Start from address 0
  I2C1_Repeated_Start();       // Issue repeated start signal
  I2C1_Wr(0xD1);       // Address DS1307 for reading R/W=1

  *seconds = I2C1_Rd(1);     // Read seconds byte
  *minutes = I2C1_Rd(1);     // Read minutes byte
  *hours = I2C1_Rd(1);       // Read hours byte
  *week =I2C1_Rd(1);
  *day =I2C1_Rd(1);
  *month =I2C1_Rd(1);
  *year =I2C1_Rd(0);

  I2C1_Stop();                 // Issue stop signal
}

/**************************************************************************************************
* Write time to RTC DS1307
* input : variables with RTC data
**************************************************************************************************/
void Write_Time(unsigned char c_hours, unsigned char c_minutes, unsigned char c_seconds,
                unsigned char c_day, unsigned char c_week, unsigned char c_month, unsigned char c_year){
   I2C1_Start();                     // issue start signal
   I2C1_Wr(RTC_ADDR);                // address DS1307
   I2C1_Wr(2);                       // start from word at address (REG0)
   I2C1_Wr(0x80);                    // write $80 to REG0. (pause counter + 0 sec)

   I2C1_Wr(c_minutes);               // write 0 to minutes word to (REG1)
   I2C1_Wr(c_hours);                 // write 17 to hours word (24-hours mode)(REG2)
   I2C1_Wr(c_week);                  // write 2 - Monday (REG3)
   I2C1_Wr(c_day);                   // write 4 to date word (REG4)
   I2C1_Wr(c_month);                 // write 5 (May) to month word (REG5)
   I2C1_Wr(c_year);                  // write 01 to year word (REG6)
   I2C1_Stop();                      // issue stop signal

   I2C1_Start();                     // issue start signal
   I2C1_Wr(RTC_ADDR);                // address DS1307
   I2C1_Wr(0);                       // start from word at address 0
   I2C1_Wr(0 | c_seconds);           // write 0 to REG0 (enable counting + 0 sec)
   I2C1_Stop();                      // issue stop signal
}
/**************************************************************************************************
* Show on the LCD display
* input : variables with RTC data
**************************************************************************************************/
void Transform_Time(char *seconds,char *minutes,char *hours,char *week,char *day,char *month,char *year){


  *seconds  =  ((*seconds & 0xF0) >> 4)*10 + (*seconds & 0x0F);
  *minutes  =  ((*minutes & 0xF0) >> 4)*10 + (*minutes & 0x0F);
  *hours    =  ((*hours & 0xF0) >> 4)*10 + (*hours & 0x0F);
  *week     =  (*week & 0x0F);
  *day      =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
  *month    =  ((*month & 0xF0) >> 4)*10 + (*month & 0x0F);
  *year     =  ((*year & 0xF0)>>4)*10+(*year & 0x0F);


  }
  void Show_Time()
  {
  switch(weeks)
  { 
      char *txt;
    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(2,14, 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, 9, (month / 10) + 48);
  Lcd_Chr(1,10, (month % 10) + 48);
  Lcd_Chr(1,14, (year / 10)  + 48);
  Lcd_Chr(1,15, (year % 10)  + 48);
  
  
  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);
}
 void Init_main()
 { 
 ADRESL = 0;
ADRESH = 0;
ADCON0=0;
ADCON1=0x0F;
ADCON2=0x00;
PORTC =0;
TRISC =0XFF;

  CMCON =7;
  PORTA = 0;

  TRISA2_bit = 1;

  I2C1_Init(100000);                 // Initialize I2C bus for communication with RTC
  Lcd_Init();                        // Initialize Lcd

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,4,txt2);                 // Write text in second row

  Delay_ms(2000);                    // delay 2 sec

  Lcd_Cmd(_LCD_CLEAR);               // Prepare and output static text on LCD
  LCD_Chr(1,8,'.');
  LCD_Chr(1,11,'.');
  LCD_Out(1,1,txt3);
  LCD_Out(2,1,txt4);
  LCD_Chr(2,8,':');
  LCD_Chr(2,11,':');
  LCD_Out(1,12,"20");
 }
/**************************************************************************************************
* MAIN PROGRAM
**************************************************************************************************/
void main()
{

    Delay_ms(200);
    Init_main();
    Write_Time(0x00,0x11,0x00,0x02,0x01,0x02,0x14);
     while(1)
     {
     Transform_Time(&seconds,&minutes,&hours,&week,&day,&month,&year);
     Show_Time();
  Read_Time(&seconds,&minutes,&hours,&week,&day,&month,&year);

     }
     
   }
 

ErnieM

Joined Apr 24, 2011
8,377
1. Fill seconds, minutes, hours, week, day, month, and year with dummy data you believe to be correct.

2. comment out the Read_Time() function in main().

3. ADD A DELAY OF 1 SECOND inside the very tight while(1) loop inside main().

4. Run modified program, come back and report results.
 

Thread Starter

PIYUSH SONI

Joined Nov 15, 2013
32
I am not getting your 1st point that you are telling about "Fill seconds, minutes, hours, week, day, month, and year with dummy data you believe to be correct".
I have commented Read_Time in main and also gave 1 second delay in while loop, but after doing this no value displays in time and date row. I am getting fuzzy at this, Help me.
 

tshuck

Joined Oct 18, 2012
3,534
He is suggesting that you attempt to verify the functionality of the rest of the system by processing known values (hard code the values fort second, minutes, hours, etc.) and attempt to display them before making the circuit more complicated (adding the RTC).
 

spinnaker

Joined Oct 29, 2009
7,830
I am not getting your 1st point that you are telling about "Fill seconds, minutes, hours, week, day, month, and year with dummy data you believe to be correct".
I have commented Read_Time in main and also gave 1 second delay in while loop, but after doing this no value displays in time and date row. I am getting fuzzy at this, Help me.
Set those values to some value and don't read the clock:


example:

seconds = 0;
minutes = 03;
hours = 1;
week = 1;
day = 1;
month = 10;
year = 2014;

I did not look close at your code, these might not be the exact variable but hopefull you get the idea.

What Ernie is trying to do is to help you determine if the issue is with reading the clock or just displaying the value. So just forget about the clock for now and just display something.
 

Thread Starter

PIYUSH SONI

Joined Nov 15, 2013
32
Actually I am getting the display of text (Time & Date), But not getting the value which i want to display..??
Now Tell me what to do???
 

Thread Starter

PIYUSH SONI

Joined Nov 15, 2013
32
But if i am doing any other program my display(LCD) is working fine. My display is working fine bro..
Is any other error u can find & help me bro...
 

tshuck

Joined Oct 18, 2012
3,534
But if i am doing any other program my display(LCD) is working fine. My display is working fine bro..
Is any other error u can find & help me bro...
Bro?:confused:

Were your LCD interface working properly, you could display hard coded data...

Comment everything else out and verify its operation - process of elimination.
 

spinnaker

Joined Oct 29, 2009
7,830
Bro?:confused:

Were your LCD interface working properly, you could display hard coded data...

Comment everything else out and verify its operation - process of elimination.
Well Bro :eek:.

You are on the right track but I would advise creating a brand new test program. One that displays a numerical value that was set in a variable. Bonus points for a for loop.

OP,

I suspect that for starters there is something wrong with your code that displays numeric variables. And Hello does not have a w on the end. :)
 

spinnaker

Joined Oct 29, 2009
7,830
Something like this:

Rich (BB code):
void main()
{
int i;

while(1)
{
    for(i=0; i<=10; i++)  
       Lcd_Chr(1, 6, (day / 10)   + 48); 
}
}
 

Thread Starter

PIYUSH SONI

Joined Nov 15, 2013
32
hello guys, I have tried a lot but still my clock is not running. Once again i am posting my code. I have done some modification , Please go through it & Help me in all possible way.
Rich (BB code):
// Lcd module connections
sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB2_bit;
sbit LCD_D5 at LATB3_bit;
sbit LCD_D6 at LATB4_bit;
sbit LCD_D7 at LATB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;

// Lcd constants
char txt1[] = "Hello";
char txt2[] = "RTC2 click";
char txt3[] = "DATE:";
char txt4[] = "TIME:";

// Module variables
//unsigned char hours, minutes, seconds, day, date, month, year,*txt;    // Global date/time variables
char oldstate = 0;




// RTC Definitions
#define RTC_ADDR  0xD0
 
/**************************************************************************************************
* DS1307 Functions
**************************************************************************************************/

/**************************************************************************************************
* Read data from RTC DS1307
* input : addres of RTC register
* output: value of of RTC register
**************************************************************************************************/
unsigned char RTC_Read(unsigned char addr){
  unsigned char value;

  I2C1_Start();                   // Issue start signal
  I2C1_Wr(RTC_ADDR);              // Address DS1307, see DS1307 datasheet
  I2C1_Wr(addr);                  // Start from address
  I2C1_Start();                   // Issue repeated start signal
  I2C1_Wr(RTC_ADDR);              // Address DS1307 for reading R/W=1

  value = I2C1_Rd(0);             // Read seconds byte
  I2C1_Stop();                    // Issue stop signal

  return value;
}

/**************************************************************************************************
* Write data from DS1307
* input : addres of RTC register, value of of RTC register
**************************************************************************************************/
void RTC_Write(unsigned char addr, unsigned char value){
  I2C1_Start();         // Issue start signal
  I2C1_Wr(RTC_ADDR);    // Address DS1307
  I2C1_Wr(addr);        // Start from address
  I2C1_Wr(value);       // Write value to RTC register
  I2C1_Stop();          // Issue stop signal
}

/**************************************************************************************************
* Read time from RTC DS1307
* input : pointer to variables where RTC data will be stored
* output: variables with RTC data
**************************************************************************************************/
void Read_Time(char seconds,char minutes,char hours,char day,char date,char month,char year)
{
  I2C1_Start();                // Issue start signal
  I2C1_Wr(RTC_ADDR);           // Address DS1307, see DS1307 datasheet
  I2C1_Wr(0);                  // Start from address 0
  I2C1_Repeated_Start();       // Issue repeated start signal
  I2C1_Wr(0xD1);       // Address DS1307 for reading R/W=1

  seconds = I2C1_Rd(1);     // Read seconds byte
  minutes = I2C1_Rd(1);     // Read minutes byte
  hours = I2C1_Rd(1);       // Read hours byte
  day =I2C1_Rd(1);
  date =I2C1_Rd(1);
  month =I2C1_Rd(1);
  year =I2C1_Rd(0);

  I2C1_Stop();                 // Issue stop signal
}

/**************************************************************************************************
* Write time to RTC DS1307
* input : variables with RTC data
**************************************************************************************************/
void Write_Time(unsigned char seconds, unsigned char c_minutes, unsigned char hours,
                unsigned char day, unsigned char date, unsigned char month, unsigned char year){
   I2C1_Start();                     // issue start signal
   I2C1_Wr(RTC_ADDR);                // address DS1307
   I2C1_Wr(0);                       // start from word at address (REG0)
   I2C1_Wr(0x00);                    // write $80 to REG0. (pause counter + 0 sec)

   I2C1_Wr(0x00);               // write 0 to minutes word to (REG1)
   I2C1_Wr(0x01);                 // write 17 to hours word (24-hours mode)(REG2)
   I2C1_Wr(0x02);
   I2C1_Wr(0x03);                  // 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(0x06);                  // write 01 to year word (REG6)
   I2C1_Stop();                      // issue stop signal

   I2C1_Start();                     // issue start signal
   I2C1_Wr(RTC_ADDR);                // address DS1307
   I2C1_Wr(0);                       // start from word at address 0
   I2C1_Wr(0 | seconds);           // write 0 to REG0 (enable counting + 0 sec)
   I2C1_Stop();                      // issue stop signal
}
/**************************************************************************************************
* Show on the LCD display
* input : variables with RTC data
**************************************************************************************************/
void Transform_Time(char seconds,char minutes,char hours,char day,char date,char month,char year){


  seconds  =  ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F);
  minutes  =  ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F);
  hours    =  ((hours & 0xF0) >> 4)*10 + (hours & 0x0F);
  day      =  (day & 0x07);
  date     =  ((date & 0xF0) >> 4)*10 + (day & 0x0F);
  month    =  ((month & 0xF0) >> 4)*10 + (month & 0x0F);
  year     =  ((year & 0xF0)>>4)*10+(year & 0x0F);


  }
  void Show_Time()
  {
  switch(day)
  {
      char *txt;
    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(2,14, txt);
  Lcd_Chr(1, 6, (date / 10)   + 48);    // Print tens digit of day variable
  Lcd_Chr(1, 7, (date % 10)   + 48);    // Print oness digit of day variable
   Lcd_Chr(1, 9, (month / 10) + 48);
  Lcd_Chr(1,10, (month % 10) + 48);
  Lcd_Chr(1,14, (year / 10)  + 48);
  Lcd_Chr(1,15, (year % 10)  + 48);


  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);
}
 void Init_main()
 {
 ADRESL = 0;
 ADRESH = 0;
 ADCON0=0;
 ADCON1=0x0F;
 ADCON2=0x00;
 SMP_bit = 1;
 PORTC =0;
 TRISC =0XFF;

  CMCON =7;
  PORTA = 0;

  TRISA2_bit = 1;

  I2C1_Init(100000);                 // Initialize I2C bus for communication with RTC
  Lcd_Init();                        // Initialize Lcd

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,4,txt2);                 // Write text in second row

  Delay_ms(2000);                    // delay 2 sec

  Lcd_Cmd(_LCD_CLEAR);               // Prepare and output static text on LCD
  LCD_Chr(1,8,'.');
  LCD_Chr(1,11,'.');
  LCD_Out(1,1,txt3);
  LCD_Out(2,1,txt4);
  LCD_Chr(2,8,':');
  LCD_Chr(2,11,':');
  LCD_Out(1,12,"20");
 }
/**************************************************************************************************
* MAIN PROGRAM
**************************************************************************************************/
void main()
{
    Delay_ms(1000);
    Init_main();
    Write_Time(seconds, minutes, hours,day, date, month, year);
     while(1)
     {           
      Delay_ms(1000);
       Transform_Time(seconds,minutes,hours,day,date,month,year);
       Show_Time();
       Read_Time(&seconds,&minutes,&hours,&day,&date,&month,&year);

     }

   }
 

t06afre

Joined May 11, 2009
5,934
What kind of a programmer do you have. If it is a PICKIT 2 or 3. Then it is time to do some debugging. The person most close to help you is your self. It is hard for others just by looking at some code to pinpoint your error
 

spinnaker

Joined Oct 29, 2009
7,830
hello guys, I have tried a lot but still my clock is not running. Once again i am posting my code. I have done some modification , Please go through it & Help me in all possible way.
I missed the part where you confirmed that your number display code is working correctly. Do you try to write hard coded numbers to the display by setting a variable to a number and then try writing that number to the display?

If you did that what happened?

If you did not then why have you not tried it?
 
Top