单总线两个ds18b20测温12684显示

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includereg51.h#includeintrins.h//内含-NOP-函数#includestdlib.h#includestring.h//#includestc11.h#defineucharunsignedchar#defineuintunsignedintintl=0;//**********宏定义所需指令#defineBASIC_SET0x30#defineEXTEND_SET0x34#defineDRAW_ON0x36#defineDRAW_OFF0x34unsignedchartemp0=0x00;unsignedcharTPH,TPL;xdataunsignedchara[]=哈尔滨理工大学;xdataunsignedcharb[]=当前室温为:;unsignedcharc[]=0000000000000000;ucharcodestr1[]={0x28,0x5E,0xAC,0x28,0x03,0x00,0x00,0xB9};//ROM1ucharcodestr2[]={0x28,0xFF,0xA8,0x68,0x04,0x00,0x00,0x57};//ROM2sbitT=P1^1;sbitRS=P0^6;sbitRW=P3^6;sbitEN=P3^7;//sbitT10=P0^5;voiddelay_1us(unsignedintx);//externvoiddelay_1us();voiddelay_5us();voiddelay_10us();voiddelay_20us();voiddelay_100us();voiddelay_200us();voidDelayXus(unsignedcharn);bitreset_DS18B20();voidwriteByte_DS18B20(unsignedcharx);unsignedcharreadByte_DS18B20();voidshow_Temperature(ucharx);voidDS18B20_show(ucharx);voidinit_DS18B20();typedefunsignedcharuint8;typedefcharint8;typedefunsignedshortuint16;typedefshortint16;typedefunsignedlonguint32;typedeflongint32;unsignedcharExchange(unsignedcharch){unsignedcharTT=0x80;unsignedcharEE=0x01;unsignedcharTemp=0,i;for(i=0;i8;i++){if(ch&TT)Temp=Temp|EE;TT=TT1;EE=EE1;}returnTemp;}voiddelay1ms(uintms)//延时1毫秒(不够精确的){uinti,j;for(i=0;ims;i++)for(j=0;j100;j++);}voidDelay1(unsignedintn){unsignedinti;for(i=0;in;i++){_nop_();}}//***********ms级延时函数voiddelay_1ms(uintx){uinti,j;for(j=0;jx;j++)for(i=0;i110;i++);}voidLCD_write_command(unsignedcharcommand){//Checkbusy();RS=0;RW=1;Delay1(10);RW=0;EN=1;P2=Exchange(command);EN=0;}ucharread_dat(void){uchartemp;P2=0xff;RS=1;RW=1;Delay1(25);EN=0;EN=1;temp=P2;EN=0;returnExchange(temp);}/*写入一个字节的数据*/voidLCD_write_byte(unsignedcharch){RS=1;RW=1;Delay1(10);RW=0;EN=1;P2=Exchange(ch);EN=0;}voidLCD_write_line(unsignedchar*ch,unsignedintLCD_line,unsignedcharLCD_High){intstr_len=0;intLCD_line_len=16;inti=0;unsignedcharaddr;str_len=strlen(ch);if(str_len16)///要显示的字符串可能大于16ByteLCD_line_len=str_len;else;switch(LCD_line){case1://写入第一行addr=0x80;break;case2://写入第二行addr=0x90;break;case3://写入第三行addr=0x88;break;case4://写入第四行addr=0x98;break;default:break;}LCD_write_command(addr+LCD_High);while(iLCD_line_len){LCD_write_byte(ch[i]);i++;}}/*voidShowAncientPeom(){LCD_write_command(0x30);LCD_write_command(0x01);Delay1(500);LCD_write_line(b,1,0);//LCD_write_line(a,1,4);//strcpy(d,g[0]);LCD_write_line(d,2,1);LCD_write_line(c1,3,0);LCD_write_line(a,4,0);}*/voidLCD_init(){unsignedcharcommand;RS=0;RW=0;EN=0;//command=0x30;//LCD_write_command(command);command=0x30;LCD_write_command(command);command=0x0c;LCD_write_command(command);command=0x01;LCD_write_command(command);command=0x06;LCD_write_command(command);}voiddelay_1us(unsignedintx){unsignedinti;x=x*5/4;for(i=0;ix;i++);}voidinit_DS18B20(){bitresult;T=1;delay_1us(5);//延迟几微秒T=0;//单片机将单线总线信号拉低480-950us,delay_1us(500);//然后释放总线(拉高电平)T=1;delay_1us(20);//等待15-60us等待DS18B20输出脉冲delay_1us(100);//DS18B20会持续输出60-240us,//之后DS18B20会拉高电平,释放总线result=T;}bitreset_DS18B20(){bitflag;T=0;DelayXus(240);DelayXus(240);/*delay_200us();delay_200us();delay_100us();delay_20us();*/T=1;//delay_100us();//delay_20us();DelayXus(100);while(T);flag=T;/*delay_200us();delay_20us();delay_10us();delay_10us();*/DelayXus(240);DelayXus(180);//while(!T);//delay_100us();return(flag);}/*voiddelay_1us(){//STC11/10系列长调用LCALL花费6个时钟周期,//ret返回花费4个时钟周期_nop_();//NOP指令花费1个时钟周期_nop_();}*/voiddelay_5us(){_nop_();}voiddelay_10us(){delay_5us();_nop_();}voiddelay_20us(){delay_10us();delay_5us();_nop_();}voiddelay_100us(){delay_20us();delay_20us();delay_20us();delay_20us();delay_10us();delay_5us();_nop_();}voiddelay_200us(){delay_100us();delay_20us();delay_20us();delay_20us();delay_20us();delay_10us();delay_5us();_nop_();}voidwriteByte_DS18B20(unsignedchardat){unsignedchari=0;for(i=0;i8;i++){T=0;////单片机将单线总线拉低,开始写_nop_();T=dat&0x01;////写入数据/*delay_20us();delay_20us();delay_20us();*/DelayXus(60);T=1;dat=1;}}unsignedcharreadByte_DS18B20(){unsignedchari;for(i=0;i8;i++){temp0=1;T=0;////单片机拉低信号,开始//_nop_();DelayXus(1);T=1;//_nop_();DelayXus(1);if(T==1)temp0|=0x80;DelayXus(20);DelayXus(20);//delay_20us();//delay_20us();////等待控制器开始在45us内采样}return(temp0);}/*voidshow_Temperature(){inti=0;reset_DS18B20();//设备复位writeByte_DS18B20(0xCC);//跳过ROM命令writeByte_DS18B20(0x44);//开始转换命令while(!T);//等待转换完成reset_DS18B20();//设备复位writeByte_DS18B20(0xCC);//跳过ROM命令writeByte_DS18B20(0xBE);//读暂存存储器命令TPL=readByte_DS18B20();//读温度低字节TPH=readByte_DS18B20();//读温度高字节while(i20000){i++;}DS18B20_show();}*/voidb20_Matchrom(uchara)//匹配ROM{charj;writeByte_DS18B20(0x55);//发送匹配ROM命令if(a==1){for(j=0;j8;j++)writeByte_DS18B20(str1[j]);//发送18B20的序列号,先发送低字节}if(a==2){for(j=0;j8;j++)writeByte_DS18B20(str2[j]);//发送18B20的序列号,先发送低字节}}voidshow_Temperature(ucharz)/*读取温度值并转换*/{inti=0;reset_DS18B20();writeByte_DS18B20(0xCC);//for(i=0;i8;i++)//{//str1[i]=readByte_DS18B20();//}//读序列号reset_DS18B20();if(z==1){b20_Matchrom(1);//匹配ROM1}if(z==2){b20_Matchrom(2);//匹配ROM2}writeByte_DS18B20(0x44);delay1ms(5);reset_DS18B20();writeByte_DS18B20(0xCC);//读序列号reset_DS

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功