资源描述:
《基于单片机的多路温度检测系统设计》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、基于单片机的多路温度检测系统设计#include<reg51.h>#defineuintunsignedint#defineucharunsignedcharsbitlcdrs=P3^0;sbitlcdrw=P3^1;sbitlcden=P3^2;//LCD选通线sbitDQ=P1^7;//18B20引脚uintvalue;//温度值sbitsound=P1^6;//蜂鸣器sbitwarning=P3^5;//LED报警灯sbitgreenled=P3^6;//LED报警灯sbitk1=P1^0;//setsbitk2=P1^1;//+sbitk3=P1^2;//
2、-charmax=40;//初始温度最大值charmin=10;//初始温度最小值ucharchange_flag;//按键次数ucharcodetable1[]={0x28,0x30,0xc5,0xb8,0x00,0x00,0x00,0x8e};//rom1测得的序列号ucharcodetable2[]={0x28,0x31,0xc5,0xb8,0x00,0x00,0x00,0xb9};//rom2ucharcodetable3[]={0x28,0x32,0xc5,0xb8,0x00,0x00,0x00,0xe0};//rom3ucharcodetable4[]={0x28
3、,0x33,0xc5,0xb8,0x00,0x00,0x00,0xd7};//rom4ucharcodetable5[]="Temple";ucharcodetable6[]="max:min:";uinttemper[5];//温度值缓存bitfg=0;//温度正负标志voiddelay(uintn){uintx,y;for(x=n;x>0;x--)for(y=110;y>0;y--);}voidwrite_com(ucharcom){lcdrs=0;//rs=0时输入指令P2=com;delay(5);lcden=1;delay(5);//en=1时读取信息l
4、cden=0;//1->0执行指令}voidwrite_dat(uchardat){lcdrs=1;//rs=1时输入数据P2=dat;delay(5);lcden=1;delay(5);lcden=0;}voidinit_lcd(){lcden=0;lcdrw=0;write_com(0x38);//8位数据,双列,5*7字形write_com(0x0c);//开启显示屏,关光标,光标不闪烁}write_com(0x06);//显示地址递增,即写一个数据后,显示位置右移一位write_com(0x01);voiddelay_us(uchart)//微妙延时{while
5、(t--);}voidinit_ds18b20()//数据初始化{DQ=1;delay_us(4);DQ=0;delay_us(80);DQ=1;delay_us(200);}voidwrite_ds18b20(uchardat)//写数据{uchari;for(i=0;i<8;i++){DQ=0;DQ=dat&0x01;delay_us(15);DQ=1;dat=dat>>1;}delay_us(10);}ucharread_ds18b20()//读数据{uchari=0,readat=0;for(i=0;i<8;i++){DQ=0;read
6、at=readat>>1;//八位全部都是0,向右移一位后,仍然全是0DQ=1;if(DQ)//如果DQ为1,readat进行或运算,如果为0,则子语句不执行,直接for循环此时采集的数据是0readat=readat
7、0x80;//此时最高位为1,然后再进行for循环最高位成为第七位,依次往复delay_us(10);}}returnreadat;voidcheck_rom(uchara)//匹配序列号{ucharj;write_ds18b20(0x55);if(a==1){for(j=0;j<8;j++){write_ds18b20(table1[j])
8、;}}if(a==2){for(j=0;j<8;j++){write_ds18b20(table2[j]);}}if(a==3){for(j=0;j<8;j++){write_ds18b20(table3[j]);}}if(a==4){for(j=0;j<8;j++){write_ds18b20(table4[j]);}}}uintchange_ds18b20(ucharz)//为DS18B20序号{uchartl,th;init_ds18b20();//初始化write_ds18b2