找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索

Proteus仿真測量溫度l時(shí)lcd1602只亮不顯示溫度

查看數(shù): 2288 | 評(píng)論數(shù): 3 | 收藏 0
關(guān)燈 | 提示:支持鍵盤翻頁<-左 右->
    組圖打開中,請(qǐng)稍候......
發(fā)布時(shí)間: 2023-5-28 01:05

正文摘要:

#include"reg51.h" sbit RS=P3^0; sbit RW=P3^1; sbit E=P3^2; sbit DQ=P3^3; unsigned int readtemp=0; unsigned char str[]={"0123456789"}; unsigned char s[]={"Temperature:"}; void delay_18B20( ...

回復(fù)

ID:68189 發(fā)表于 2023-5-28 08:11
  1. void writedat(unsigned char dat)
  2. {
  3.         RS=1;
  4.         RW=0;
  5.         E=0;
  6.         P2=dat;
  7.         delay(5);
  8.         E=1;
  9.         E=0;
  10. }

  11. void writecom(unsigned char com)
  12. {
  13.         RS=0;
  14.         RW=0;
  15.         E=0;
  16.         P2=com;
  17.         delay(5);
  18.         E=1;
  19.         E=0;
  20. }
復(fù)制代碼

LCD1602的數(shù)據(jù)端口 與  單片機(jī)的驅(qū)動(dòng)端口  連接錯(cuò)誤。
代碼中,顯示驅(qū)動(dòng)端口是P2   實(shí)際仿真圖中卻沒有與P2連接。
P2.0 ---D0   P2.1-----D1   .......P2.7 ------ D7
ID:213173 發(fā)表于 2023-5-28 06:22
1.缺少函數(shù)ReadOneChar()2.LCD初始化函數(shù)名不符

3.顯示函數(shù)中 temp1、temp2沒有賦值
4.顯示固定字符代碼的位置不當(dāng)

5.仿真中1602數(shù)據(jù)接口連接與程序不符



  1. #include"reg51.h"
  2. sbit RS=P3^0;
  3. sbit RW=P3^1;
  4. sbit E=P3^2;
  5. sbit DQ=P3^3;
  6. unsigned int readtemp=0;
  7. unsigned char str[]={"0123456789"};
  8. unsigned char s[]={"Temperature:"};

  9. void delay_18B20(unsigned int i)
  10. {
  11.         for(;i>0;i--);
  12. }

  13. void Init_DS18B20(void)         
  14. {
  15.   unsigned char x=0;
  16.   DQ = 1;          //DQ??
  17.   delay_18B20(8);  //????
  18.   DQ = 0;          //DQ??
  19.   delay_18B20(80); //????480us
  20.   DQ = 1;          //????
  21.   delay_18B20(14);
  22.   x=DQ;            //?x=0?????,?x=1?????
  23.   delay_18B20(20);
  24. }
  25. void WriteOneChar(unsigned char dat)
  26. {
  27.         unsigned char i=0;
  28.         for (i=8; i>0; i--)
  29.         {
  30.                 DQ = 0;
  31.                 DQ = dat&0x01;
  32.                 if(DQ){delay_18B20(1);DQ=1;}
  33.                 else{delay_18B20(5);DQ = 1;}
  34.                 dat>>=1;
  35.         }
  36. }

  37. unsigned char ReadOneChar()
  38. {
  39.         unsigned char i=0;
  40.         unsigned char dat=0;
  41.         for(i=8;i>0;i--)
  42.         {
  43.                 DQ=0;
  44.                 dat>>=1;
  45.                 DQ=1;
  46.                 if(DQ)
  47.                         dat|=0x80;
  48.                 delay_18B20(4);
  49.         }
  50.         return (dat);
  51. }

  52. unsigned char ReadTemperature(void)
  53. {                                                         
  54.         unsigned char a=0,b=0;
  55.         unsigned int temp=0;
  56.         Init_DS18B20();
  57.         WriteOneChar(0xCC);  // ????????
  58.         WriteOneChar(0x44);  // ??????
  59.         delay_18B20(100);    //
  60.         Init_DS18B20();
  61.         WriteOneChar(0xCC);  //????????
  62.         WriteOneChar(0xBE);  //???????
  63.         delay_18B20(100);
  64.         a=ReadOneChar();     //?????
  65.         b=ReadOneChar();     //?????
  66.         temp=((b*256+a)>>4); //???????16??????
  67.         return(temp);
  68. }

  69. void delay(unsigned int t)
  70. {
  71.         unsigned int i=0,j=0;
  72.         for(i=0;i<t;i++)
  73.         {
  74.                 for(j=0;j<120;j++);
  75.         }
  76. }

  77. void writedat(unsigned char dat)
  78. {
  79.         RS=1;
  80.         RW=0;
  81.         E=0;
  82.         P2=dat;
  83.         delay(1);
  84.         E=1;
  85.         E=0;
  86. }

  87. void writecom(unsigned char com)
  88. {
  89.         RS=0;
  90.         RW=0;
  91.         E=0;
  92.         P2=com;
  93.         delay(1);
  94.         E=1;
  95.         E=0;
  96. }

  97. void initlcd()
  98. {
  99.         writecom(0x38);
  100.         writecom(0x0c);
  101.         writecom(0x06);
  102.         writecom(0x01);
  103. }

  104. void display()
  105. {
  106.         unsigned char temp0=0,temp1=0,temp2=0;
  107.         temp0=readtemp/100;
  108.         temp1=(readtemp%100)/10;
  109.         temp2=readtemp%10;
  110.         writecom(0x80+0x40+5);
  111.         writedat(str[temp0]);
  112.         delay(5);
  113.         writedat(str[temp1]);
  114.         delay(5);
  115.         writedat(str[temp2]);
  116.         delay(5);
  117.         writedat(0xdf);
  118.         delay(5);
  119.         writedat('C');
  120.         delay(5);
  121. }

  122. void main()
  123. {
  124.         unsigned char i;
  125.         initlcd();
  126.         writecom(0x80);
  127.         while(s[i]!='\0')
  128.         {
  129.                 writedat(s[i]);
  130.                 delay(5);
  131.                 i++;
  132.         }
  133.         while(1)
  134.         {
  135.                 readtemp=ReadTemperature();
  136.                 display();
  137.         }
  138. }
復(fù)制代碼



手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表