找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 13421|回復(fù): 6
收起左側(cè)

基于單片機的溫度傳感器DS18B20顯示溫度 仿真圖,流程圖,源程序,文檔

[復(fù)制鏈接]
ID:438970 發(fā)表于 2018-12-4 09:42 | 顯示全部樓層 |閱讀模式
這是我?guī)字芮白龅膯纹瑱C實驗,用LCD液晶顯示溫度傳感器的溫度
其中包含仿真,源程序,流程圖等

單片機源程序如下:
  1. uint temp;
  2. float f_temp;
  3. uchar code table1[] = {"DATA:2014-7-10"};
  4. uchar code table2[] = {"Temperature:xx c"};
  5. uchar code table3[] = {"0123456789"};

  6. void delay(uint xms)
  7. {                           // 延時函數(shù)
  8.         uint i,j;
  9.         for(i = xms;i > 0;i--)
  10.                 for(j = 110;j > 0;j--);
  11. }
  12. /*******************************************************************/      
  13. /*                                                                                                                                 
  14. /*                  DS18B20溫度傳感器模塊                                    
  15. /*                                                                        
  16. /*******************************************************************/
  17. void dsreset(void)    //18B20復(fù)位,初始化函數(shù)
  18. {
  19.   uint i;
  20.   ds=0;
  21.   i=103;
  22.   while(i>0)i--;
  23.   ds=1;
  24.   i=4;
  25.   while(i>0)i--;
  26. }

  27. bit tempreadbit()   //讀1位數(shù)據(jù)函數(shù)
  28. {
  29.    uint i;
  30.    bit dat;
  31.    ds=0;i++;          //i++ 起延時作用
  32.    ds=1;i++;i++;
  33.    dat=ds;
  34.    i=8;while(i>0)i--;
  35.    return dat;
  36. }

  37. uchar tempread()   //讀1個字節(jié)
  38. {
  39.   uchar i,j,dat;
  40.   dat=0;
  41.   for(i=1;i<=8;i++)
  42.   {
  43.     j=tempreadbit();
  44.     dat=(j<<7)|(dat>>1);   //讀出的數(shù)據(jù)最低位在最前面,這樣剛好一個字節(jié)在DAT里
  45.   }
  46.   return dat;
  47. }

  48. void tempwritebyte(uchar dat)   //向DS18B20寫一個字節(jié)數(shù)據(jù)函數(shù)
  49. {
  50.   uint i;
  51.   uchar j;
  52.   bit testb;
  53.   for(j=1;j<=8;j++)
  54.   {
  55.     testb=dat&0x01;
  56.     dat=dat>>1;
  57.     if(testb)     //寫 1
  58.     {
  59.       ds=0;
  60.       i++;i++;
  61.       ds=1;
  62.       i=8;while(i>0)i--;
  63.     }
  64.     else
  65.     {
  66.       ds=0;       //寫 0
  67.       i=8;while(i>0)i--;
  68.       ds=1;
  69.       i++;i++;
  70.     }

  71.   }
  72. }
  73. void tempchange(void)   //DS18B20 開始獲取溫度并轉(zhuǎn)換
  74. {
  75.   dsreset();
  76.   delay(1);
  77.   tempwritebyte(0xcc);  // 寫跳過讀ROM指令
  78.   tempwritebyte(0x44);  // 寫溫度轉(zhuǎn)換指令
  79. }
  80. uint get_temp()         //讀取寄存器中存儲的溫度數(shù)據(jù)函數(shù)
  81. {
  82.   uchar a,b;

  83.   dsreset();
  84.   delay(1);
  85.   tempwritebyte(0xcc);
  86.   tempwritebyte(0xbe);
  87.   a=tempread();         //讀低8位
  88.   b=tempread();         //讀高8位
  89.   temp=b;
  90.   temp<<=8;            //兩個字節(jié)組合為1個字
  91.   temp=temp|a;
  92.   f_temp=temp*0.0625;      //溫度在寄存器中為12位 分辨率位0.0625°
  93.   temp=f_temp*10+0.5; //乘以10表示小數(shù)點后面只取1位,加0.5是四舍五入
  94.   f_temp=f_temp+0.05;
  95.   return temp;         //temp是整型
  96. }
  97. /*******************************************************************/      
  98. /*                                                                                                                                 
  99. /*                      1602液晶模塊                                    
  100. /*                                                                        
  101. /*******************************************************************/                                                                                                        
  102. void lcd_wcmd(uchar cmd)   // 寫入指令數(shù)據(jù)到LCD
  103. {                          //RS=L,RW=L,E=高脈沖,D0-D7=指令碼。
  104.         LCD_RS = 0;
  105.         LCD_RW = 0;
  106.         LCD_EP = 0;
  107.         P0 = cmd;
  108.         delay(1);
  109.         LCD_EP = 1;
  110.         delay(1);
  111.         LCD_EP = 0;  
  112. }  
  113. void lcd_wdata(uchar dat)  //寫入字符顯示數(shù)據(jù)到LCD
  114. {                          //RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)
  115.         LCD_RS = 1;
  116.         LCD_RW = 0;
  117.         LCD_EP = 0;
  118.         P0 = dat;
  119.         delay(1);
  120.         LCD_EP = 1;
  121.         delay(1);
  122.         LCD_EP = 0;
  123. }
  124. void lcd_init()                          //LCD初始化設(shè)定初始化函數(shù)
  125. {                        
  126.         lcd_wcmd(0x38);       //16*2顯示,5*7點陣,8位數(shù)據(jù)
  127.         delay(1);                        
  128.         lcd_wcmd(0x0c);       //設(shè)置開顯示,不顯示光標
  129.         delay(1);
  130.         lcd_wcmd(0x06);       //寫一個字符后地址指針加1
  131.         delay(1);
  132.         lcd_wcmd(0x01);       //顯示清0,數(shù)據(jù)指針清0
  133.         delay(1);
  134. }
  135. /*******************************************************************/      
  136. /*                                                                                                                                 
  137. /*                         主函數(shù)                                    
  138. /*                                                                        
  139. /*******************************************************************/
  140. void main()
  141. {
  142.         uchar i;
  143.           beep = 0;                                 //蜂鳴器默認處于開啟狀態(tài),所以關(guān)閉它
  144.         lcd_init();                                 //LCD初始化
  145.         delay(10);
  146.         i = 0;
  147.         lcd_wcmd(0x80);                 //第1行第1列
  148.         while(table1[i] != '\0') //按鍵1對應(yīng)的字樣
  149.            {                                 
  150.                lcd_wdata(table1[i]);
  151.                i++;           
  152.     }
  153.         i = 0;
  154.         lcd_wcmd(0x80+0x40);         //第2行第1列
  155.         while(table2[i] != '\0') //按鍵1對應(yīng)的字樣
  156.            {                                 
  157.                lcd_wdata(table2[i]);
  158.                i++;           
  159.     }
  160.         lcd_wcmd(0x80+0x4e);         // 設(shè)置顯示位置為第二行第13個字符
  161.         lcd_wdata(0xdf);             // 顯示字符°        
  162.     while(1)
  163.          {   
  164.             tempchange();
  165.                 lcd_wcmd(0x80+0x4c);
  166.                 lcd_wdata(table3[get_temp()/100]);           //溫度的十位
  167.                 lcd_wdata(table3[get_temp()%100/10]);  //溫度的個位                           
  168.          }
  169. }
復(fù)制代碼

基于單片機液晶顯示溫度.docx (394.6 KB, 下載次數(shù): 210)

仿真圖

仿真圖
image2.jpeg

拆分程序

拆分程序
回復(fù)

使用道具 舉報

ID:764811 發(fā)表于 2020-5-30 10:05 | 顯示全部樓層
能發(fā)一下剩下的程序嗎?
回復(fù)

使用道具 舉報

ID:742391 發(fā)表于 2020-5-31 10:32 | 顯示全部樓層
我下載了沒仿真啊能發(fā)一下仿真么?
回復(fù)

使用道具 舉報

ID:682190 發(fā)表于 2020-6-16 08:53 | 顯示全部樓層
怎么沒有整套的仿真圖
回復(fù)

使用道具 舉報

ID:1081907 發(fā)表于 2023-6-4 14:28 | 顯示全部樓層
能發(fā)一下整套程序嗎  謝謝
回復(fù)

使用道具 舉報

ID:915969 發(fā)表于 2023-6-5 14:51 | 顯示全部樓層
1731362105@qq.c 發(fā)表于 2020-5-30 10:05
能發(fā)一下剩下的程序嗎?

加一
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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