- 该温度检测系统可直接测温,测温范围为-125-+125度,还可以时时对不同时刻的温度进行手动采样,待采样完成后,可以将采样温度一一读出,也可进行定时采样,例如间隔1秒采样温度一次,采样开始有指示灯亮,采样结束指示灯灭,停止采样,一一读出数据。
- 源程序
- #include<reg51.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit lcdrs=P1^1;
- sbit lcdrw=P1^2;
- sbit lcden=P1^3;
- sbit DQ = P3^7;
- sbit JL=P3^0;
- sbit DT=P3^1;
- sbit DS=P3^2;
- sbit LED=P3^3;
- uchar code table0[]=" RUI LOVE YING";
- uchar unm,temp,temp1,temp2,temp3,kk=0,s,tt=0;
- uchar temp00,temp01,temp02,temp03,temp04,temp05,temp06,temp07,temp08,temp09;
- uchar xs00,xs01,xs02,xs03,xs04,xs05,xs06,xs07,xs08,xs09;
- /*************精确延时函数*****************/
- void delay(unsigned char i)
- {
- while(--i);
- }
- /******************************************
- 此延时函数针对的是12Mhz的晶振
- delay(0):延时518us 误差:518-2*256=6
- delay(1):延时7us (原帖写"5us"是错的)
- delay(10):延时25us 误差:25-20=5
- delay(20):延时45us 误差:45-40=5
- delay(100):延时205us 误差:205-200=5
- delay(200):延时405us 误差:405-400=5
- *******************************************/
- /*****************DS18B20******************/
- void Init_Ds18b20(void) //DS18B20初始化send reset and initialization command
- {
- DQ = 1; //DQ复位,不要也可行。
- delay(1); //稍做延时
- DQ = 0; //单片机拉低总线
- delay(250); //精确延时,维持至少480us
- DQ = 1; //释放总线,即拉高了总线
- delay(100); //此处延时有足够,确保能让DS18B20发出存在脉冲。
- }
- uchar Read_One_Byte() //读取一个字节的数据read a byte date
- //读数据时,数据以字节的最低有效位先从总线移出
- {
- uchar i = 0;
- uchar dat = 0;
- for(i=8;i>0;i--)
- {
- DQ = 0; //将总线拉低,要在1us之后释放总线
- //单片机要在此下降沿后的15us内读数据才会有效。
- _nop_(); //至少维持了1us,表示读时序开始
- dat >>= 1; //让从总线上读到的位数据,依次从高位移动到低位。
- DQ = 1; //释放总线,此后DS18B20会控制总线,把数据传输到总线上
- delay(1); //延时7us,此处参照推荐的读时序图,尽量把控制器采样时间放到读时序后的15us内的最后部分
- if(DQ) //控制器进行采样
- {
- dat |= 0x80; //若总线为1,即DQ为1,那就把dat的最高位置1;若为0,则不进行处理,保持为0
- }
- delay(10); //此延时不能少,确保读时序的长度60us。
- }
- return (dat);
- }
- void Write_One_Byte(uchar dat)
- {
- uchar i = 0;
- for(i=8;i>0;i--)
- {
- DQ = 0; //拉低总线
- _nop_(); //至少维持了1us,表示写时序(包括写0时序或写1时序)开始
- DQ = dat&0x01; //从字节的最低位开始传输
- //指令dat的最低位赋予给总线,必须在拉低总线后的15us内,
- //因为15us后DS18B20会对总线采样。
- delay(10); //必须让写时序持续至少60us
- DQ = 1; //写完后,必须释放总线,
- dat >>= 1;
- delay(1);
- }
- }
- uint Get_Tmp() //获取温度get the temperature
- {
- uchar a,b;
- Init_Ds18b20(); //初始化
- Write_One_Byte(0xcc); //忽略ROM指令
- Write_One_Byte(0x44); //温度转换指令
- Init_Ds18b20(); //初始化
- Write_One_Byte(0xcc); //忽略ROM指令
- Write_One_Byte(0xbe); //读暂存器指令
- a = Read_One_Byte(); //读取到的第一个字节为温度LSB
- b = Read_One_Byte(); //读取到的第一个字节为温度MSB
- temp1 = b<<4;
- temp3=(a&0xf0)>>4;
- temp=temp1+temp3;
- temp2=(a&0x0f)*0.0625*100;
- return temp;
- }
- void delay1(int z)
- {
- int x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--) ;
- }
- void writen_com(uchar com)
- {
- lcdrs=0;
- P2=com;
- delay1(5);
- lcden=1;
- delay1(5);
- lcden=0;
- }
-
- void writen_date(uchar date)
- {
- lcdrs=1;
- P2=date;
- delay1(5);
- lcden=1;
- delay1(5);
- lcden=0;
- }
- void writen_sfm(uchar add,uchar date)
- {
- uchar bai,shi,ge;
- bai=date/100;
- shi=date%100/10;
- ge=date%10;
- writen_com(0x80+0x40+add);
- writen_date(0x30+bai);
- writen_date(0x30+shi);
- writen_date(0x30+ge);
- }
- void writen_sfm1(uchar add,uchar date)
- {
- uchar shi,ge;
-
- shi=date/10;
- ge=date%10;
- writen_com(0x80+0x40+add);
- writen_date(0x30+shi);
- writen_date(0x30+ge);
- }
- void main();
- void xianshi_temp()
- {
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('0');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs00);
- writen_sfm(2,temp00);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('1');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs01);
- writen_sfm(2,temp01);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('2');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs02);
- writen_sfm(2,temp02);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('3');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs03);
- writen_sfm(2,temp03);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('4');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs04);
- writen_sfm(2,temp04);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('5');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs05);
- writen_sfm(2,temp05);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('6');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs06);
- writen_sfm(2,temp06);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('7');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs07);
- writen_sfm(2,temp07);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('8');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs08);
- writen_sfm(2,temp08);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- writen_com(0x01);
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- writen_date('9');
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- while(DT)
- {
- writen_sfm1(6,xs09);
- writen_sfm(2,temp09);
- }
- if(DT ==0)
- {
- delay(5);
- if(DT==0)
- {
- while(!DT);
- s=0;
- main();
- }}}}}}}}}}}}}}}}}}}}}}
- }
- void record_temp()
- {
- if(JL == 0)
- {
- delay(5);
- if(JL == 0)
- {
- while(!JL);
- switch(s)
- {
- case 0:temp00=Get_Tmp(),xs00=temp2;break;
- case 1:temp01=Get_Tmp(),xs01=temp2;break;
- case 2:temp02=Get_Tmp(),xs02=temp2;break;
- case 3:temp03=Get_Tmp(),xs03=temp2;break;
- case 4:temp04=Get_Tmp(),xs04=temp2;break;
- case 5:temp05=Get_Tmp(),xs05=temp2;break;
- case 6:temp06=Get_Tmp(),xs06=temp2;break;
- case 7:temp07=Get_Tmp(),xs07=temp2;break;
- case 8:temp08=Get_Tmp(),xs08=temp2;break;
- case 9:temp09=Get_Tmp(),xs09=temp2;break;
- }
- s++;
- if(s==10)
- s=0;
- }
- }
- }
- void init()
- {
- temp00=temp01=temp02=temp03=temp04=temp05=temp06=temp07=temp08=temp09=0;
- xs00=xs01=xs02=xs03=xs04=xs05=xs06=xs07=xs08=xs09=0;
- s=0;
- LED=1;
- lcden=0;
- lcdrw=0;
- writen_com(0x38);
- writen_com(0x0c);
- writen_com(0x06);
- writen_com(0x80);
- for(unm=0;unm<14;unm++)
- {
- writen_date(table0[unm]);
- }
- writen_com(0x80+0x40+0x05);
- writen_date('.');
- }
- void main()
- {TMOD=0X01; //定时器工作方式设定
- TH0=(65536-50000)/256; //设定定时初值50MS
- TL0=(65536-50000)%256;
- EA=1; //开总中断
- ET0=1; //开定时器0中断
- TR0=1;
- init();
- while(1)
- {
- xianshi_temp();
- record_temp();
- writen_sfm1(6,temp2);
- writen_sfm(2,Get_Tmp());
- }
- }
- void time0() interrupt 1 //定时器0
- {
- if(DS==0)
- {
- delay(5);
- if(DS==0)
- {
- LED=0;
- if(tt==4)
- {
- tt=0;
- switch(s)
- {
- case 0:temp00=Get_Tmp(),xs00=temp2;break;
- case 1:temp01=Get_Tmp(),xs01=temp2;break;
- case 2:temp02=Get_Tmp(),xs02=temp2;break;
- case 3:temp03=Get_Tmp(),xs03=temp2;break;
- case 4:temp04=Get_Tmp(),xs04=temp2;break;
- case 5:temp05=Get_Tmp(),xs05=temp2;break;
- case 6:temp06=Get_Tmp(),xs06=temp2;break;
- case 7:temp07=Get_Tmp(),xs07=temp2;break;
- case 8:temp08=Get_Tmp(),xs08=temp2;break;
- case 9:temp09=Get_Tmp(),xs09=temp2;break;
- }
- s++;
- if(s==10)
- {s=0;
- LED=1;
- while(!DS);
- }}}}
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- Get_Tmp();
- tt++;
- }
复制代码
|