还是电子钟的问题,用1602显示。出现乱码。大家帮忙看看。

[复制链接]
3759|3
 楼主| liaobiaoxing 发表于 2009-8-29 10:36 | 显示全部楼层 |阅读模式
本帖最后由 liaobiaoxing 于 2009-8-29 21:53 编辑

自己看了半天,终于找到错误了。显示正确了,谢谢回复贴子的人。


  1. #include<reg52.h>
  2. sbit E=P2^7;
  3. sbit RS=P2^5;
  4. sbit RW=P2^6;
  5. unsigned char show[8]={'0','0','-','0','0','-','0','0'};
  6. unsigned int count=0,sec=0,min=0,hour=0;

  7. void delay(unsigned char x)      
  8. {  unsigned int a,b;
  9.     for(a=0;a<=x;a++)
  10.     for(b=0;b<=10;b++);
  11. }

  12. void time0_init()
  13. {  EA=1;
  14.     ET0=1;
  15.     TMOD=0X01;
  16.     TH0=(65536-50000)/256;
  17.     TL0=(65536-50000)%256;
  18.     TR0=1;
  19. }


  20. void time0_int() interrupt 1
  21. {   TH0=(65536-50000)/256;
  22.      TL0=(65536-50000)%256;
  23.      count++;
  24.      if(count==10)
  25.       {  count=0;
  26.            sec++;
  27.           if(sec==60)
  28.              {  sec=0;
  29.                 min++;
  30.                   if(min==60)
  31.                     { min=0;
  32.                       hour++;
  33.                      if(hour==24)
  34.                        {  hour=0;
  35.                                       }
  36.                             }
  37.                       }
  38.               }

  39. show[0]=0x30+hour/10;
  40. show[1]=0x30+hour%10;
  41. show[2]='-';
  42. show[3]=0x30+min/10;
  43. show[4]=0x30+min%10;
  44. show[5]='-';
  45. show[6]=0x30+sec/10;
  46. show[7]=0x30+sec%10;
  47. }


  48. unsigned char convert(unsigned char input)      //由于板件的数据位跟P0口的数据位接反了,这是转换程序。
  49. {  unsigned char i,output=0,temp;
  50.    
  51.     for(i=0;i<=7;i++)
  52.        {  temp=(input>>i)&0x01;
  53.              output|=(temp<<(7-i));
  54.         }
  55.   return output;
  56. }


  57. void write_com(unsigned char com)
  58. {   RS=0;
  59.      RW=0;
  60.      P0=convert(com);
  61.      E=1;
  62.      delay(1);
  63.      E=0;
  64.      delay(1);
  65. }


  66. void write_data(unsigned char indata)
  67. {   RS=1;
  68.      RW=0;
  69.      P0=convert(indata);
  70.      E=1;
  71.      delay(1);
  72.      E=0;
  73.      delay(1);
  74. }

  75. void c1602_init()
  76. {  write_com(0x38);
  77.     write_com(0x0c);
  78.     write_com(0x06);
  79.     write_com(0x01);
  80. }

  81. void c1602_char(unsigned char x,unsigned char y,unsigned char charvaule)
  82. {  if(y==1)
  83.       write_com(0x80+x);
  84.   else
  85.    write_com(0xc0+x);
  86.    write_data(charvaule);
  87. }

  88. void c1602_str(unsigned char space,unsigned char hang,unsigned char *str)
  89. {    while(*str!='\0')
  90.      c1602_char(space++,hang,*str++);
  91. }

  92. void main()
  93. { c1602_init();
  94. time0_init();
  95.   while(1)
  96.   {
  97. c1602_str(3,1,show);
  98.    }
  99. }
一级菜鸟 发表于 2009-8-29 10:38 | 显示全部楼层
P0当IO口使用需要加上拉
 楼主| liaobiaoxing 发表于 2009-8-29 14:55 | 显示全部楼层
P0当IO口使用需要加上拉
一级菜鸟 发表于 2009-8-29 10:38

这个我知道的,已经加了上拉的。
一级菜鸟 发表于 2009-8-29 17:45 | 显示全部楼层
这个我知道的,已经加了上拉的。
liaobiaoxing 发表于 2009-8-29 14:55


那么就把你送进LCD的数转成ASC-II码再送
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

13

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部