Help me understand some lines in RTC displayed in lcd

Discussion in 'Embedded Systems and Microcontrollers' started by xed339, Feb 10, 2013.

  1. xed339

    Thread Starter New Member

    Feb 4, 2013
    13
    0
    Hi everyone please help me understand some lines in this code.i found this on the web, i forgot the man who made this code but thanks to him. i dont know the function of some lines.ill post the whole code. here it is. the ones that i highlighted.. thanks in advance. im looking forward for your replys:)

    Code ( (Unknown Language)):
    1.  
    2. // LCD module connections
    3. sbit LCD_RS at RD2_bit;
    4. sbit LCD_EN at RD3_bit;
    5. sbit LCD_D4 at RD4_bit;
    6. sbit LCD_D5 at RD5_bit;
    7. sbit LCD_D6 at RD6_bit;
    8. sbit LCD_D7 at RD7_bit;
    9.  
    10. sbit LCD_RS_Direction at TRISD2_bit;
    11. sbit LCD_EN_Direction at TRISD3_bit;
    12. sbit LCD_D4_Direction at TRISD4_bit;
    13. sbit LCD_D5_Direction at TRISD5_bit;
    14. sbit LCD_D6_Direction at TRISD6_bit;
    15. sbit LCD_D7_Direction at TRISD7_bit;
    16. // End LCD module connections
    17.  
    18.  
    19.  
    20. unsigned short read_ds1307(unsigned short address)
    21. {
    22.   unsigned short r_data;
    23.   I2C1_Start();
    24.   delay_ms(400);
    25.   I2C1_Wr(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
    26.   I2C1_Wr(address);
    27.   I2C1_Repeated_Start();
    28.   //delay_ms(400);
    29.   I2C1_Wr(0xD1); //0x68 followed by 1 --> 0xD1
    30.   r_data=I2C1_Rd(0);
    31.   I2C1_Stop();
    32.   //delay_ms(400);
    33.   return(r_data);
    34. }
    35.  
    36.  
    37. void write_ds1307(unsigned short address,unsigned short w_data)
    38. {
    39.   I2C1_Start(); // issue I2C start signal
    40.   //delay_ms(400);
    41.   //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
    42.   I2C1_Wr(0xD0); // send byte via I2C (device address + W)
    43.   I2C1_Wr(address); // send byte (address of DS1307 location)
    44.   I2C1_Wr(w_data); // send data (data to be written)
    45.   I2C1_Stop(); // issue I2C stop signal
    46. }
    47.  
    48.  
    49. unsigned char BCD2UpperCh(unsigned char bcd)
    50. {
    51.   return ((bcd >> 4) + '0');
    52. }
    53.  
    54.  
    55. [COLOR=Red]unsigned char BCD2LowerCh(unsigned char bcd)
    56. {
    57.   return ((bcd & 0x0F) + '0');
    58. }
    59.  
    60.  
    61. int Binary2BCD(int a)
    62. {
    63.    int t1, t2;
    64.    t1 = a%10;
    65.    t1 = t1 & 0x0F;
    66.    a = a/10;
    67.    t2 = a%10;
    68.    t2 = 0x0F & t2;
    69.    t2 = t2 << 4;
    70.    t2 = 0xF0 & t2;
    71.    t1 = t1 | t2;
    72.    return t1;
    73. }
    74.  
    75.  
    76. int BCD2Binary(int a)
    77. {
    78.    int r,t;
    79.    t = a & 0x0F;
    80.    r = t;
    81.    a = 0xF0 & a;
    82.    t = a >> 4;
    83.    t = 0x0F & t;
    84.    r = t*10 + r;
    85.    return r;
    86. }
    87. [/COLOR]
    88.  
    89.  
    90. int second;
    91. int minute;
    92. int hour;
    93. int hr;
    94. int day;
    95. int dday;
    96. int month;
    97. int year;
    98. int ap;
    99.  
    100. unsigned short set_count = 0;
    101. short set;
    102.  
    103. char time[] = "00:00:00 AM";
    104. char date[] = "00-00-00";
    105.  
    106. void main()
    107. {
    108.    I2C1_Init(100000); //DS1307 I2C is running at 100KHz
    109.    //delay_ms(400);
    110.  
    111.    CMCON = 0x07;   // To turn off comparators
    112.    ADCON1 = 0x06;  // To turn off analog to digital converters
    113.  
    114.    TRISA = 0x07;
    115.    PORTA = 0x00;
    116.  
    117.    Lcd_Init();                        // Initialize LCD
    118.    Lcd_Cmd(_LCD_CLEAR);               // Clear display
    119.    Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
    120.    Lcd_out(1,1,"Time:");
    121.    Lcd_out(2,1,"Date:");
    122.  
    123.    do
    124.    {
    125.      set = 0;
    126.      if(PORTA.F0 == 0)
    127.      {
    128.          Delay_ms(100);
    129.          if(PORTA.F0 == 0)
    130.          {
    131.              set_count++;
    132.              if(set_count >= 7)
    133.              {
    134.                 set_count = 0;
    135.              }
    136.          }
    137.      }
    138.      if(set_count)
    139.      {
    140.         if(PORTA.F1 == 0)
    141.         {
    142.           Delay_ms(100);
    143.           if(PORTA.F1 == 0)
    144.               set = 1;
    145.         }
    146.  
    147.         if(PORTA.F2 == 0)
    148.         {
    149.           Delay_ms(100);
    150.           if(PORTA.F2 == 0)
    151.               set = -1;
    152.         }
    153.         if(set_count && set)
    154.         {
    155.           switch(set_count)
    156.           {
    157.             case 1:
    158.                     hour = BCD2Binary(hour);
    159.                     hour = hour + set;
    160.                     hour = Binary2BCD(hour);
    161.                     if((hour & 0x1F) >= 0x13)
    162.                     {
    163.                      [COLOR=red] hour = hour & 0b11100001;[/COLOR]
    164.                       [COLOR=red]hour = hour ^ 0x20;[/COLOR]
    165.                     }
    166.                     else if((hour & 0x1F) <= 0x00)
    167.                     {
    168.                       hour = hour | 0b00010010;
    169.                       hour = hour ^ 0x20;
    170.                     }
    171.                     write_ds1307(2, hour); //write hour
    172.                     break;
    173.             case 2:
    174.                      minute = BCD2Binary(minute);
    175.                      minute = minute + set;
    176.                      if(minute >= 60)
    177.                         minute = 0;
    178.                      if(minute < 0)
    179.                         minute = 59;
    180.                      minute = Binary2BCD(minute);
    181.                      write_ds1307(1, minute); //write min
    182.                      break;
    183.             case 3:
    184.                     if(abs(set))
    185.                       write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
    186.                     break;
    187.             case 4:
    188.                      day = BCD2Binary(day);
    189.                      day = day + set;
    190.                      day = Binary2BCD(day);
    191.                      if(day >= 0x32)
    192.                         day = 1;
    193.                      if(day <= 0)
    194.                         day = 0x31;
    195.                      write_ds1307(4, day); // write date 17
    196.                      break;
    197.             case 5:
    198.                     month = BCD2Binary(month);
    199.                     month = month + set;
    200.                     month = Binary2BCD(month);
    201.                     if(month > 0x12)
    202.                       month = 1;
    203.                     if(month <= 0)
    204.                       month = 0x12;
    205.                     write_ds1307(5,month); // write month 6 June
    206.                     break;
    207.             case 6:
    208.                     year = BCD2Binary(year);
    209.                     year = year + set;
    210.                     year = Binary2BCD(year);
    211.                     if(year <= -1)
    212.                        year = 0x99;
    213.                     if(year >= 0x50)
    214.                        year = 0;
    215.                     write_ds1307(6, year); // write year
    216.                     break;
    217.           }
    218.         }
    219.      }
    220.  
    221.       second = read_ds1307(0);
    222.       minute = read_ds1307(1);
    223.       hour = read_ds1307(2);
    224.        hr = hour & 0b00011111;
    225.        ap = hour & 0b00100000;
    226.       dday = read_ds1307(3);
    227.       day = read_ds1307(4);
    228.       month = read_ds1307(5);
    229.       year = read_ds1307(6);
    230.  
    231.  
    232.       time[0] = BCD2UpperCh(hr);
    233.       time[1] = BCD2LowerCh(hr);
    234.       time[3] = BCD2UpperCh(minute);
    235.       time[4] = BCD2LowerCh(minute);
    236.       time[6] = BCD2UpperCh(second);
    237.       time[7] = BCD2LowerCh(second);
    238.  
    239.       date[0] = BCD2UpperCh(day);
    240.       date[1] = BCD2LowerCh(day);
    241.       date[3] = BCD2UpperCh(month);
    242.       date[4] = BCD2LowerCh(month);
    243.       date[6] = BCD2UpperCh(year);
    244.       date[7] = BCD2LowerCh(year);
    245.  
    246.       if(ap)
    247.       {
    248.          time[9] = 'P';
    249.          time[10] = 'M';
    250.       }
    251.       else
    252.       {
    253.          time[9] = 'A';
    254.          time[10] = 'M';
    255.       }
    256.  
    257.  
    258.       Lcd_out(1, 6, time);
    259.       Lcd_out(2, 6, date);
    260.       Delay_ms(50);
    261.  
    262.  
    263.    }while(1);
    264. }
    265.  
     
    Last edited by a moderator: Feb 11, 2013
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    Exactly what is it that you need to understand? First do you know the term BCD and it's purpose? Lets start there.
     
  3. xed339

    Thread Starter New Member

    Feb 4, 2013
    13
    0
    :)..i dont know that either...ok sir lets start there
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
  5. xed339

    Thread Starter New Member

    Feb 4, 2013
    13
    0
    Sir I read some of it. Correct me if I'm wrong. Does this page explain about converting binary to decimal? Is that right? That's my understanding about this page. Well I understand how to convert. So do we proceed to the next stage?
     
  6. xed339

    Thread Starter New Member

    Feb 4, 2013
    13
    0
    Sir is it really needed in this program..that BCD thing??
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    BCD is a way to represent a number decimal number from 0 to 9 in binary 0000 to 1001.

    This is useful because it is the smallest way to store all numbers. Also, since in computers a byte is 8 bits it can store 2 BCD numbers 00 thru 99. This is sometimes called a "packed BCD number." If you look close at the BCD2UpperCh() and BCD2LowerCh() functions and you can see how each BCD number is extracted from the packed number.

    Both BCD and binary have strengths and weaknesses. Binary is the best format to use to do calculations. BCD is better to display the data.

    The DS1307 uses packed BCD to store the time, see sheet 8 of the data sheet.

    The Binary2BCD() function just converts a binary number to it's BCD representation, and BCD2Binary() does the reverse.
     
  8. xed339

    Thread Starter New Member

    Feb 4, 2013
    13
    0
    Oh..So thats how it goes..Now i understand bit by bit.

    So the only purpose of these codes is to convert it

    Code ( (Unknown Language)):
    1.  
    2. [COLOR=Red]unsigned char BCD2LowerCh(unsigned char bcd)
    3. {
    4.   return ((bcd & 0x0F) + '0');
    5. }
    6.  
    7.  
    8. int Binary2BCD(int a)
    9. {
    10.    int t1, t2;
    11.    t1 = a%10;
    12.    t1 = t1 & 0x0F;
    13.    a = a/10;
    14.    t2 = a%10;
    15.    t2 = 0x0F & t2;
    16.    t2 = t2 << 4;
    17.    t2 = 0xF0 & t2;
    18.    t1 = t1 | t2;
    19.    return t1;
    20. }
    21.  
    22.  
    23. int BCD2Binary(int a)
    24. {
    25.    int r,t;
    26.    t = a & 0x0F;
    27.    r = t;
    28.    a = 0xF0 & a;
    29.    t = a >> 4;
    30.    t = 0x0F & t;
    31.    r = t*10 + r;
    32.    return r;
    33. }[/COLOR]
    34.  
    How about this sir? I'm not sure but i guess this is about the address, it store data to rtc address am I correct?The highlighted one

    Code ( (Unknown Language)):
    1.  
    2.    hour = BCD2Binary(hour);
    3.                     hour = hour + set;
    4.                     hour = Binary2BCD(hour);
    5.                     if((hour & 0x1F) >= 0x13)
    6.                     {
    7.                       [COLOR=red]hour = hour & 0b11100001;
    8.                       hour = hour ^ 0x20;[/COLOR]
    9.                     }
    10.  
     
    Last edited by a moderator: Feb 11, 2013
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    There's not much I can make out from that fragment. I have no idea where "set" came from.

    if((hour & 0x1F) >= 0x13)

    is true when the hour is 13 (1PM) or greater. While the next two lines use the bitwise logical AND (&) and OR (^) operators I have no idea why that is done.
    is seeing if the
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    Let me mention something. The second hardest task in programming is writing your own code from scratch and making it work.

    The very hardest task is taking someone else's code and making it work. It is orders of magnitude harder.

    While reading another's code is the only way I can think of to learn to write your own code you should limit this to well documented code written as a learning tool.
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Do you know about ASCII codes and how LCDs work :)
     
Loading...