Problem in Glcd clock with ds1307 rtc

Discussion in 'Embedded Systems and Microcontrollers' started by adarsh33, Jan 20, 2014.

  1. adarsh33

    Thread Starter New Member

    Jan 20, 2014
    1
    0
    Hello Everyone,
    Kindly anyone could help me, I am making a Glcd based clock with ds1307 rtc using pic 18f4520,I am using HS oscillator of 20Mhz. My Glcd(12864a) is working fine and I have also checked rtc(also using 32khz crystal for drving the rtc ) through oscilloscope. I am not able to find why my clock is not working and It only displays one digit ascii character and keeps blinking always. I am attaching my source code. I am using MikroC pro for Pic compiler and pic kit 3 programmer in .

    Code ( (Unknown Language)):
    1. // Glcd module connections
    2. char GLCD_DataPort at PORTD;
    3.  
    4. sbit GLCD_CS1 at LATB3_bit;
    5. sbit GLCD_CS2 at LATB4_bit;
    6. sbit GLCD_RS  at LATB0_bit;
    7. sbit GLCD_RW  at LATB1_bit;
    8. sbit GLCD_EN  at LATB2_bit;
    9. sbit GLCD_RST at LATB5_bit;
    10. sbit GLCD_DB0 at LATD0_bit;
    11. sbit GLCD_DB1 at LATD1_bit;
    12. sbit GLCD_DB2 at LATD2_bit;
    13. sbit GLCD_DB3 at LATD3_bit;
    14. sbit GLCD_DB4 at LATD4_bit;
    15. sbit GLCD_DB5 at LATD5_bit;
    16. sbit GLCD_DB6 at LATD6_bit;
    17. sbit GLCD_DB7 at LATD7_bit;
    18.  
    19. sbit GLCD_CS1_Direction at TRISB3_bit;
    20. sbit GLCD_CS2_Direction at TRISB4_bit;
    21. sbit GLCD_RS_Direction  at TRISB0_bit;
    22. sbit GLCD_RW_Direction  at TRISB1_bit;
    23. sbit GLCD_EN_Direction  at TRISB2_bit;
    24. sbit GLCD_RST_Direction at TRISB5_bit;
    25.  
    26. sbit GLCD_DB0_Direction at TRISD0_bit;
    27. sbit GLCD_DB1_Direction at TRISD1_bit;
    28. sbit GLCD_DB2_Direction  at TRISD2_bit;
    29. sbit GLCD_DB3_Direction  at TRISD3_bit;
    30. sbit GLCD_DB4_Direction  at TRISD4_bit;
    31. sbit GLCD_DB5_Direction at TRISD5_bit;
    32. sbit GLCD_DB6_Direction  at TRISD6_bit;
    33. sbit GLCD_DB7_Direction at TRISD7_bit;
    34. // End Glcd module connections
    35.  
    36.  
    37.  
    38. unsigned short read_ds1307(unsigned short address)
    39. {
    40.   unsigned short r_data;
    41.   I2C1_Start();
    42.   I2C1_Wr(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
    43.   I2C1_Wr(address);
    44.   I2C1_Repeated_Start();
    45.   I2C1_Wr(0xD1); //0x68 followed by 1 --> 0xD1
    46.   r_data=I2C1_Rd(0);
    47.   I2C1_Stop();
    48.   return(r_data);
    49. }
    50.  
    51.  
    52. void write_ds1307(unsigned short address,unsigned short w_data)
    53. {
    54.   I2C1_Start(); // issue I2C start signal
    55.   //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
    56.   I2C1_Wr(0xD0); // send byte via I2C (device address + W)
    57.   I2C1_Wr(address); // send byte (address of DS1307 location)
    58.   I2C1_Wr(w_data); // send data (data to be written)
    59.   I2C1_Stop(); // issue I2C stop signal
    60. }
    61.  
    62.  
    63. unsigned char BCD2UpperCh(unsigned char bcd)
    64. {
    65.   return ((bcd >> 4) + '1');
    66. }
    67.  
    68.  
    69. unsigned char BCD2LowerCh(unsigned char bcd)
    70. {
    71.   return ((bcd & 0x0F) + '1');
    72. }
    73.  
    74.  
    75. int Binary2BCD(int a)
    76. {
    77.    int t1, t2;
    78.    t1 = a%10;
    79.    t1 = t1 & 0x0F;
    80.    a = a/10;
    81.    t2 = a%10;
    82.    t2 = 0x0F & t2;
    83.    t2 = t2 << 4;
    84.    t2 = 0xF0 & t2;
    85.    t1 = t1 | t2;
    86.    return t1;
    87. }
    88.  
    89.  
    90. int BCD2Binary(int a)
    91. {
    92.    int r,t;
    93.    t = a & 0x0F;
    94.    r = t;
    95.    a = 0xF0 & a;
    96.    t = a >> 4;
    97.    t = 0x0F & t;
    98.    r = t*10 + r;
    99.    return r;
    100. }
    101.  
    102.  
    103.  
    104. int second;
    105. int minute;
    106. int hour;
    107. int hr;
    108. int day;
    109. int dday;
    110. int month;
    111. int year;
    112. int ap;
    113.  
    114. unsigned short set_count = 0;
    115. short set;
    116.  
    117. char *time = "00:00:00 PM";
    118. char *date = "00-00-00";
    119.  
    120. void main()
    121. {
    122.  
    123.  
    124. I2C1_Init(100000); //DS1307 I2C is running at 100KHz
    125.  
    126.     ADRESL = 0;
    127.     ADRESH = 0;
    128.    CMCON = 0x07;   // To turn off comparators
    129.    ADCON1 = 0xFF;  // To turn off analog to digital converters
    130.  
    131.  
    132.    TRISA = 0x07;
    133.    PORTA = 0x00;
    134.  
    135.    Glcd_Init();                        // Initialize GLCD
    136.    Glcd_Fill(0xFF);
    137.    Delay_ms(100);
    138.  
    139.    Glcd_Set_Font(Font_Glcd_Character8x7, 8 , 7, 32);
    140.    Glcd_Write_Text( "Time:" , 80, 1, 2);
    141.    Glcd_Write_Text( "Date:" , 65, 5, 2);
    142.  
    143.  
    144.    do
    145.    {
    146.      set = 0;
    147.      if(PORTA.F0 == 0)
    148.      {
    149.          Delay_ms(100);
    150.          if(PORTA.F0 == 0)
    151.          {
    152.              set_count++;
    153.              if(set_count >= 7)
    154.              {
    155.                 set_count = 0;
    156.              }
    157.          }
    158.      }
    159.      if(set_count)
    160.      {
    161.         if(PORTA.F2 == 0)
    162.         {
    163.           Delay_ms(100);
    164.           if(PORTA.F2 == 0)
    165.               set = 1;
    166.         }
    167.  
    168.         if(PORTA.F0 == 0)
    169.         {
    170.           Delay_ms(100);
    171.           if(PORTA.F2 == 0)
    172.               set = -1;
    173.         }
    174.         if(set_count && set)
    175.         {
    176.           switch(set_count)
    177.           {
    178.             case 1:
    179.                     hour = BCD2Binary(hour);
    180.                     hour = hour + set;
    181.                     hour = Binary2BCD(hour);
    182.                     if((hour & 0x1F) >= 0x13)
    183.                     {
    184.                       hour = hour & 0b11100001;
    185.                       hour = hour ^ 0x20;
    186.                     }
    187.                     else if((hour & 0x1F) <= 0x00)
    188.                     {
    189.                       hour = hour | 0b00010010;
    190.                       hour = hour ^ 0x20;
    191.                     }
    192.                     write_ds1307(2, hour); //write hour
    193.                     break;
    194.             case 2:
    195.                      minute = BCD2Binary(minute);
    196.                      minute = minute + set;
    197.                      if(minute >= 60)
    198.                         minute = 0;
    199.                      if(minute < 0)
    200.                         minute = 59;
    201.                      minute = Binary2BCD(minute);
    202.                      write_ds1307(1, minute); //write min
    203.                      break;
    204.             case 3:
    205.                     if(abs(set))
    206.                       write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
    207.                     break;
    208.             case 4:
    209.                      day = BCD2Binary(day);
    210.                      day = day + set;
    211.                      day = Binary2BCD(day);
    212.                      if(day >= 0x32)
    213.                         day = 1;
    214.                      if(day <= 0)
    215.                         day = 0x31;
    216.                      write_ds1307(4, day); // write date 17
    217.                      break;
    218.             case 5:
    219.                     month = BCD2Binary(month);
    220.                     month = month + set;
    221.                     month = Binary2BCD(month);
    222.                     if(month > 0x12)
    223.                       month = 1;
    224.                     if(month <= 0)
    225.                       month = 0x12;
    226.                     write_ds1307(5,month); // write month 6 June
    227.                     break;
    228.             case 6:
    229.                     year = BCD2Binary(year);
    230.                     year = year + set;
    231.                     year = Binary2BCD(year);
    232.                     if(year <= -1)
    233.                        year = 0x99;
    234.                     if(year >= 0x50)
    235.                        year = 0;
    236.                     write_ds1307(6, year); // write year
    237.                     break;
    238.           }
    239.         }
    240.      }
    241.  
    242.       second = read_ds1307(0);
    243.       minute = read_ds1307(1);
    244.       hour = read_ds1307(2);
    245.        hr = hour & 0b00011111;
    246.        ap = hour & 0b00100000;
    247.       dday = read_ds1307(3);
    248.       day = read_ds1307(4);
    249.       month = read_ds1307(5);
    250.       year = read_ds1307(6);
    251.  
    252.  
    253.       time[0] = BCD2UpperCh(hr);
    254.       time[1] = BCD2LowerCh(hr);
    255.       time[3] = BCD2UpperCh(minute);
    256.       time[4] = BCD2LowerCh(minute);
    257.       time[6] = BCD2UpperCh(second);
    258.       time[7] = BCD2LowerCh(second);
    259.  
    260.       date[0] = BCD2UpperCh(day);
    261.       date[1] = BCD2LowerCh(day);
    262.       date[3] = BCD2UpperCh(month);
    263.       date[4] = BCD2LowerCh(month);
    264.       date[6] = BCD2UpperCh(year);
    265.       date[7] = BCD2LowerCh(year);
    266.  
    267.       if(ap)
    268.       {
    269.          time[9] = 'P';
    270.          time[10] = 'M';
    271.       }
    272.       else
    273.       {
    274.          time[9] = 'A';
    275.          time[10] = 'M';
    276.       }
    277.  
    278.       Glcd_Write_Char( time, 80, 2, 2);
    279.       Glcd_Write_Char( date, 70, 6, 2);
    280.  
    281.       Delay_ms(100);
    282.  
    283.  
    284.    }while(1);
    285. }
     
  2. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    Code ( (Unknown Language)):
    1. Glcd_Write_Char( time, 80, 2, 2);
    2.  
    This function only write 1 char..

    Try something like this.
    Code ( (Unknown Language)):
    1.  
    2. for(x=0:x<8;x++)
    3.    Glcd_Write_Char( time[x], 80, 2+x, 2);
    4.  
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    That's what happens when you write lots and lots of code without any intermediate testing. You describe the display as working (you get a single digit) but then state the display doesn't work.

    You merged together at least 3 tasks inside main:

    Defining the time structure
    Incrementing the time structure
    Displaying the time structure

    Plus the date stuff, updating the DS1307...

    First you want to get a static time (or date) to display, then work from there.

    Lastly, if you can get a single character to display but not a string keep in mind a string in c is an array that ends in a zero, and the zero marks the end of the string.

    The string buffer must be at least as long as the string, but may be longer then the string.
     
  4. hexreader

    Active Member

    Apr 16, 2011
    249
    82
    I agree 100% with everything that ErnieM stated in the previous post.

    Luckily for you I am in good mood and desperately bored, so I have fixed some of the more basic errors in your code.

    Still plenty left for you to do though.

    Here are just enough fixes to give you encouragement:

    Code ( (Unknown Language)):
    1. // Glcd module connections
    2. char GLCD_DataPort at PORTD;
    3.  
    4. sbit GLCD_CS1 at LATB3_bit;
    5. sbit GLCD_CS2 at LATB4_bit;
    6. sbit GLCD_RS  at LATB0_bit;
    7. sbit GLCD_RW  at LATB1_bit;
    8. sbit GLCD_EN  at LATB2_bit;
    9. sbit GLCD_RST at LATB5_bit;
    10. sbit GLCD_DB0 at LATD0_bit;
    11. sbit GLCD_DB1 at LATD1_bit;
    12. sbit GLCD_DB2 at LATD2_bit;
    13. sbit GLCD_DB3 at LATD3_bit;
    14. sbit GLCD_DB4 at LATD4_bit;
    15. sbit GLCD_DB5 at LATD5_bit;
    16. sbit GLCD_DB6 at LATD6_bit;
    17. sbit GLCD_DB7 at LATD7_bit;
    18.  
    19. sbit GLCD_CS1_Direction at TRISB3_bit;
    20. sbit GLCD_CS2_Direction at TRISB4_bit;
    21. sbit GLCD_RS_Direction  at TRISB0_bit;
    22. sbit GLCD_RW_Direction  at TRISB1_bit;
    23. sbit GLCD_EN_Direction  at TRISB2_bit;
    24. sbit GLCD_RST_Direction at TRISB5_bit;
    25.  
    26. sbit GLCD_DB0_Direction at TRISD0_bit;
    27. sbit GLCD_DB1_Direction at TRISD1_bit;
    28. sbit GLCD_DB2_Direction  at TRISD2_bit;
    29. sbit GLCD_DB3_Direction  at TRISD3_bit;
    30. sbit GLCD_DB4_Direction  at TRISD4_bit;
    31. sbit GLCD_DB5_Direction at TRISD5_bit;
    32. sbit GLCD_DB6_Direction  at TRISD6_bit;
    33. sbit GLCD_DB7_Direction at TRISD7_bit;
    34. // End Glcd module connections
    35.  
    36.  
    37.  
    38. unsigned short read_ds1307(unsigned short address)
    39. {
    40.   unsigned short r_data;
    41.   I2C1_Start();
    42.   I2C1_Wr(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
    43.   I2C1_Wr(address);
    44.   I2C1_Repeated_Start();
    45.   I2C1_Wr(0xD1); //0x68 followed by 1 --> 0xD1
    46.   r_data=I2C1_Rd(0);
    47.   I2C1_Stop();
    48.   return(r_data);
    49. }
    50.  
    51.  
    52. void write_ds1307(unsigned short address,unsigned short w_data)
    53. {
    54.   I2C1_Start(); // issue I2C start signal
    55.   //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
    56.   I2C1_Wr(0xD0); // send byte via I2C (device address + W)
    57.   I2C1_Wr(address); // send byte (address of DS1307 location)
    58.   I2C1_Wr(w_data); // send data (data to be written)
    59.   I2C1_Stop(); // issue I2C stop signal
    60. }
    61.  
    62.  
    63. unsigned char BCD2UpperCh(unsigned char bcd)
    64. {
    65.   return ((bcd >> 4) + '0');
    66. }
    67.  
    68.  
    69. unsigned char BCD2LowerCh(unsigned char bcd)
    70. {
    71.   return ((bcd & 0x0F) + '0');
    72. }
    73.  
    74.  
    75. int Binary2BCD(int a)
    76. {
    77.    int t1, t2;
    78.    t1 = a%10;
    79.    t1 = t1 & 0x0F;
    80.    a = a/10;
    81.    t2 = a%10;
    82.    t2 = 0x0F & t2;
    83.    t2 = t2 << 4;
    84.    t2 = 0xF0 & t2;
    85.    t1 = t1 | t2;
    86.    return t1;
    87. }
    88.  
    89.  
    90. int BCD2Binary(int a)
    91. {
    92.    int r,t;
    93.    t = a & 0x0F;
    94.    r = t;
    95.    a = 0xF0 & a;
    96.    t = a >> 4;
    97.    t = 0x0F & t;
    98.    r = t*10 + r;
    99.    return r;
    100. }
    101.  
    102.  
    103.  
    104. int second;
    105. int minute;
    106. int hour;
    107. int hr;
    108. int day;
    109. int dday;
    110. int month;
    111. int year;
    112. int ap;
    113. int old_second;
    114.  
    115. unsigned short set_count = 0;
    116. short set;
    117.  
    118. char time[16] = "00:00:00 PM";
    119. char date[16] = "00-00-00";
    120.  
    121. void main()
    122. {
    123.  
    124.  
    125.     I2C1_Init(100000); //DS1307 I2C is running at 100KHz
    126.  
    127.     ADRESL = 0;
    128.     ADRESH = 0;
    129.     CMCON = 0x07;   // To turn off comparators
    130.     ADCON1 = 0xFF;  // To turn off analog to digital converters
    131.  
    132.     TRISA = 0x07;
    133.     PORTA = 0x00;
    134.  
    135.     Glcd_Init();                        // Initialize GLCD
    136.    Glcd_Set_Font(Font_Glcd_Character8x7, 8 , 7, 32);
    137.  
    138.    do
    139.    {
    140.  
    141.      set = 0;
    142.      if(PORTA.F0 == 0)
    143.      {
    144.          Delay_ms(100);
    145.          if(PORTA.F0 == 0)
    146.          {
    147.              set_count++;
    148.              if(set_count >= 7)
    149.              {
    150.                 set_count = 0;
    151.              }
    152.          }
    153.      }
    154.      if(set_count)
    155.      {
    156.         if(PORTA.F2 == 0)
    157.         {
    158.           Delay_ms(100);
    159.           if(PORTA.F2 == 0)
    160.               set = 1;
    161.         }
    162.  
    163.         if(PORTA.F0 == 0)
    164.         {
    165.           Delay_ms(100);
    166.           if(PORTA.F2 == 0)
    167.               set = -1;
    168.         }
    169.         if(set_count && set)
    170.         {
    171.           switch(set_count)
    172.           {
    173.             case 1:
    174.                     hour = BCD2Binary(hour);
    175.                     hour = hour + set;
    176.                     hour = Binary2BCD(hour);
    177.                     if((hour & 0x1F) >= 0x13)
    178.                     {
    179.                       hour = hour & 0b11100001;
    180.                       hour = hour ^ 0x20;
    181.                     }
    182.                     else if((hour & 0x1F) <= 0x00)
    183.                     {
    184.                       hour = hour | 0b00010010;
    185.                       hour = hour ^ 0x20;
    186.                     }
    187.                     write_ds1307(2, hour); //write hour
    188.                     break;
    189.             case 2:
    190.                      minute = BCD2Binary(minute);
    191.                      minute = minute + set;
    192.                      if(minute >= 60)
    193.                         minute = 0;
    194.                      if(minute < 0)
    195.                         minute = 59;
    196.                      minute = Binary2BCD(minute);
    197.                      write_ds1307(1, minute); //write min
    198.                      break;
    199.             case 3:
    200.                     if(abs(set))
    201.                       write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
    202.                     break;
    203.             case 4:
    204.                      day = BCD2Binary(day);
    205.                      day = day + set;
    206.                      day = Binary2BCD(day);
    207.                      if(day >= 0x32)
    208.                         day = 1;
    209.                      if(day <= 0)
    210.                         day = 0x31;
    211.                      write_ds1307(4, day); // write date 17
    212.                      break;
    213.             case 5:
    214.                     month = BCD2Binary(month);
    215.                     month = month + set;
    216.                     month = Binary2BCD(month);
    217.                     if(month > 0x12)
    218.                       month = 1;
    219.                     if(month <= 0)
    220.                       month = 0x12;
    221.                     write_ds1307(5,month); // write month 6 June
    222.                     break;
    223.             case 6:
    224.                     year = BCD2Binary(year);
    225.                     year = year + set;
    226.                     year = Binary2BCD(year);
    227.                     if(year <= -1)
    228.                        year = 0x99;
    229.                     if(year >= 0x50)
    230.                        year = 0;
    231.                     write_ds1307(6, year); // write year
    232.                     break;
    233.           }
    234.         }
    235.      }
    236.  
    237.       second = read_ds1307(0);
    238.       minute = read_ds1307(1);
    239.       hour = read_ds1307(2);
    240.        hr = hour & 0b00011111;
    241.        ap = hour & 0b00100000;
    242.       dday = read_ds1307(3);
    243.       day = read_ds1307(4);
    244.       month = read_ds1307(5);
    245.       year = read_ds1307(6);
    246.  
    247.       time[0] = BCD2UpperCh(hr);
    248.       time[1] = BCD2LowerCh(hr);
    249.       time[3] = BCD2UpperCh(minute);
    250.       time[4] = BCD2LowerCh(minute);
    251.       time[6] = BCD2UpperCh(second);
    252.       time[7] = BCD2LowerCh(second);
    253.  
    254.       date[0] = BCD2UpperCh(day);
    255.       date[1] = BCD2LowerCh(day);
    256.       date[3] = BCD2UpperCh(month);
    257.       date[4] = BCD2LowerCh(month);
    258.       date[6] = BCD2UpperCh(year);
    259.       date[7] = BCD2LowerCh(year);
    260.  
    261.       if(ap)
    262.       {
    263.          time[9] = 'P';
    264.          time[10] = 'M';
    265.       }
    266.       else
    267.       {
    268.          time[9] = 'A';
    269.          time[10] = 'M';
    270.       }
    271.  
    272.       if(second != old_second){                        // only display if data has changed
    273.           Glcd_Fill(0xFF);
    274.           Glcd_Write_Text( "Time:" , 10, 1, 2);
    275.           Glcd_Write_Text( "Date:" , 10, 5, 2);
    276.  
    277.           Glcd_Write_Text( time, 20, 2, 2);
    278.           Glcd_Write_Text( date, 20, 6, 2);
    279.           old_second = second;                            // prevent repeat display of same data
    280.       }
    281.  
    282.    }while(1);
    283. }
     
Loading...