找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1549|回复: 0
打印 上一主题 下一主题
收起左侧

有人知道1602和点阵能一起用P0口吗?为什么我的1602没有显示?而且点阵只显示初始化

[复制链接]
跳转到指定楼层
楼主
ID:272089 发表于 2018-10-18 14:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
10黑币
  1. #include <regx51.h>
  2. #include <intrins.h>
  3. #include <absacc.h>
  4. #define uchar unsigned char
  5. #define uint unsigned int

  6. #define PA XBYTE[0x0000]
  7. #define PB XBYTE[0x2000]
  8. #define PC XBYTE[0x4000]
  9. #define COM XBYTE[0x6000]

  10. void delay(uint i){ while(--i);}

  11. uchar num1=0,num2=0;
  12. sbit DQ=P2^3;
  13. sbit jr=P2^4;
  14. sbit row0=P3^0;sbit row1=P3^1;sbit col0=P3^2;
  15. sbit col1=P3^3;sbit col2=P3^4;sbit col3=P3^5;


  16. sbit RS=P2^0; //1=数据;0=命令
  17. sbit RW=P2^1; //1=读;0=写
  18. sbit E=P2^2;  //1=使能;0=禁止
  19. #define Dat1602 P0 /** 数据端口 **/
  20. sbit BF=Dat1602^7; /** 忙信号线 **/

  21. uchar code zm[][8]={
  22. 0x1C,0x22,0x32,0x2A,0x26,0x22,0x1C,0x00, // -0-
  23. 0x08,0x0C,0x08,0x08,0x08,0x08,0x1C,0x00, // -1-
  24. 0x1C,0x22,0x20,0x18,0x04,0x02,0x3E,0x00, // -2-
  25. 0x3E,0x20,0x10,0x18,0x20,0x22,0x1C,0x00, // -3-
  26. 0x10,0x18,0x14,0x12,0x3E,0x10,0x10,0x00, // -4-
  27. 0x3E,0x02,0x1E,0x20,0x20,0x22,0x1C,0x00, // -5-
  28. 0x38,0x04,0x02,0x1E,0x22,0x22,0x1C,0x00, // -6-
  29. 0x3E,0x20,0x10,0x08,0x04,0x04,0x04,0x00, // -7-
  30. 0x1C,0x22,0x22,0x1C,0x22,0x22,0x1C,0x00, // -8-
  31. 0x1C,0x22,0x22,0x3C,0x20,0x10,0x0E,0x00, // -9-
  32. 0x00,0x00,0x04,0x08,0xDE,0x90,0x88,0x9C,        //初始化       
  33. 0x00,0x00,0x00,0x00,0x87,0x44,0xF4,0xA4,                               
  34. 0x00,0x00,0x00,0x10,0x08,0x24,0x7D,0x00,                               
  35. 0x00,0x00,0x04,0x12,0x93,0x52,0x32,0x1A,                          
  36. 0xAA,0x88,0x48,0x28,0x08,0x00,0x00,0x00,                       
  37. 0x44,0xA4,0x04,0x06,0x04,0x00,0x00,0x00,                       
  38. 0x3C,0x24,0x24,0x3C,0x00,0x00,0x00,0x00,                       
  39. 0x12,0x12,0x12,0x92,0xE2,0x00,0x00,0x00,                       
  40. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,        //不亮         18
  41. 0x02,0xF5,0x0A,0x08,0x08,0x08,0x08,0xF0,    //℃         19
  42. };

  43. uchar key(void)
  44. {
  45.     uchar k=25,n;
  46.     COM=0x8a;
  47.         PC=0xfe;
  48.         n=PC;
  49.         n&=0xf0;
  50.         if(n!=0xf0)
  51.         {
  52.             switch(n)
  53.                 {
  54.                     case 0xe0:k=0;break;
  55.                         case 0xd0:k=1;break;
  56.                         case 0xb0:k=2;break;
  57.                         case 0x70:k=3;break;
  58.                 }
  59.                 while(PC!=0xfe);
  60.         }
  61.         else k=25;
  62.         return k;
  63. }

  64. //初始化DS18B20,成功返回0,失败返回1
  65. bit STA_1820(void){
  66.     bit ack;
  67.         DQ=1;delay(5);
  68.         DQ=0;delay(80);           //拉低总线480~960μS
  69.         DQ=1;delay(5);           //拉高等待15~60μS
  70.     ack=DQ;delay(50);  //读应答位并延时480μS以上
  71.     return ack;       
  72. }
  73. //读DS18B20一字节,返回读到的数
  74. char Read_1820(void){
  75.         unsigned char i,dat=0;
  76.         for(i=0;i<8;i++){
  77.                 DQ=0; dat>>=1;         //拉低,右移兼延时
  78.                 DQ=1; if(DQ)dat|=0x80; //拉高,读数据
  79.                 delay(7);                           //延时使总时间>60μS
  80.         }
  81.         return(dat);
  82. }
  83. //向DS18B20写一字节,参数为要写的数
  84. void Write_1820(unsigned char dat){
  85.         unsigned char i;
  86.         for(i=0;i<8;i++)
  87.     {
  88.                 DQ=0;                                  //拉低开始
  89.                 DQ=dat&0x01; delay(7);//送数并保持60μS以上
  90.                 DQ=1; dat>>=1;                  //拉高,右移准备下一位
  91.         }
  92. }
  93. //启动温度转换
  94. void startTemperature(){
  95.     if(STA_1820())return;
  96.     Write_1820(0xcc);
  97.     Write_1820(0x44);            
  98. }
  99. //读温度
  100. int readTemperature(void) {
  101.     unsigned char TCL,TCH;
  102.     STA_1820();
  103.     Write_1820(0xcc);
  104.     Write_1820(0xbe);
  105.     TCL = Read_1820();
  106.     TCH = Read_1820();
  107.     return (TCH*0x100+TCL)*0.0625;
  108.    
  109. }

  110. uchar buf[8];

  111. void dis(){
  112.         uchar i,n;
  113.         uint row=0x01;
  114.         for(i=0;i<16;i++){
  115.                 n=i<8?0:1;
  116.                 P0=zm[buf[0+4*n]][i-8*n];         col0=1;col0=0;
  117.                 P0=zm[buf[1+4*n]][i-8*n];        col1=1;col1=0;
  118.                 P0=zm[buf[2+4*n]][i-8*n];        col2=1;col2=0;
  119.                 P0=zm[buf[3+4*n]][i-8*n];        col3=1;col3=0;
  120.                 P0=row%0x100;        row0=1;row0=0;
  121.                 P0=row/0x100;        row1=1;row1=0;
  122.                 delay(50);
  123.                 row=_irol_(row,1);
  124.                 P0=0;
  125.                 row0=row1=col0=col1=col2=col3=1;
  126.                 row0=row1=col0=col1=col2=col3=0;               
  127.         }
  128. }

  129. /** LCM忙检测 **/
  130. bit busy1602(){
  131.         bit busy=0;           //暂存标志
  132.         Dat1602=0xff;  //口线置1,防止干扰
  133.         RS=0; RW=1;           //置“命令、读”模式
  134.         E=1;  E=1;     //使能,写两次做短延时
  135.         busy=BF;E=0;   //暂存标志,关闭使能
  136.         return busy;   //返回忙信号,1=忙
  137. }
  138. /** 写命令 参数:comDat为要发送的命令 **/
  139. void WriteComm(uchar comDat){  
  140.     while (busy1602());     //忙,则等待
  141.         RS=0;RW=0;                                //置“命令、写”模式
  142.     Dat1602=comDat;E=1;E=0;        //送出命令,并使之有效
  143. }
  144. /** 写数据 参数:dat为要发送的数据 **/
  145. void WriteData(uchar dat){  
  146.     while (busy1602());  //忙,则等待
  147.         RS=1;RW=0;                         //置“数据、写”模式
  148.     Dat1602=dat;E=1;E=0; //送出数据,并使之有效
  149. }
  150. void Init1602(){
  151.         WriteComm(0x38);//8位,两行,5*7点阵
  152.         WriteComm(0x0c);//显示开,光标关
  153.         WriteComm(0x06);//默认,光标右移
  154.         WriteComm(1);        //清显示
  155. }

  156. uint  i=55500,j;
  157. uint k;
  158. bit z=0,x=0,c=0,v=0;

  159. void init()
  160. {
  161.             
  162.           
  163.           buf[0]=10;buf[1]=11;buf[2]=12;buf[3]=13;                                         //显示初始化
  164.           buf[4]=14;buf[5]=15;buf[6]=16;buf[7]=17;
  165.           
  166.           

  167.           Init1602();
  168.           WriteComm(0x80+4);//光标到第1行,第6位
  169.           WriteData('W');WriteData('e');WriteData('l');WriteData('c');
  170.           WriteData('o');WriteData('m');WriteData('e');WriteData('!');
  171.                                
  172.           WriteComm(1);      //清屏
  173.           WriteComm(0x80+1);//光标到第1行,第2位
  174.           WriteData('W');WriteData(':');WriteData(' ');WriteData(num1/10%10+'0');WriteData(num1%10+'0');WriteData(' ');
  175.           WriteData('Y');WriteData(':');WriteData(' ');WriteData(num2/10%10+'0');WriteData(num2%10+'0');
  176.           WriteComm(0xc0+1);//光标到第2行,第6位
  177.           WriteData('Z');WriteData('t');WriteData(':');
  178.           WriteData('S');WriteData('t');WriteData('a');WriteData('y');WriteData(' ');
  179.           WriteData('h');WriteData('o');WriteData('t');
  180.           while(i>0){dis();i--;}
  181. }
  182. void main(){
  183.       COM=0X8A;
  184.           TMOD=0x01;
  185.           EA=ET0=TR0=1;
  186.           init();       
  187.           while(1){
  188.                 EA=0; startTemperature(); EA=1;            //启动温度转换
  189.                     EA=0; j=readTemperature();EA=1;                   //读温度值
  190.                 buf[0]=j/10;buf[1]=j%10;buf[2]=19;buf[3]=18;
  191.                 buf[4]=18;buf[5]=18;buf[6]=18;buf[7]=18;

  192.             dis();                       

  193.                      if(key()==0){c=1;x=0;break;}
  194.                         if(key()==1){
  195.                                   WriteComm(1);
  196.                                           buf[0]=18;buf[1]=18;buf[2]=18;buf[3]=18;
  197.                           buf[4]=18;buf[5]=18;buf[6]=18;buf[7]=18;
  198.                                           dis();
  199.                                           x=1;
  200.                         }
  201.                         if(key()==2){
  202.                                            WriteComm(0xc0+4);//光标到第2行,第1位
  203.                            WriteData('P');WriteData('a');WriteData('u');WriteData('s');WriteData('e');WriteData(' ');
  204.                                            WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');
  205.                                            v=1;z=0;
  206.                         }
  207.                         if(key()==3){
  208.                                    WriteComm(0xc0+4);//光标到第2行,第1位
  209.                            WriteData('R');WriteData('u');WriteData('n');WriteData(' ');WriteData(' ');WriteData(' ');
  210.                                            WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');
  211.                                            z=1;v=0;
  212.                         }
  213.                         if(j<45&&z==1&&v==0){jr=0;}
  214.                         else jr=1;
  215.           }       
  216. }

  217. void TIME0() interrupt 1
  218. {
  219.     TH0=0xfc;
  220.         TL0=0x17;
  221.         //dis();
  222. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表