久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6496|回復(fù): 4
打印 上一主題 下一主題
收起左側(cè)

分享:LCD1602溫度檢測顯示程序(ATmega16)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:114509 發(fā)表于 2018-12-17 16:50 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
LCD1602溫度檢測顯示程序(ATmega16)

單片機(jī)源程序如下:

  1. //**********************************************************************//
  2. //*************************      頭文件定義       **********************//
  3. //**********************************************************************//
  4. #include<iom16v.h>
  5. #include <macros.h>

  6. //宏定義
  7. #define uchar unsigned char
  8. #define uint unsigned int

  9. //溫度18b20(數(shù)據(jù)線端口)
  10. #define tmp (PINB&BIT(PB3))
  11. #define temp_h PORTB |= BIT(PB3)
  12. #define temp_l PORTB &=~BIT(PB3)
  13. #define temp_o DDRB |= BIT(PB3)
  14. #define temp_i DDRB &=~BIT(PB3)

  15. //LCD1602液晶顯示(數(shù)據(jù)線端口)
  16. #define rs_h PORTB |= BIT(PB0)//數(shù)據(jù)/命令選擇
  17. #define rs_l PORTB &=~BIT(PB0)
  18. #define rw_h PORTB |= BIT(PB1)//讀/寫選擇
  19. #define rw_l PORTB &=~BIT(PB1)
  20. #define en_h PORTB |= BIT(PB2)//使能信號
  21. #define en_l PORTB &=~BIT(PB2)

  22. //溫度18b20(變量定義)
  23. unsigned char dat1=0x00;//保存讀出的溫度 L
  24. unsigned char dat2=0x00;//保存讀出的溫度 H
  25. unsigned long int dat=0;//保存讀出的溫度 XS
  26. unsigned char flag=0;//錯誤標(biāo)志位
  27. //按鍵定義
  28. unsigned char key1=0;
  29. unsigned char key2=0;
  30. //unsigned char key3=0;
  31. //unsigned char key4=0;
  32. //返回值變量
  33. unsigned char keyvalue=0;
  34. //溫度H
  35. unsigned char tempH=30;
  36. //溫度L
  37. unsigned char tempL=20;

  38. //**********************************************************************//
  39. //*************************      IO 端口定義      **********************//
  40. //**********************************************************************//

  41. void IO_init(void)
  42. {         
  43.          DDRA = 0XFF;
  44.          DDRB = 0XF0;
  45.          DDRC = 0XFF;
  46.          DDRD = 0XFF;
  47.         PORTA = 0X00;
  48.         PORTB = 0X00;
  49.         PORTC = 0XFF;
  50.         PORTD = 0XFF;
  51. }

  52. //**********************************************************************//
  53. //*************************      延時函數(shù)         **********************//
  54. //**********************************************************************//

  55. void delayms(uint z)          //8M晶振下,延時1ms
  56. {
  57.          uint x,y;
  58.          for(x=z;x>0;x--)
  59.                   for(y=1333;y>0;y--);
  60. }

  61. //**********************************************************************//
  62. //***************************      18B20        ************************//
  63. //**********************************************************************//

  64. void Ds18b20_reset(void)//DS18B20初始化
  65. {
  66.         uint count;
  67.         temp_o;
  68.         temp_l;
  69.         for(count=700;count>0;count--);//延時480us
  70.         temp_h;
  71.         temp_i;//不須配置PORT內(nèi)部上拉電阻,MCU輸入輸出自動切換
  72.         while((tmp==0x08));//&&(i>0)) i--;
  73.         for(count=700;count>0;count--);//延時480us
  74. }
  75. void Ds18b20_write(uchar dat)//向DS18B20寫一個字節(jié)
  76. {
  77.         uchar count;
  78.         uchar i;
  79.         temp_o;
  80.         for(i=8;i>0;i--)
  81.         {
  82.                 temp_l;
  83.                 for(count=2;count>0;count--);
  84.                 //temp_h;//不能有此語句
  85.                 if(dat&0x01==0x01)
  86.                         temp_h;
  87.                 else
  88.                         temp_l;
  89.                 for(count=120;count>0;count--);//延時60us
  90.                 temp_h;
  91.                 dat>>=1;        
  92.         }        
  93. }
  94. uchar Ds18b20_read(void)//從DS18B20讀一個字節(jié)
  95. {
  96.         uchar i,datt;
  97.         uchar count;
  98.         for(i=8;i>0;i--)
  99.         {
  100.                 datt>>=1;
  101.                 temp_o;
  102.                 temp_l;
  103.                 for(count=2;count>0;count--);
  104.                 temp_h;//此語句必須有,參考datasheet的P15
  105.                 for(count=1;count>0;count--);
  106.                 temp_i;
  107.                 if(tmp==0x08)
  108.                         datt|=0x80;
  109.                 for(count=120;count>0;count--);        //延時60us
  110.         }
  111.         return datt;
  112. }
  113. void temp_Read(void)//溫度讀取
  114. {
  115.          Ds18b20_reset();//DS18B20初始化
  116.         Ds18b20_write(0xcc);//跳過ROM
  117.         Ds18b20_write(0x44);//發(fā)送溫度轉(zhuǎn)換命令
  118.         delayms(1000);//延時1s,等待溫度轉(zhuǎn)換完成
  119.         Ds18b20_reset();//DS18B20初始化
  120.         Ds18b20_write(0xcc);//跳過ROM
  121.         Ds18b20_write(0xbe);//發(fā)送讀溫度寄存器命令
  122.         dat1=Ds18b20_read();//讀溫度值的低字節(jié)
  123.         dat2=Ds18b20_read();//讀溫度值的高字節(jié)
  124. }
  125. void temp_display(void)//溫度顯示
  126. {
  127.         if(dat2>=240)//dat2溫度值的高字節(jié)為1時為負(fù)溫度
  128.         {
  129.                 dat=(~(dat2*256+dat1)+1)*0.625;//負(fù)溫度:取反加一,保留一位小數(shù)
  130.                 flag=1;
  131.         }
  132.         else
  133.         {
  134.                 dat=(dat2*256+dat1)*0.625;
  135.                 flag=0;
  136.         }
  137.         if(flag==1)//負(fù)溫度顯示
  138.         {
  139.                  LCD_write_str(0,0," 18B20 ");
  140.                 LCD_write_str(3,1,"Temp:");
  141.                 LCD_write_str(8,1,"-");// 符號“- ”
  142.                 LCD_write_char(9,1,0x30+dat/1000);
  143.                 LCD_write_char(10,1,0x30+dat%1000/100);
  144.                 LCD_write_char(11,1,0x30+dat%100/10);
  145.                 LCD_write_str (12,1,".");// 符號“. ”
  146.                 LCD_write_char(13,1,0x30+dat%10);        
  147.         }
  148.         if(flag==0)//正溫度顯示
  149.         {           
  150.                 LCD_write_str(0,0," 18B20 ");
  151.                 LCD_write_str(3,1,"Temp:");
  152.                 LCD_write_str(8,1," ");// 符號“+ ”
  153.                 LCD_write_char(9,1,0x30+dat/1000);
  154.                 LCD_write_char(10,1,0x30+dat%1000/100);
  155.                 LCD_write_char(11,1,0x30+dat%100/10);
  156.                 LCD_write_str (12,1,".");// 符號“. ”
  157.                 LCD_write_char(13,1,0x30+dat%10);
  158.         }        
  159. }
  160. void tempH_Setting(void)//最高溫度設(shè)置顯示
  161. {
  162.          LCD_write_str(0,0," temp(H)Setting ");
  163.         LCD_write_char(6,1,0x30+tempH%1000/100);
  164.         LCD_write_char(7,1,0x30+tempH%100/10);
  165.         LCD_write_char(8,1,0x30+tempH%10);
  166. }
  167. void tempL_Setting(void)//最低溫度設(shè)置顯示
  168. {
  169.          LCD_write_str(0,0," temp(L)Setting ");
  170.         LCD_write_char(6,1,0x30+tempL%1000/100);
  171.         LCD_write_char(7,1,0x30+tempL%100/10);
  172.         LCD_write_char(8,1,0x30+tempL%10);
  173. }
  174. void temp_police(void)//溫度報警
  175. {
  176.         if(dat/10>=tempH)//最高檢測溫度>=設(shè)定溫度:燈亮
  177.         {
  178.                  PORTC&=~BIT(7);
  179.         }
  180.         else
  181.         {
  182.                  PORTC|= BIT(7);
  183.         }
  184.         if(dat/10<=tempL)//最低檢測溫度<=設(shè)定溫度:燈亮
  185.         {
  186.                  PORTC&=~BIT(6);
  187.         }
  188.         else
  189.         {
  190.                  PORTC|= BIT(6);
  191.         }
  192. }

  193. //**********************************************************************//
  194. //***************************      LCD1602      ************************//
  195. //**********************************************************************//

  196. void LCD_init(void)//LCD顯示屏初始化函數(shù)
  197. {
  198.         DDRA = 0xFF;                                                    //I/O口方向設(shè)置
  199.         DDRB|=BIT(PB0)|BIT(PB1)|BIT(PB2);
  200.         delayms(15);                           //上電延時一段時間,使供電穩(wěn)定
  201.         Write_Instruction(0x38);                                //8bit interface,2line,5*7dots
  202.         delayms(5);
  203.         Write_Instruction(0x38);        
  204.         delayms(5);
  205.         Write_Instruction(0x38);        
  206.         Write_Instruction(0x08);        //關(guān)顯示,不顯光標(biāo),光標(biāo)不閃爍
  207.         Write_Instruction(0x01);        //清屏
  208.         delayms(5);
  209.         Write_Instruction(0x04);        //寫一字符,整屏顯示不移動
  210.         //Write_Instruction(0x05);        //寫一字符,整屏右移
  211.         //Write_Instruction(0x06);        //寫一字符,整屏顯示不移動
  212.         //Write_Instruction(0x07);        //寫一字符,整屏左移
  213.         delayms(5);
  214.         //Write_Instruction(0x0B);        //關(guān)閉顯示(不顯示字符,只有背光亮)
  215.         Write_Instruction(0x0C);        //開顯示,光標(biāo)、閃爍都關(guān)閉
  216.         //Write_Instruction(0x0D);        //開顯示,不顯示光標(biāo),但光標(biāo)閃爍
  217.         //Write_Instruction(0x0E);        //開顯示,顯示光標(biāo),但光標(biāo)不閃爍
  218.         //Write_Instruction(0x0F);        //開顯示,光標(biāo)、閃爍均顯示
  219. }
  220. void lcd_en(void)        //en端產(chǎn)生一個高電平脈沖,控制LCD寫時序
  221. {
  222.         delayms(1);
  223.         en_h;
  224.     delayms(1);
  225.     en_l;
  226. }
  227. void LCD_clear(void)//清屏函數(shù)
  228. {
  229.   Write_Instruction(0x01);
  230.   delayms(5);
  231. }
  232. void Write_Instruction(uchar com)//向LCD1602寫命令
  233. {
  234.         rs_l;
  235.         rw_l;
  236.         en_h;
  237.         PORTA=com;
  238.         lcd_en();//寫入命令
  239. }
  240. void lcd_dat(uchar dat)//向LCD1602寫數(shù)據(jù)
  241. {
  242.         rs_h;
  243.         rw_l;
  244.         en_h;
  245.         PORTA=dat;
  246.         lcd_en();//寫入數(shù)據(jù)
  247. }
  248. void LCD_SET_XY(uchar X,uchar Y)//字符顯示初始地址設(shè)置
  249. {
  250.   uchar address;
  251.   if(Y==0)
  252.     address=0x80+X;//Y=0,表示在第一行顯示,地址基數(shù)為0x80
  253.   else
  254.     address=0xc0+X;//Y非0時,表時在第二行顯示,地址基數(shù)為0XC0
  255.     Write_Instruction(address);//寫指令,設(shè)置顯示初始地址
  256. }
  257. void LCD_write_str(uchar X,uchar Y,uchar *s)//在第X行Y列開始顯示,指針*S所指向的字符串
  258. {
  259.   LCD_SET_XY(X,Y);//設(shè)置初始字符顯示地址
  260.   while(*s)//逐次寫入顯示字符,直到最后一個字符"/0"
  261.   {
  262.     lcd_dat(*s);//寫入當(dāng)前字符并顯示
  263.         s++;//地址指針加1,指向下一個待寫字符
  264.   }
  265. }
  266. void LCD_write_char(uchar X,uchar Y,uchar Wdata)//在第X行Y列開始顯示W(wǎng)data所對應(yīng)的單個字符
  267. {
  268.   LCD_SET_XY(X,Y);//寫地址
  269.   lcd_dat(Wdata);//寫入當(dāng)前字符并顯示
  270. }
  271. void LCD_Start(void)//LCD啟動界面
  272. {
  273.          LCD_write_str(0,0,"  18B20         ");
  274.         LCD_write_str(0,1,"  temp display  ");
  275. }
  276. //**********************************************************************//
  277. //***************************      按鍵掃描     ************************//
  278. //**********************************************************************//

  279. uchar keys(void)
  280. {
  281.         if(!(PIND&BIT(0)))//溫度顯示
  282.         {
  283.                 delayms(20);
  284.                 if(!(PIND&BIT(0)))
  285.                 {
  286.                         LCD_clear();//LCD清屏
  287.                         keyvalue=0;
  288.                         while(!(PIND&BIT(0)));//等待按鍵抬起
  289.                 }
  290.         }
  291.         if(!(PIND&BIT(1)))//最高、最低溫度設(shè)置選擇
  292.         {
  293.                 delayms(20);
  294.                 if(!(PIND&BIT(1)))
  295.                 {
  296.                         LCD_clear();//LCD清屏
  297.                         key1=key1+1;
  298.                         switch(key1)
  299.                         {
  300.                                  case 1:
  301.                                            tempH_Setting();//最高溫度設(shè)置顯示
  302.                                            keyvalue=1;//按鍵最高溫度返回值:1
  303.                                 break;
  304.                                 case 2:
  305.                                            tempL_Setting();//最低溫度設(shè)置顯示
  306.                                            keyvalue=2;//按鍵最低溫度返回值:2
  307.                                            key1=0;//按鍵清零
  308.                                 break;
  309.                         }
  310.                         while(!(PIND&BIT(1)));//等待按鍵抬起
  311.                 }
  312.         }
  313.         if(!(PIND&BIT(2)))//溫度加
  314.         {
  315.                 delayms(20);
  316.                 if(!(PIND&BIT(2)))
  317.                 {
  318.                         if(keyvalue==1)
  319.                         {
  320.                                  tempH++;
  321.                                 if(tempH>=100)
  322.                                 {
  323.                                          tempH=100;
  324.                                 }
  325.                                 tempH_Setting();//最高溫度設(shè)置顯示
  326.                         }
  327.                         else if(keyvalue==2)
  328.                         {
  329.                                  tempL++;
  330.                                 if(tempL>=100)
  331.                                 {
  332.                                          tempL=100;
  333.                                 }
  334.                                 tempL_Setting();//最低溫度設(shè)置顯示
  335.                         }
  336.                         //while(!(PIND&BIT(2)));//等待按鍵抬起
  337.                 }
  338.         }
  339.         if(!(PIND&BIT(3)))//溫度減
  340.         {
  341.                 delayms(20);
  342.                 if(!(PIND&BIT(3)))
  343.                 {
  344.                         if(keyvalue==1)
  345.                         {
  346.                                  tempH--;
  347.                                 if(tempH<=10)
  348.                                 {
  349.                                          tempH=10;
  350.                                 }
  351.                                 tempH_Setting();//最高溫度設(shè)置顯示
  352.                         }
  353.                         else if(keyvalue==2)
  354.                         {
  355.                                  tempL--;
  356.                                 if(tempL<=10)
  357.                                 {
  358.                                          tempL=10;
  359.                                 }
  360.                                 tempL_Setting();//最低溫度設(shè)置顯示
  361.                         }
  362.                         //while(!(PIND&BIT(3)));//等待按鍵抬起
  363.                 }
  364.         }
  365. }

  366. //**********************************************************************//
  367. //***************************      主函數(shù)       ************************//
  368. //**********************************************************************//

  369. void main(void)
  370. {
  371.         uchar key_j;
  372.         IO_init();        //端口初始化
  373.         LCD_init(); //LCD初始化
  374.         LCD_clear();//LCD清屏
  375.         //LCD_Start();//LCD啟動界面
  376.         while(1)
  377.         {
  378.                 keys();
  379.                 if(keyvalue==0)
  380.                 {
  381.                          temp_Read();//溫度讀取
  382.                         temp_display();//溫度顯示
  383.                         temp_police();//溫度報警
  384.                 }
  385.         }
  386. }
