为了响应国家环保要求,拆锅炉了改电了。程序改自网络,DS18B20测温,设置启动和停止温度,用继电器输出控制电暖器的自动启停。
以下为程序代码:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
unsigned int xs;
unsigned int temp;
unsigned int temp1;
unsigned int temp2;
unsigned int sxwd;
unsigned int xxwd;
unsigned int sjwdsw;
unsigned int sjwdgw;
unsigned int sjwdbw;
unsigned char flag;
unsigned int sxbw=1;
unsigned int sxsw=5;
unsigned int xxbw=0;
unsigned int xxsw=9;
void guangoukongzhi();
void shezhiwendusx();
void shezhiwenduxx();
void sxshezhi();
void xxshezhi();
sbit DQ=P1^0;
sbit wx4=P3^4;
sbit wx3=P3^5;
sbit wx2=P3^6;
sbit wx1=P3^7;
sbit shuchu=P1^4;
sbit qiehuan=P1^5;
sbit jia=P1^6;
sbit jia1=P1^7;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0X88,0x83,0xc6,0xa1,0x86,0x8e,0x7f,0x89,0xc7};
void delay1(unsigned int m)//oáÃëÑóê±
{
unsigned int i,j;
for(i=m;i>0;i--)
for(j=110;j>0;j--);
}
void delay(unsigned int m)//ζèÑóê±3ìDò
{
while(m--);
}
void Init_DS18B20()//3õê¼»ˉ
{
unsigned char x=0;
DQ=1;//DQ¸′λ
delay(8);
DQ=0;//μ¥Ƭ»ú½«QDà-μí
delay(80);
DQ=1;//à-¸ßDQ
delay(4);
x=DQ;//éÔ×öÑóê±èç1ûX=0Ôò3õê¼»ˉ3é1| X=1Ôò3õê¼»ˉ꧰ü
delay(20);
}
uchar ReadOneChar()//ds18b20¶áò»¸ö×Ö½ú
{
unsigned char i=0;
unsigned char dat=0;
for(i=8;i>0;i--)
{
DQ=0;//¸ßμçƽà-3éμíμçƽ걶áÖüÆú¿aê¼
dat>>=1;
DQ=1;//¸øÂö3å
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);}
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
DQ=0;//′ó¸ßμçƽà-μ½μíμçƽê±D′ÖüÆú¿aê¼
DQ=dat&0x01;
delay(5);
DQ=1;//′ó×îμíλμ½×î¸ßλ′«èë
dat>>=1;
}
}
void ReadTemperature()
{
unsigned char a=0;
unsigned b=0;
unsigned t=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
delay(5);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
delay(5);
a=ReadOneChar();
b=ReadOneChar();
temp1=b<<4;
temp1+=(a&0xf0)>>4;
temp2=a&0x0f;
temp=((b*256+a)>>4);
xs=temp2*0.0625*10;
}
void wenduxianshi()
{
wx1=0;
P0=table[temp/10];
delay1(3);
wx1=1;
wx2=0;
P0=table[temp%10];
delay1(3);
wx2=1;
wx2=0;
P0=table[16];
delay1(3);
wx2=1;
P0=0x0ff;
delay1(3);
wx3=0;
P0=table[xs%10];
delay1(3);
wx3=1;
wx4=0;
P0=table[12];
delay1(3);
wx4=1;
}
void main()
{
while(1)
{
ReadTemperature();
guangoukongzhi();
if(qiehuan==0)
{ delay1(80);
if(qiehuan==0)
{
flag++;
}
}
if(flag==3)
{
flag=0;
}
if(flag==0)
{
wenduxianshi();
}
if(flag==1)
{
shezhiwendusx();
}
if(flag==2)
{
shezhiwenduxx();
}
}
}
void guangoukongzhi()
{
sjwdsw=temp%10;
sjwdbw=temp/10;
if(sjwdbw==xxbw)
{
if(sjwdsw<=xxsw)
{ shuchu=0;
}
}
if(sjwdbw>=sxbw)
{
if(sjwdsw>=sxsw)
{
shuchu=1;
}
}
}
void shezhiwendusx()
{
wx1=0;
P0=0x89;
delay1(3);
wx1=1;
wx2=0;
P0=table[sxbw];
delay1(3);
wx2=1;
wx3=0;
P0=table[sxsw];
delay1(3);
wx3=1;
wx4=0;
P0=table[12];
delay1(3);
wx4=1;
if(jia==0)//éÏÏT°ùλ¼ó′|àí
{
delay1(80);
{
if(jia==0)
{
sxbw++;
if(sxbw>=4)
{
sxbw=0;
}
}
}
}
//if(jian==0)//éÏÏT°ùλ¼õ′|àí
//{
//delay1(80);
//{
//if(jian==0)
//{
//if(sxbw>0)
// {sxbw--;
// if(sxbw<=0)
//{
// sxbw=3;
// }
//}
//}
//}
//}
if(jia1==0)//éÏÏTê®Î»¼ó′|àí
{
delay1(80);
{
if(jia1==0)
{
sxsw++;
if(sxsw>9)
{
sxsw=0;
}
}
}
}
//if(jian1==0)//éÏÏTê®Î»¼õ′|àí
//{
//delay1(80);
//{
//if(jian1==0)
//{
//if(sxsw>0)
// {sxsw--;
// if(sxsw=0)
// {
// sxsw=9;
// }
//}
//}
//}
//}
}
void shezhiwenduxx()
{
wx1=0;
P0=0xc7;
delay1(3);
wx1=1;
wx2=0;
P0=table[xxbw];
delay1(3);
wx2=1;
wx3=0;
P0=table[xxsw];
delay1(3);
wx3=1;
wx4=0;
P0=table[12];
delay1(3);
wx4=1;
if(jia==0)//ÏÂÏT°ùλ¼ó′|àí
{
delay1(80);
{
if(jia==0)
{
xxbw++;
if(xxbw>=2)
{
xxbw=0;
}
}
}
}
if(jia1==0)//ÏÂÏTê®Î»¼ó′|àí
{
delay1(80);
{
if(jia1==0)
{
xxsw++;
if(xxsw>9)
{
xxsw=0;
}
}
}
}
}
|