RTC12C887 Interfacing Error

Discussion in 'Embedded Systems and Microcontrollers' started by scintrillating, Dec 30, 2011.

  1. scintrillating

    Thread Starter New Member

    Dec 30, 2011
    5
    0
    I have written the code in C Language to build a Digital Clock using DS12C887 and a 16*2 LCD...But I am facing a problem in getting the exact output...

    Initially as i set time i get output as (HH:MM:SS format)
    16:58:01
    and get the outputs as

    16:58:02
    03:03:03
    58:58:04
    and so on but not in unique pattern...


    The out put is very much unpredictable...please help me out

    The code for the program appears as below...


    Code ( (Unknown Language)):
    1.  
    2. #include<reg51.h>
    3. #include<absacc.h>
    4.  
    5. #define port P3
    6. #define lcd_dataport P2  // lcd_dataport for lcd
    7. #define keypad P1  // Port for keypadpad
    8. #define timeport P0 // Port for RTC
    9.  
    10. sbit rs = port^0;
    11. sbit rw = port^1;
    12. sbit en = port^2;
    13.  
    14. sbit key0=keypad^0;
    15. sbit key1=keypad^1;
    16. sbit key2=keypad^2;
    17. sbit key3=keypad^3;
    18. sbit key4=keypad^4;
    19. sbit key5=keypad^5;
    20. sbit key6=keypad^6;
    21. sbit key7=keypad^7;
    22.  
    23. int hr, min, sec;
    24.  
    25. void delay(unsigned int msec) //Time delay function
    26. {
    27. int i,j ;
    28. for(i=0;i<msec;i++)
    29. for(j=0;j<1275;j++);
    30. }
    31.  
    32. void lcdcmd(unsigned char item) //Function to send command to LCD
    33. {
    34. lcd_dataport = item;
    35. rs= 0;
    36. rw=0;
    37. en=1;
    38. delay(1);
    39. en=0;
    40. return;
    41. }
    42.  
    43. void lcddata(unsigned char item) //Funtion to send data on LCD
    44. {
    45. lcd_dataport = item;
    46. rs= 1;
    47. rw=0;
    48. en=1;
    49. delay(1);
    50. en=0;
    51. }
    52.  
    53. void lcddata_string(unsigned char *str) // Function to send string on LCD
    54. {
    55. int i=0;
    56. while(str[i]!='\0')
    57. {
    58. lcddata(str[i]);
    59. i++;
    60. delay(1);
    61. }
    62. }
    63.  
    64. void lcd(unsigned char str[10])  // Funtion to Initialize LCD
    65. {
    66. lcdcmd(0x38);
    67. lcdcmd(0x0C);
    68. lcdcmd(0x01);
    69. lcdcmd(0x80);
    70. lcddata_string(str);
    71. }
    72.  
    73. void check_key()
    74. {
    75. if(key0==0)
    76. lcd("SET TIME");
    77. if(key1==0)
    78. lcd("Increment");
    79. if(key2==0)
    80. lcd("Decrement");
    81. if(key3==0)
    82. lcd("SET A");
    83. if(key4==0)
    84. lcd("SET B");
    85. if(key5==0)
    86. lcd("SET C");
    87. if(key6==0)
    88. lcd("SET BANGS");
    89. if(key7==0)
    90. lcd("SET GROUPS");
    91. }
    92.  
    93. void bcdconv(unsigned char mybyte)
    94. {
    95. unsigned char x,y;
    96. x=mybyte&0x0F;
    97. x=x|0x30;
    98. y=mybyte&0xF0;
    99. y=y>>4;
    100. y=y|0x30;
    101. lcddata(y);
    102. lcddata(x);
    103. }
    104.  
    105. void main()
    106. {
    107.         delay(220);
    108.         XBYTE[10]=0x20;
    109.         XBYTE[11]=0x82;
    110.         lcd("TIME:");
    111.         while(1)
    112.         {
    113.         lcdcmd(0x88);
    114.         hr=XBYTE[4];
    115.         bcdconv(hr);
    116.         lcddata(':');
    117.         min=XBYTE[2];
    118.         bcdconv(min);
    119.         lcddata(':');
    120.         sec=XBYTE[0];
    121.         bcdconv(sec);
    122.         lcdcmd(0x02);
    123.         }
    124. }
    125.  
    126. [/i][/i]
     
    Last edited by a moderator: Dec 30, 2011
Loading...