1. Dumken

    Thread Starter New Member

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

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    No colors. Too bad, so sad.

    The only thing that jumped out at me is time[10] is initialized as 'M' is never anything different than 'M' and thus never needs to be changed ever. ;)

    Code tags are great but I think they kill colors. Either name the routine or make a source listing with line numbers and note the numbers you need help with.
     
Loading...