復(fù)制代碼

所有資料51hei提供下載:
LCD1602&amp;DS18B20溫度測試.zip (254.85 KB, 下載次數(shù): 89)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩1
回復(fù)

使用道具 舉報

沙發(fā)
ID:482371 發(fā)表于 2019-6-10 22:50 | 只看該作者
沒有.h文件嗎
回復(fù)

使用道具 舉報

板凳
ID:482371 發(fā)表于 2019-6-13 17:36 | 只看該作者
熔絲位怎么設(shè)置啊
回復(fù)

使用道具 舉報

地板
ID:549610 發(fā)表于 2019-7-18 15:05 | 只看該作者
謝謝樓主分享!
回復(fù)

使用道具 舉報

5#
ID:615580 發(fā)表于 2019-9-24 11:09 | 只看該作者
謝謝大佬的幫助!!!感謝大佬!!!
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产午夜精品一区二区三区嫩草 | 国产在线视频在线观看 | 欧美一级在线观看 | 一本色道久久综合亚洲精品高清 | 99久久精品国产麻豆演员表 | 中文字幕日韩欧美一区二区三区 | 亚州av| 色偷偷人人澡人人爽人人模 | 欧美一卡二卡在线观看 | 粉嫩国产精品一区二区在线观看 | 日韩一区二区视频 | 成人性视频免费网站 | 日韩精品视频在线 | 中文字幕在线电影观看 | 精品国产99 | 日韩成人在线一区 | 91成人在线 | 久久99蜜桃综合影院免费观看 | 日本一区二区三区四区 | 久久久久久久久一区 | 国产精品一区二区三区四区 | 国产精品污污视频 | 3级毛片| 欧美精品综合 | 国产精品爱久久久久久久 | 九九导航| 欧美一级二级视频 | 国产精品18久久久久久白浆动漫 | 国产免费一区二区 | 一区二区福利视频 | 日韩av成人 | 久久国产综合 | 午夜免费在线观看 | 久久久久中文字幕 | 综合久久久| 欧美一级黄色网 | 欧美一级毛片久久99精品蜜桃 | 中文字幕亚洲区一区二 | 日韩av免费在线观看 | 91精品久久久久久久久 | 久久久久久国产精品mv |