51单片机lcd1602显示温度源程序18b20测温.docx
- 文档编号:16071485
- 上传时间:2023-07-10
- 格式:DOCX
- 页数:13
- 大小:271.05KB
51单片机lcd1602显示温度源程序18b20测温.docx
《51单片机lcd1602显示温度源程序18b20测温.docx》由会员分享,可在线阅读,更多相关《51单片机lcd1602显示温度源程序18b20测温.docx(13页珍藏版)》请在冰点文库上搜索。
51单片机lcd1602显示温度源程序18b20测温
51单片机lcd1602显示温度源程序18b20测温
部门:
xxx
时间:
xxx
制作人:
xxx
整理范文,仅供参考,可下载自行修改
/////////////////////////////////////////////////////
//////
///=======================================///b5E2RGbCAP
////===作者:
Ni的年少W^o轻狂==========////p1EanqFDPw
////===创建时间:
2008.07.03==========////DXDiTa9E3d
///=======================================////RTCrpUDGiT
/////5PCzVD7HxA
//////////////////////////////////////////////////////jLBHrnAILg
////////////////////////////////////
/////DS18b20温度传感器///////
////////////////////////////////////
/*
注:
1、当用到循环左移或右移时<>>或<<),不能超出变的范围
例如:
unsignedchari,j。
j=(i<<8>>>4。
得到的结果出错。
。
。
unsignedcharj。
unsignedinti。
j=(i<<8>>>4。
得到的结果正确.....
2、LCD的清屏时间----1.64ms。
*/
#include
#include"LCD1602.h"
#include
#defineucharunsignedchar
#defineuintunsignedint
uchartmp[4]={0,0,0,0}。
#include"DS18B20.h"
///////////////////////////////////////////////////////////xHAQX74J0X
//////主程序///////
///////////////////////////////////////////////////////////LDAYtRyKfE
voidmain(void>
{
uchartemp。
LCD_init(>。
LCD_write_command(0x01>。
//清屏显示
Delay_50Us(50>。
gotoxy(2,1>。
LCD_display("NOWTEMP:
">。
gotoxy(2,2>。
LCD_display("163qishentong">。
Delay_2Us(200>。
temp=Ds18b20_Read_Temperature(>。
Delay_50Us(1000>。
//空读一次,读出默认的85
while(1>
{
gotoxy(12,1>。
change(>。
//Delay_2Us(200>。
LCD_display(tmp>。
Delay_50Us(100>。
}
}
/*==================================
===作者:
Ni的年少W^o疯狂======
===创建时间:
2008.06.20======
==================================*/
#defineLCD_DBP0//定义LCD的数据端口
sbitLCD_RS=P2^0。
sbitLCD_RW=P2^1。
sbitLCD_E=P2^2。
#defineucharunsignedchar
#defineuintunsignedint
//===========延时子函数========================
voiddelay(uintx>
{uinti,j。
for(i=x。
i>0。
i-->
for(j=0。
j<2。
j++>。
}
//==================写指令函数=================
voidLCD_write_command(ucharcommand>
{LCD_DB=command。
LCD_RS=0。
//指令
LCD_RW=0。
//写入
LCD_E=1。
LCD_E=0。
delay(1>。
//等待执行完毕。
。
。
}
//===================写数据函数=================
voidLCD_write_data(uchardat>
{LCD_DB=dat。
LCD_RS=1。
//数据寄存器
LCD_RW=0。
//写入数据
LCD_E=1。
LCD_E=0。
delay(1>。
//等待程序执行完毕。
。
。
}
/*============显示一个字符子函数===============
voidLCD_disp_char(ucharx,uchary,uchardat>
{ucharaddress。
if(y==1>
address=0x80+x。
else
address=0xc0+x。
LCD_write_command(address>。
LCD_write_data(dat>。
}
===================================*/
//==========延时1====================
voiddelay1(uintx>
{uinti,j。
for(j=0。
j j++> for(i=0。 i<100。 i++>。 } //===========初始化函数============= voidLCD_init(void> {LCD_write_command(0x38>。 //设置8位格式,2行,5*7 LCD_write_command(0x0c>。 //整体显示,关光标,不闪烁 LCD_write_command(0x06>。 //设定输入方式,增量不移位 LCD_write_command(0x01>。 //清屏显示 delay(200>。 } //==========光标定位======================= voidgotoxy(ucharx,uchary>//x表示列,y表示行 {ucharaddress。 if(y==1> address=0x80+x。 else address=0xc0+x。 LCD_write_command(address>。 } //=========显示一个字符串================== voidLCD_display(uchar*dat> {while(*dat! ='\0'> {LCD_write_data(*dat>。 dat++。 } } /////////////////////////////////////////////////////Zzz6ZB2Ltk //////dvzfvkwMI1 ///=======================================///rqyn14ZNXI ////===作者: Ni的年少W^o轻狂==========////EmxvxOtOco ////===创建时间: 2008.07.03==========////SixE2yXPq5 ///=======================================////6ewMyirQFL /////kavU42VRUs //////////////////////////////////////////////////////y6v3ALoS89 ////////////////////////////////////////////////////////////M2ub6vSTnP ////Ds18b20引脚分配///// ///////////////////////////////////////////////////////////0YujCfmUCw sbitDQ=P3^1。 ///////////////////////////////////////////////////////////eUts8ZQVRd //////延时子程序/////// ///////////////////////////////////////////////////////////sQsAEJkW5T voidDelay_2Us(uintus>//微妙延时 { while(--us>。 } voidDelay_50Us(uintt>//毫秒延时 { ucharj。 for(。 t>0。 t--> for(j=19。 j>0。 j-->。 } ///////////////////////////////////////////////////////////GMsIasNXkA //////DS18b20初始化/////// ///////////////////////////////////////////////////////////TIrRGchYzg voidDs18b20_Initial(void> { DQ=1。 _nop_(>。 DQ=0。 //DQ复位7EqZcWLZNX Delay_50Us(12>。 //精确延时,大于480uslzq7IGf02E DQ=1。 //拉高总线zvpgeqJ1hk Delay_2Us(5>。 //稍做延时后 while(DQ>。 //若x=0则初始化成功,若x=1则初始化失败 Delay_2Us(20>。 } ///////////////////////////////////////////////////////////NrpoJac3v1 //////写一个字节的数据/////// //////////////////////////////////////////////////////////1nowfTG4KI voidDs18b20_Write(uchardat> { uchari。 for(i=0。 i<8。 i++> { DQ=0。 _nop_(>。 DQ=dat&0x01。 Delay_2Us(20>。 DQ=1。 dat>>=1。 } } ///////////////////////////////////////////////////////////fjnFLDa5Zo //////读一个字节的数据/////// ///////////////////////////////////////////////////////////tfnNhnE6e5 ucharDs18b20_Read(void> { uchari,dat。 dat=0。 for(i=8。 i>0。 i--> { DQ=0。 dat>>=1。 DQ=1。 _nop_(>。 if(DQ>dat=dat|0x80。 Delay_2Us(20>。 } return(dat>。 } ///////////////////////////////////////////////////////////HbmVN777sL //////读两个字节的温度值/////// ///////////////////////////////////////////////////////////V7l4jRB8Hs ucharDs18b20_Read_Temperature(void> { uchartemp1,temp。 inttemp2。 Ds18b20_Initial(>。 Ds18b20_Write(0xcc>。 Ds18b20_Write(0x44>。 Delay_2Us(200>。 Ds18b20_Initial(>。 Ds18b20_Write(0xcc>。 Ds18b20_Write(0xbe>。 Delay_2Us(200>。 temp1=Ds18b20_Read(>。 temp2=Ds18b20_Read(>。 temp=(temp1|(temp2<<8>>>>4。 returntemp。 } voidchange(void> { uchartp[4],temp。 temp=Ds18b20_Read_Temperature(>。 Delay_2Us(250>。 if((temp&0xc0>==0xc0> { temp=~temp+1。 tp[1]=(temp/10>%10+'0'。 tp[2]=temp%10+'0'。 tp[3]='\0'。 if(tp[1]=='0'> { tmp[0]=''。 tmp[1]='-'。 tmp[2]=tp[2]。 tmp[3]='\0'。 } else { tmp[0]='-'。 tmp[1]=tp[1]。 tmp[2]=tp[2]。 tmp[3]='\0'。 } } else { tp[0]=(temp/100>%10+'0'。 tp[1]=(temp/10>%10+'0'。 tp[2]=temp%10+'0'。 if((tp[0]=='0'>&&(tp[1]=='0'>> { tmp[0]=''。 tmp[1]=''。 tmp[2]=tp[2]。 tmp[3]='\0'。 } if((tp[0]=='0'>&&(tp[1]! ='0'>> { tmp[0]=''。 tmp[1]=tp[1]。 tmp[2]=tp[2]。 tmp[3]='\0'。 } if((tp[0]! ='0'>&&(tp[1]! ='0'>> { tmp[0]=tp[0]。 tmp[1]=tp[1]。 tmp[2]=tp[2]。 tmp[3]='\0'。 } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 单片机 lcd1602 显示 温度 源程序 18 b20 测温
![提示](https://static.bingdoc.com/images/bang_tan.gif)