您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 基于单片机AT89C51的温度自动检测系统代码及软件模拟
温度自定检测系统1.delay.h#ifndef_DELAY_H#define_DELAY_Hvoiddelay_ms(uinttimer);voiddelay_us(uchartimer);#endif2.delay.c#includemain.hvoiddelay_ms(uinttimer)//延时1ms{ucharj=0;while(timer--){for(j=124;j0;j--){;}}}voiddelay_us(uchartimer)//延时约9us*timer+6um{for(;timer0;timer--){_nop_();}}******************************************************************************************3.ds1302.h#ifndef_DS1302_H#define_DS1302_H#includemain.h//DS1302控制字(地址/命令)#defineDS1302_SEC0x80#defineDS1302_MIN0x82#defineDS1302_HOUR0x84#defineDS1302_DATE0x86#defineDS1302_MONTH0x88#defineDS1302_DAY0x8A#defineDS1302_YEAR0x8C#defineDS1302_WRITE0x8E#defineDS1302_POWER0x90//DS1302写保护#defineDS1302_ENABLE0x00//写使能#defineDS1302_DISABLE0x80//写保护voidds1302_init(void);voidwrite_ds1302(ucharaddr,uchardat);ucharread_ds1302(ucharaddr);SYSTEM_TIMEread_time(void);voidset_time(SYSTEM_TIMEstr_time);#endif4.ds1302.c#includemain.h#includeds1302.h//DS1302的引脚定义sbitDS_RST=P1^2;//RST管脚sbitDS_SCLK=P1^3;//SCLK管脚sbitDS_IO=P1^4;//IO管脚#defineBCD2DEC(X)(((X&0x70)4)*10+(X&0x0F))//用于将BCD码转成十进制#defineDEC2BCD(X)((X/10)4|(X%10))//用于将十进制转成BCD码voidds1302_init(void)//初始化DS1302的引脚{DS_RST=0;DS_SCLK=0;DS_IO=0;}voidwrite_byte(uchardat)//写单字节到DS1302{uchari=0;for(i=0;i8;i++){DS_IO=(dat&1);DS_SCLK=0;_nop_();_nop_();DS_SCLK=1;//上升沿发出数据_nop_();_nop_();dat=1;}}ucharread_byte(void)//从DS1302读单字节{uchari=0;uchardat=0;uchartmp=0;for(i=0;i8;i++){DS_SCLK=1;_nop_();_nop_();DS_SCLK=0;//下降沿读出数据_nop_();_nop_();tmp=DS_IO;dat=1;//先读的是低位,移位为低位dat|=(tmp7);}dat=BCD2DEC(dat);//BCD转换return(dat);}ucharread_ds1302(ucharaddr)//从DS1302的指定位置读数据{uchartmp;DS_RST=0;DS_SCLK=0;DS_RST=1;write_byte(addr);tmp=read_byte();DS_RST=0;DS_SCLK=1;return(tmp);}voidwrite_ds1302(ucharaddr,uchardat)//写数据到DS1302的指定位置{uchartmp=0;tmp=DEC2BCD(dat);DS_RST=0;DS_SCLK=0;DS_RST=1;write_byte(addr);write_byte(tmp);DS_RST=0;DS_SCLK=1;}SYSTEM_TIMEread_time(void)//读取年、月、日、时、分、秒、星期{SYSTEM_TIMEstr_time;str_time.year=read_ds1302(DS1302_YEAR|0x01);str_time.month=read_ds1302(DS1302_MONTH|0x01);str_time.date=read_ds1302(DS1302_DATE|0x01);str_time.hour=read_ds1302(DS1302_HOUR|0x01);str_time.min=read_ds1302(DS1302_MIN|0x01);str_time.sec=read_ds1302(DS1302_SEC|0x01);str_time.day=read_ds1302(DS1302_DAY|0x01);return(str_time);}voidset_time(SYSTEM_TIMEstr_time)//置DS1302的年、月、日、时、分、秒、星期{write_ds1302(DS1302_YEAR,str_time.year);write_ds1302(DS1302_MONTH,str_time.month);write_ds1302(DS1302_DATE,str_time.date);write_ds1302(DS1302_HOUR,str_time.hour);write_ds1302(DS1302_MIN,str_time.min);write_ds1302(DS1302_SEC,str_time.sec);write_ds1302(DS1302_DAY,str_time.day);}******************************************************************************************5.DS18B20.h#ifndef_DS18B20_H#define_DS18B20_Hvoidread_temper(uchar*pbuff);voidtemper_convert(void);#endif6.DS18B20.c#includemain.h#includedelay.hsbitDQ=P1^5;//数据通信线DQvoidmaster_reset(void)//初始化DS18B20(产生复位脉冲、等待应答信号){uinti=0;DQ=1;DQ=0;delay_us(80);//拉低480~960us,这里大该80*9+8us//for(i=100;i0;i--);DQ=1;//产生上升沿i=20;while(DQ==1)//上升沿,等待应答信号(15~60us){if(i--==0)break;}i=100;while(DQ==0)//应答信号60~240us{if(i--==0)break;}delay_us(1);//短暂延时}bitsing_read_bit(void)//读取数据的一个位(足读时隙要求){bitb=0;DQ=0;//把总线拉低,低电平延时大于1us_nop_();DQ=1;//释放低电平,等待DS18B20输出数据delay_us(1);//延时15us以上,读时隙下降沿后15us,DS18B20输出数据才有效b=DQ;delay_us(5);//延时大于45us(5*9+8us)return(b);}ucharsing_read_byte(void)//读取数据的一个字节{uchari,j,b;i=0;for(i=1;i=8;i++){j=sing_read_bit();b=(j7)|(b1);}return(b);}voidsing_write_byte(ucharb)//写一个字节数据(注意写0和写1的时序){uinti=0;ucharj=0;bitbTmp=0;for(j=1;j=8;j++){bTmp=b&0x01;b=b1;//取下一位(由低位向高位)if(bTmp){DQ=0;//写1i++;i++;//延时,使得15us以内拉高DQ=1;i=8;while(i0)i--;//整个写1时限不低于60us}else{DQ=0;//写i=8;while(i0)i--;//保持低电平在60us到120us之间DQ=1;i++;i++;}}}voidtemper_convert(void)//启动温度转换{EA=0;master_reset();//初始化DS18B20(产生复位脉冲、等待应答信号)sing_write_byte(0xCC);//skiprom命令sing_write_byte(0x44);//convertT命令EA=1;}voidread_temper(uchar*pbuff)//读取温度值{uchartplsb,tpmsb;EA=0;master_reset();//初始化DS18B20(产生复位脉冲、等待应答信号)sing_write_byte(0xCC);//skiprom命令sing_write_byte(0xBE);//readscratchpad命令tplsb=sing_read_byte();//温度值低位字节(其中低4位为二进制的“小数”部分)tpmsb=sing_read_byte();//高位值高位字节(其中高5位为符号位)EA=1;*pbuff=(tpmsb4)|(tplsb4);*(pbuff+1)=(tplsb&0x0F);}******************************************************************************************7.iic.h#ifndef_IIC_H#define_IIC_H#includemain.hvoidinit_IIC(void);voidwrite_IIC(ucharadd,uchardat);ucharread_IIC(ucharadd);#endif8.iic.c#includemain.hsbitSDA=P1^1;//SDA管脚sbitSCL=P1^0;//SCL管脚voiddelay(void)//短暂延时函数,大概12um{_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();//_nop_();_nop_();}voidinit_IIC(void)//初始化IIC{SDA=1;delay();SCL=1;delay();}voidstart(void)//IIC起始信号{SDA=1;delay();SCL=1;delay();SDA=0;delay();}voidstop(void)//IIC终止信号{SDA=0;delay();SCL=1;delay();SDA=1;delay();}voidack(void)//IICACK应答信号{uchari=0;SCL=1;delay();while((SDA==1)&&(i250)){i++;}SCL=0;delay();}voidnoack(void)//IICNOACK应答信号{SDA=1;delay();SCL=1;delay();SCL=0;delay();}voidIIC_write_byte(uchardat)//IIC写数据{uchari,tmp;tmp=dat;for(i=0;i8;i++){SCL=0;delay();if(tmp&0x80){SDA=1;}el
本文标题:基于单片机AT89C51的温度自动检测系统代码及软件模拟
链接地址:https://www.777doc.com/doc-6442030 .html