智能温度报警系统摘要:随着时代的进步和发展,单片机技术已经普及到我们生活,工作,科研,各个领域,已经成为一种比较成熟的技术,本文将介绍一种基于单片机控制的数字温度计,本温度计属于多功能温度计,可以设置上下报警温度,当温度不在设置范围内时,可以报警。
关键词:单片机,数字控制,温度计,DS18B20,AT89S51ABSTRACTAlong with the progress and developments of the ages, a machine technique has already make widely available we are living, work, the research, each realm, has already become a kind of to compare the mature technique, this text will introduce a kind of numerical thermometer that controls according to a machine, this thermometer belongs to the multi-function thermometer, can establish the top and bottom to report to the police the temperature, being the temperature not within the scope of establish, can report to the police.Key Word: numeral control, thermometer,DS18B20,AT89S52引言随着人们生活水平的不断提高,单片机控制无疑是人们追求的目标之一,它所给人带来的方便也是不可否定的,其中数字温度计就是一个典型的例子,但人们对它的要求越来越高,要为现代人工作、科研、生活、提供更好的更方便的设施就需要从数单片机技术入手,一切向着数字化控制,智能化控制方向发展。
本设计所介绍的数字温度计与传统的温度计相比,具有读数方便,测温范围广,测温准确,其输出温度采用数字显示,主要用于对测温比较准确的场所,或科研实验室使用,该设计控制器使用单片机AT89S51,测温传感器使用DS18B20,用3位共阳极LED数码管以串口传送数据,实现温度显示,能准确达到以上要求。
智能温度报警系统:元件清单程序:#include<reg52.h>#include <stdio.h>#include <absacc.h>code unsigned char seg7code[11]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40}; //显示段码sbit TMDAT =P3^1; //DS18B20 的数据输入/输出脚DQ,根据情况设定sbit jia=P2^1;sbit jian=P2^0;sbit hong=P1^0; //红色警告灯sbit sheng=P1^1; //蜂鸣器sbit lan=P1^2; //兰色灯bit write=0; //写24C08 的标志;j=30;unsigned int sdata;//测量到的温度的整数部分unsigned char xiaoshu1;//小数第一位unsigned char xiaoshu2;//小数第二位unsigned char xiaoshu;//两位小数bit fg=1; //温度正负标志////////24C08 读写驱动程序////////////////////sbit scl=P3^4; // 24c08 SCLsbit sda=P3^5; // 24c08 SDAvoid delay1(unsigned char x){ unsigned int i;for(i=0;i<x;i++);;}void flash(){ ; ; }void x24c08_init() //24c08 初始化子程序{scl=1; flash(); sda=1; flash();}void start() //启动(I方C)总线{sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();}void stop() //停止(I方C)总线{sda=0; flash(); scl=1; flash(); sda=1; flash();}void writex(unsigned char j) //写一个字节{ unsigned char i,temp;temp=j;for (i=0;i<8;i++){temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();}scl=0; flash(); sda=1; flash();}unsigned char readx() //读一个字节{unsigned char i,j,k=0;scl=0; flash(); sda=1;for (i=0;i<8;i++){flash(); scl=1; flash();if (sda==1) j=1;else j=0;k=(k<<1)|j;scl=0;}flash(); return(k);}void clock() //(I方C)线时钟{unsigned char i=0;scl=1; flash();while ((sda==1)&&(i<255))i++;scl=0; flash();}////////从24c02 的地址address 中读取一个字节数据/////unsigned char x24c08_read(unsigned char address){unsigned char i;start(); writex(0xa0);clock(); writex(address);clock(); start();writex(0xa1); clock();i=readx(); stop();delay1(10);return(i);}//////向24c02 的address 地址中写入一字节数据info///// void x24c08_write(unsigned char address,unsigned char info){EA=0;start(); writex(0xa0);clock(); writex(address);clock(); writex(info);clock(); stop();EA=1;delay1(50);}/*////////////24C08 读写驱动程序完///////////////////// void Delay2(unsigned int tc) //延时程序{while( tc != 0 ){unsigned int i;for(i=0; i<100; i++);tc--;}}*///////////*显示延时程序*///////////////void Delay(unsigned int tc){while( tc != 0 ){unsigned int i;for(i=0; i<80; i++);tc--;}}////////////延时部分///////////////void yanshi (unsigned int count){unsigned char i;while(count--){for(i=0;i<115;i++);}}/////////////发送复位///////////////void fashong (void){unsigned char i;TMDAT = 0; for(i=0;i<103;i++);TMDAT = 1; for(i=0;i<4;i++);}bit tmrbit (void) //读一位//{unsigned int i;bit dat;TMDAT = 0;i++;TMDAT = 1;i++; i++; //微量延时//dat = TMDAT;for(i=0;i<8;i++);return (dat);}unsigned char tmrbyte (void) //读一个字节{unsigned char i,j,dat;dat = 0;for (i=1;i<=8;i++){ j = tmrbit(); dat = (j << 7) | (dat >> 1); } return (dat);}void tmwbyte (unsigned char dat) //写一个字节{unsigned char j,i;bit testb;for (j=1;j<=8;j++){ testb = dat & 0x01;dat = dat >> 1;if (testb){ TMDAT = 0; //写0i++; i++;TMDAT = 1;for(i=0;i<8;i++); }else{ TMDAT = 0; //写0for(i=0;i<8;i++);TMDAT = 1;i++; i++;}}}void tmstart (void) //发送ds1820 开始转换{fashong(); //复位yanshi(1); //延时tmwbyte(0xcc); //跳过序列号命令tmwbyte(0x44); //发转换命令44H,}void tmrtemp (void) //读取温度{unsigned char a,b;fashong (); //复位yanshi (1); //延时tmwbyte (0xcc); //跳过序列号命令tmwbyte (0xbe); //发送读取命令a = tmrbyte (); //读取低位温度b = tmrbyte (); //读取高位温度if(b>0x7f) //最高位为1 时温度是负{a=~a; b=~b+1; //补码转换,取反加一fg=0; //读取温度为负时fg=0}sdata = a/16+b*16; //整数部分xiaoshu1 = (a&0x0f)*10/16; //小数第一位xiaoshu2 = (a&0x0f)*100/16%10;//小数xiaoshu=xiaoshu1*10+xiaoshu2; //小数}void DS18B20PRO(void){tmstart();//yanshi(5); //如果是不断地读取的话可以不延tmrtemp(); //读取温度,执行完毕温度将存于}void Led(){if(fg==1) //温度为正时显示的数据{P2=P2&0xef;P0=seg7code[sdata/10]; //输Delay(2); P2=P2|0xf0; P2=P2&0xdf;P0=seg7code[sdata%10]|0x80; //输出个Delay(2); P2=P2|0xf0; P2=P2&0xbf;P0=seg7code[xiaoshu1]; //输出小数点Delay(2); P2=P2|0xf0; P2=P2&0x7f;P0=seg7code[xiaoshu2]; //输出小Delay(1); P2=P2|0xf0;Delay(2); P2=P2|0xf0;P2=P2&0xf7;P0=seg7code[j/10]; //输出十位Delay(2); P2=P2|0x0f; P2=P2&0xfb;P0=seg7code[j%10]|0x80; //输出个位Delay(1); P2=P2|0x0f;。