51单片机lcd1602显示温度源程序18b20测温_第1页
51单片机lcd1602显示温度源程序18b20测温_第2页
51单片机lcd1602显示温度源程序18b20测温_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/III/ = /=作者:Ni 的年少 WAo 轻狂=/=创建时间:2008.07.03 =/ = / / DS18b20 温度传感器/*注:1、当用到循环左移或右移时<>> 或 <<),不能超出变的范围例如:un sig ned char i,j。j=(i<<8>>>4得到的结果出错。un sig ned char j。unsignedint i。j=(i<<8>>>4。得到的结果正确2、LCD的清屏时间-1.64ms。*/ #include<reg51.h> #include"L

2、CD1602.h" #include <intrins.h> #define uchar unsigned char #define uint unsigned int uchartmp4=0,0,0,0 。 #include"DS18B20.h" / / 主程序 / void main(void> uchar temp 。 LCD_init(>。LCD_write_command(0x01> 。 / 清屏显示 Delay_50Us(50>。gotoxy(2,1> 。 LCD_display("NOW TEMP:

3、">。gotoxy(2,2>。 LCD_display("163qishentong"> 。Delay_2Us(200>。 temp=Ds18b20_Read_Temperature(> 。85Delay_50Us(1000>。/ 空读一次,读出默认的while(1> gotoxy(12,1> 。 change(>。/Delay_2Us(200> 。 LCD_display(tmp>。Delay_50Us(100>。 /*= = 作者:Ni的年少 WAo疯狂= 创建时间: 2008.06.20

4、 = =*/ #define LCD_DB P0定义 LCD 的数据端口 sbit LCD_RS=P2A0。sbit LCD_RW=P2A1。 sbit LCD_E=P2A2。#define uchar unsigned char #define uint unsigned int/=延时子函数 = void delay(uint x>uinti,j 。for(i=x 。 i>0。 i-> for(j=0 。 j<2。 j+>。/=写指令函数 = voidLCD_write_command(uchar command> LCD_DB=command。LCD_

5、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,uchar y, uchardat> uchar address 。 if(y=1> address=0x80+x。 else

6、address=0xc0+x。LCD_write_command(address> 。 LCD_write_data(dat> 。 =*/ /=延时 1= void delay1(uint x>uinti,j 。 for(j=0 。 j<x。 j+> for(i=0 。 i<100。 i+>。 /=初始化函数 = voidLCD_init(void>LCD_write_command(0x38> 。/ 设置 8 位格式, 2 行, 5*7 LCD_write_command(0x0c> 。 / 整体显示,关光标,不闪烁 LCD_wri

7、te_command(0x06> 。 / 设定输入方式,增量不移位 LCD_write_command(0x01> 。 / 清屏显示 delay(200>。/= 光标定位 = void gotoxy(ucharx,uchar y> / x 表示列, y 表示行 uchar address 。if(y=1>address=0x80+x。elseaddress=0xc0+x。LCD_write_command(address> 。/=显示一个字符串 = voidLCD_display(uchar *dat> while(*dat!='0'&

8、gt;LCD_write_data(*dat> 。dat+。/ / / = /Illi =作者:Ni 的年少 WAo 轻狂= Illi / = 创建时间: 2008.07.03 = / lll = lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll Ds18b20 引脚分配 llllllllllllllllllllllllllllllllllllllllllllllllllllllll

9、llllllllsbit DQ=P3A1。lllllllllllllllllllllllllllllllllllllllllllllllllllllllllll llllll 延时子程序 llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllvoid Delay_2Us(uint us> ll 微妙延时 while(-us>。void Delay_50Us(uint t> ll 毫秒延时uchar j 。for( 。 t>0 。 t->for(j=19。j>0。j->。/ /

10、DS18b20 初始化 / void Ds18b20_Initial(void>DQ_nop_(>。DQ = 0。Delay_50Us(12>。DQ = 1。Delay_2Us(5>。/ DQT位/精确延时,大于 480us /拉/ 高总线/稍做延时后while(DQ。/若x=0则初始化成功,若 x=1则初始化失败Delay_2Us(20。 / 写一个字节的数据 / void Ds18b20_Write(uchardat>uchar i。for(i=0。 i<8。 i+>DQ = 0。_nop_(>。DQ=dat&0x01 。Delay_

11、2Us(20>。DQ = 1。dat>>=1 。/ 读一个字节的数据 /uchar Ds18b20_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> 。 / 读两个字节的温度值 / / uchar Ds18b20_Read_Temperature(void> uchar temp1,temp 。 int temp2

12、 。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 。 return temp 。 void c

13、hange(void> uchartp4,temp 。 temp=Ds18b20_Read_Temperature(> 。 Delay_2Us(250>。if(temp&0xc0>=0xc0> temp=temp+1 。 tp1=(temp/10>%10+'0' 。 tp2=temp%10+'0' 。tp3='0' 。 if(tp1='0'> tmp0=' ' 。 tmp1='-' 。 tmp2=tp2 。 tmp3='0' 。elsetmp0='-' 。 tmp1=tp1 。 tmp2=tp2 。tmp3='0' 。else tp0=(temp/100>%10+'0' 。 tp1=(temp/10>%10+'0' 。 tp2=temp%10+'0' 。 if(tp0='0'>&&(tp1='0'>>tmp0=' ' 。tmp1=' ' 。 tmp2=tp2 。tmp3='0' 。 if(tp0

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论