DS1307 Rtc Problem

Discussion in 'Embedded Systems and Microcontrollers' started by PIYUSH SONI, Jan 24, 2014.

  1. PIYUSH SONI

    Thread Starter Member

    Nov 15, 2013
    32
    1
    Hello, everyone.
    I am trying to display date & time from RTC Ds1307 on LCD using Pic 18f4520 MCU. I have also connected a push button on PORTA and by pressing it Time & date should display.

    Sometime i get garbage value but most time nothing at all.
    I have attached my code guys. PLEASE go through it & Kindly HELP me.
    Code ( (Unknown Language)):
    1.  
    2. // Lcd module connections
    3. sbit LCD_RS at LATB0_bit;
    4. sbit LCD_EN at LATB1_bit;
    5. sbit LCD_D4 at LATB2_bit;
    6. sbit LCD_D5 at LATB3_bit;
    7. sbit LCD_D6 at LATB4_bit;
    8. sbit LCD_D7 at LATB5_bit;
    9.  
    10. sbit LCD_RS_Direction at TRISB0_bit;
    11. sbit LCD_EN_Direction at TRISB1_bit;
    12. sbit LCD_D4_Direction at TRISB2_bit;
    13. sbit LCD_D5_Direction at TRISB3_bit;
    14. sbit LCD_D6_Direction at TRISB4_bit;
    15. sbit LCD_D7_Direction at TRISB5_bit;
    16.  
    17. // Lcd constants
    18. char txt1[] = "SPG INFOTECH";
    19. char txt2[] = "DIGITAL CLOCK";
    20. char txt3[] = "DATE:";
    21. char txt4[] = "TIME:";
    22.  
    23. // Module variables
    24. unsigned char hours, minutes, seconds, day, week, month, year;    // Global date/time variables
    25. char oldstate = 0;
    26.  
    27.  
    28.  
    29. // RTC Definitions
    30. #define RTC_ADDR  0xD0
    31.  
    32. /**************************************************************************************************
    33. * DS1307 Functions
    34. **************************************************************************************************/
    35.  
    36. /**************************************************************************************************
    37. * Read data from RTC DS1307
    38. * input : addres of RTC register
    39. * output: value of of RTC register
    40. **************************************************************************************************/
    41. unsigned char RTC_Read(unsigned char addr){
    42.   unsigned char value;
    43.  
    44.   I2C1_Start();                   // Issue start signal
    45.   I2C1_Wr(RTC_ADDR);              // Address DS1307, see DS1307 datasheet
    46.   I2C1_Wr(addr);                  // Start from address 2
    47.   I2C1_Start();                   // Issue repeated start signal
    48.   I2C1_Wr(RTC_ADDR);              // Address DS1307 for reading R/W=1
    49.  
    50.   value = I2C1_Rd(0);             // Read seconds byte
    51.   I2C1_Stop();                    // Issue stop signal
    52.  
    53.   return value;
    54. }
    55.  
    56. /**************************************************************************************************
    57. * Write data from DS1307
    58. * input : addres of RTC register, value of of RTC register
    59. **************************************************************************************************/
    60. void RTC_Write(unsigned char addr, unsigned char value){
    61.   I2C1_Start();         // Issue start signal
    62.   I2C1_Wr(RTC_ADDR);    // Address DS1307
    63.   I2C1_Wr(addr);        // Start from address
    64.   I2C1_Wr(value);       // Write value to RTC register
    65.   I2C1_Stop();          // Issue stop signal
    66. }
    67.  
    68. /**************************************************************************************************
    69. * Read time from RTC DS1307
    70. * input : pointer to variables where RTC data will be stored
    71. * output: variables with RTC data
    72. **************************************************************************************************/
    73. void Read_Time(unsigned char *p_hours, unsigned char *p_minutes, unsigned char *p_seconds,
    74.                unsigned char *p_day, unsigned char *p_week, unsigned char *p_month, unsigned char *p_year){
    75.   I2C1_Start();                // Issue start signal
    76.   I2C1_Wr(RTC_ADDR);           // Address DS1307, see DS1307 datasheet
    77.   I2C1_Wr(0);                  // Start from address 0
    78.   I2C1_Repeated_Start();       // Issue repeated start signal
    79.   I2C1_Wr(RTC_ADDR + 1);       // Address DS1307 for reading R/W=1
    80.  
    81.   *p_seconds = I2C1_Rd(1);     // Read seconds byte
    82.   *p_minutes = I2C1_Rd(1);     // Read minutes byte
    83.   *p_hours = I2C1_Rd(1);       // Read hours byte
    84.   *p_week =I2C1_Rd(1);
    85.   *p_day =I2C1_Rd(1);
    86.   *p_month =I2C1_Rd(1);
    87.   *p_year =I2C1_Rd(0);
    88.  
    89.   I2C1_Stop();                 // Issue stop signal
    90. }
    91.  
    92. /**************************************************************************************************
    93. * Write time to RTC DS1307
    94. * input : variables with RTC data
    95. **************************************************************************************************/
    96. void Write_Time(unsigned char c_hours, unsigned char c_minutes, unsigned char c_seconds,
    97.                 unsigned char c_day, unsigned char c_week, unsigned char c_month, unsigned char c_year){
    98.    I2C1_Start();                     // issue start signal
    99.    I2C1_Wr(RTC_ADDR);                // address DS1307
    100.    I2C1_Wr(0);                       // start from word at address (REG0)
    101.    I2C1_Wr(0x80);                    // write $80 to REG0. (pause counter + 0 sec)
    102.  
    103.    I2C1_Wr(c_minutes);               // write 0 to minutes word to (REG1)
    104.    I2C1_Wr(c_hours);                 // write 17 to hours word (24-hours mode)(REG2)
    105.    I2C1_Wr(c_week);                  // write 2 - Monday (REG3)
    106.    I2C1_Wr(c_day);                   // write 4 to date word (REG4)
    107.    I2C1_Wr(c_month);                 // write 5 (May) to month word (REG5)
    108.    I2C1_Wr(c_year);                  // write 01 to year word (REG6)
    109.    I2C1_Stop();                      // issue stop signal
    110.  
    111.    I2C1_Start();                     // issue start signal
    112.    I2C1_Wr(RTC_ADDR);                // address DS1307
    113.    I2C1_Wr(0);                       // start from word at address 0
    114.    I2C1_Wr(0 | c_seconds);           // write 0 to REG0 (enable counting + 0 sec)
    115.    I2C1_Stop();                      // issue stop signal
    116. }
    117. /**************************************************************************************************
    118. * Show on the LCD display
    119. * input : variables with RTC data
    120. **************************************************************************************************/
    121. void Show_Time(){
    122.   char *txt;
    123.  
    124.   seconds  =  ((seconds & 0x70) >> 4)*10 + (seconds & 0x0F);
    125.   minutes  =  ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F);
    126.   hours    =  ((hours & 0x30) >> 4)*10 + (hours & 0x0F);
    127.   week     =  (week & 0x07);
    128.   day      =  ((day & 0xF0) >> 4)*10 + (day & 0x0F);
    129.   month    =  ((month & 0x10) >> 4)*10 + (month & 0x0F);
    130.   year     =  ((year & 0xF0)>>4)*10+(year & 0x0F);
    131.  
    132.   switch(week)
    133.   {
    134.     case 1: txt="Sun"; break;
    135.     case 2: txt="Mon"; break;
    136.     case 3: txt="Tue"; break;
    137.     case 4: txt="Wed"; break;
    138.     case 5: txt="Thu"; break;
    139.     case 6: txt="Fri"; break;
    140.     case 7: txt="Sat"; break;
    141.   }
    142.  
    143.   /*LCD_Out(1,1, txt);*/
    144.   Lcd_Out(1, 6, (day / 10)   + 48);    // Print tens digit of day variable
    145.   Lcd_Out(1, 7, (day % 10)   + 48);    // Print oness digit of day variable
    146.   Lcd_Out(1, 9, (month / 10) + 48);
    147.   Lcd_Out(1,10, (month % 10) + 48);
    148.   Lcd_Out(1,14, (year / 10)  + 48);
    149.   Lcd_Out(1,15, (year % 10)  + 48);
    150.  
    151.   Lcd_Out(2, 6, (hours / 10)   + 48);
    152.   Lcd_Out(2, 7, (hours % 10)   + 48);
    153.   Lcd_Out(2, 9, (minutes / 10) + 48);
    154.   Lcd_Out(2,10, (minutes % 10) + 48);
    155.   Lcd_Out(2,12, (seconds / 10) + 48);
    156.   Lcd_Out(2,13, (seconds % 10) + 48);
    157. }
    158.  
    159. /**************************************************************************************************
    160. * MAIN PROGRAM
    161. **************************************************************************************************/
    162. void main(){
    163.  ADRESL = 0;
    164. ADRESH = 0;
    165. CMCON =7;
    166. PORTA = 0;
    167. TRISA =1;
    168. LATA=1;
    169. ADCON0 =0;
    170. ADCON1 =0;
    171. ADCON2 = 0;
    172. TRISA = 0b00000111;
    173. PORTD = 0;
    174.   I2C1_Init(100000);                 // Initialize I2C bus for communication with RTC
    175.   Lcd_Init();                        // Initialize Lcd
    176.  
    177.   Lcd_Cmd(_LCD_CLEAR);               // Clear display
    178.   Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
    179.   Lcd_Out(1,1,txt1);                 // Write text in first row
    180.   Lcd_Out(2,4,txt2);                 // Write text in second row
    181.  
    182.   Delay_ms(2000);                    // delay 2 sec
    183.  
    184.   Lcd_Cmd(_LCD_CLEAR);               // Prepare and output static text on LCD
    185.   LCD_Chr(1,8,'.');
    186.   LCD_Chr(1,11,'.');
    187.   LCD_Out(2,1,txt3);
    188.   LCD_Out(1,1,txt4);
    189.   LCD_Chr(2,8,':');
    190.   LCD_Chr(2,11,':');
    191.   LCD_Out(1,12,"20");
    192.  
    193.  // Start the test examples
    194.   while(1){                           // Endless loop
    195.     if (Button(&PORTA, 1, 1, 1)) {               // Detect logical one
    196.       oldstate = 1;                              // Update flag
    197.     }
    198.     if (oldstate && Button(&PORTA, 1, 1, 0)) {   // Detect one-to-zero transition
    199. // Write TIME    11  : 15  : 00  : 24.  tuesday april  2012
    200.       Write_Time(0x11, 0x15, 0x00, 0x24, 0x03,   0x04, 0x12);
    201.       oldstate = 0;                             // Update flag
    202.     }
    203.  
    204.  
    205.     Read_Time(&hours, &minutes, &seconds, &day, &week, &month, &year);
    206.     Show_Time();
    207.     Delay_mS(10);
    208.   }
    209.     }
    210.  
    211.  
    212. /**************************************************************************************************
    213. * End of File
    214.  
    215.  
    216.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    If the time will not update faster then, ohh, say, once a second, why do you try to update every 10 milliseconds? While you provide little info on your specific problem it could just be you are updating the screen so fast there isn't time for anything to appear.

    Otherwise, break the problem down: display test data, not DS1307 data but data you create. Then read the DS1307 and look at the results on your in-circuit debugger.

    Once they both work, stitch together and enjoy the timeless quality of your new clock.

    Also, what compiler are you using? I've seen XC8 do so poorly doing pointers I stopped using it for a while. You over and over pass the time variables as pointers, but as they are global variables anyway they are already shared and don't need to be passed at all.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    You are going to have to put a whole lot more effort into this on your own if you want help.

    First off what does "Sometime i get garbage value but most time nothing" mean?

    Have you confirmed that your LCD code is working properly by trying to display some static message such as "Hello World" ? If you have not then start there. Don't worry about the RTC values yet.
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    It looks like you are using MikroC compiler?

    I thought it already has functions for using the Dallas DTC 1-wire clock?

    MikroE sell a Dallas 1-wire clock module, so I'm sure they have source code already working for that IC. :)
     
  5. PIYUSH SONI

    Thread Starter Member

    Nov 15, 2013
    32
    1
    Hi, Erniem.
    Are u telling me to give 1sec delay... I am using mikroCpro and i am not getting you about breaking the problem down.
    Can you explain me.
    Thanks.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    The same thing that I wrote. Do not try to work on the whole problem at the same time.


    1. Get something simple displayed on your LCD, like "Hello".

    once that works

    2. Use your debugger and investigate the results that you get back from the sensor.

    Once you know you are getting a value back and understand how to convert move on to displaying it on the LCD.

    You are trying to do too much all at once. If you break the problem down into small pieces then it will be much easier to understand and for others to help you.
     
  7. PIYUSH SONI

    Thread Starter Member

    Nov 15, 2013
    32
    1
    ok i got it spinnaker. I am able to display the text part on LCD "SPG INFOTECH" , "Digital clock", "DATE:" & "TIME" .
    These all are displaying. Only the clock part is not diplaying.
    I don't know the problem behind it..Can you...?
    Thanks for ur cooperation.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    That is good. Now use your debugger and see what value you are getting from the RTC.
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    And do you have a scope or logic analyzer? You should test that proper data is being sent to the rtc.
     
Loading...