STC89C52读12887显示到液晶上每次上电秒钟都会清零

2019-07-15 22:22发布

  1. //1602
  2. #define        uchar unsigned char
  3. #define uint unsigned int
  4. sbit RW=P2^5;
  5. sbit RS=P2^6;
  6. sbit LE=P2^7;
  7. uchar code mun_to_char[] = {"0123456789ABCDEF"}; /*定义数字跟ASCII码的关系*/
  8. uchar data time_data_buff[7]={0x00,0x00,0x09,0x01,0x01,0x04,0x09};/*格式为: 秒 分 时 日 月 星期 年 */
  9. uchar data lcd1602_line1[]={"2000-00-00   000"};
  10. uchar data lcd1602_line2[]={"  00:00:00  00.0"};
  11. uchar code Weeks[][3]={{"SUN"},{"MON"},{"TUE"},{"WED"},{"THU"},{"FRI"},{"SAT"},{"SUN"}};
  12. void delay(uint z)
  13. {
  14.         uint x,y;
  15.         for(x=z;x>0;x--)
  16.                 for(y=110;y>0;y--);
  17. }
  18. void write_com(uchar date)
  19. {
  20.         RS=0;
  21.         RW=0;       
  22.         P0=date;
  23.         delay(5);
  24.         LE=1;
  25. //        delay(5);
  26.         LE=0;
  27. }
  28. void write_date(uchar date)
  29. {
  30.         RS=1;
  31.         RW=0;
  32.         P0=date;
  33.         delay(5);
  34.         LE=1;
  35. //        delay(5);
  36.         LE=0;
  37. }
  38. void RST1602()
  39. {
  40. //        LE=0;
  41.         write_com(0x38);
  42.         write_com(0x0C);
  43.         write_com(0x01);
  44.         write_com(0x06);       
  45. //        write_com(0x80);
  46. }
  47. void write_add(uchar x_pos,y_pos,date)
  48. {
  49.         x_pos&=0x0F;
  50.         y_pos&=0x01;
  51.         if(y_pos==1)x_pos+=0x40;
  52.         x_pos+=0x80;
  53.         write_com(x_pos);
  54.         write_date(date);
  55. }
  56. //12887
  57. sbit DScs=P3^0;
  58. sbit DSas=P3^1;
  59. sbit DSrw=P3^2;
  60. sbit DSds=P3^3;
  61. sbit DSirq=P3^4;
  62. sbit beep=P3^5;
  63. void write_ds12887(uchar add,uchar date)//写12887
  64. {
  65.         DScs=0;
  66.         DSas=1;
  67.         DSds=1;
  68.         DSrw=1;
  69.         P1=add;
  70.         DSas=0;
  71.         DSrw=0;
  72.         P1=date;
  73.         DSrw=1;
  74.         DSas=1;
  75.         DScs=1;
  76. }
  77. uchar read_ds12887(uchar add)         //读12887
  78. {
  79.         uchar date;       
  80.         //P1=0xFF;
  81.         DScs=0;
  82.         DSas=1;
  83.         DSds=1;
  84.         DSrw=1;
  85.         P1=add;
  86.         DSas=0;
  87.         DSds=0;       
  88.         date=P1;
  89.         DSds=1;
  90.         DSas=1;
  91.         DScs=1;
  92.         return date;               
  93. }
  94. void SET_12887()
  95. {
  96.         write_ds12887(0,0x00);//second       
  97.         write_ds12887(2,0x00);//minutes
  98.         write_ds12887(4,0x20);//hours
  99.         write_ds12887(6,0x03);//day
  100.         write_ds12887(7,0x25);//date
  101.         write_ds12887(8,0x09);//month
  102.         write_ds12887(9,0x13);//year
  103. }

  104. //主函数
  105. void main()
  106. {
  107.         uchar i;
  108.         RST1602();
  109.         write_ds12887(0x0B,0x63);
  110. //        SET_12887();       
  111.         while(1)
  112.         {
  113.                 lcd1602_line1[2]  = mun_to_char[read_ds12887(9)/0x10];
  114.                 lcd1602_line1[3]  = mun_to_char[read_ds12887(9)%0x10];  /*年*/
  115.                 lcd1602_line1[5]  = mun_to_char[read_ds12887(8)/0x10];
  116.                 lcd1602_line1[6]  = mun_to_char[read_ds12887(8)%0x10];  /*月*/
  117.                 lcd1602_line1[8]  = mun_to_char[read_ds12887(7)/0x10];
  118.                 lcd1602_line1[9] = mun_to_char[read_ds12887(7)%0x10];  /*日*/
  119.                 for(i=0;i<3;i++) lcd1602_line1[i+13]=Weeks[read_ds12887(6)&0x07][i]; /*星期*/
  120.                 lcd1602_line2[2]  = mun_to_char[read_ds12887(4)/0x10];
  121.                   lcd1602_line2[3]  = mun_to_char[read_ds12887(4)%0x10]; /*时*/
  122.                 lcd1602_line2[5]  = mun_to_char[read_ds12887(2)/0x10];
  123.                 lcd1602_line2[6]  = mun_to_char[read_ds12887(2)%0x10]; /*分*/
  124.                 lcd1602_line2[8]  = mun_to_char[read_ds12887(0)/0x10];
  125.                 lcd1602_line2[9]  = mun_to_char[read_ds12887(0)%0x10]; /*秒*/
  126.                 for(i=0;i<16;i++) write_add(i,0,lcd1602_line1[i]);
  127.             for(i=0;i<16;i++) write_add(i,1,lcd1602_line2[i]);       
  128.         }                 
  129. }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。