本项目是基于51单片机的仿真实验 系统开启后,可以选择直接开始工作或者预约时间开始工作 温度上下限可通过按键设置调节 当温度低于设置的温度下限时,电机启动加热,指示灯亮起 当温度处于设定的温度上下限时,绿灯亮起 当温度高于设定的温度上限时,蓝色指示灯亮起 通过压力传感器检测水压,以实现自动加水功能 当水压小于设定值100的时候,即系统检测到缺水的时候,指示灯亮起,自动加水,水压达到设定值的时候,系统自动停止续水
部分代码:
/***********************************lcd显示**************************/
void LcdDisplay(int temp) //LCD显示
{
unsigned char a,b,c,d,e,f,g,h;
float t;
if(temp< 0) //温度为负
{
LcdWriteCom(0xd5);
LcdWriteData('-');
temp=~temp;
temp=temp+1;
t=temp*0.0625;
temp=t*100+0.5;
}
else //温度为正
{
LcdWriteCom(0xd5);
LcdWriteData('+');
t=temp*0.0625;
temp=t*100; }
if(t<=lowr)
{flag=1;} //flag作为标志位,由温度控制
if((t<high)&&(t>lowr))
{flag=2;}
if(t>=high)
{flag=3;}
if(Out<100)
{flag_add=1;}
f=Out/100;
g=Out%100/10;
h=Out%100%10;
e=temp % 100000/10000;
a=temp % 10000/1000; //十位
b=temp % 1000/100;
c=temp % 100/10;
d=temp % 10;
high_L=high%10;
high_H=(high%100)/10;
high_Z=high/100;
lowr_L=lowr%10;
lowr_H=lowr/10;
miao_L = miao%10;
miao_H = miao/10;
fen_L = fen%10;
fen_H = fen/10;
shi_L = shi%10;
shi_H = shi/10;
LcdWriteCom(0xd6);
LcdWriteData('0'+e);
SBUF='0'+e;
TI=0;
LcdWriteCom(0xd7);
LcdWriteData('0'+a); //十位
SBUF='0'+a; //将接收的数据发送到寄存器
while (!TI); //等待数据完成
TI=0;
LcdWriteCom(0xd8);
LcdWriteData('0'+b); //个位
SBUF='0'+b;
TI=0;
LcdWriteCom(0xda);
LcdWriteData('0'+c);
SBUF='0'+c;
TI=0;
LcdWriteCom(0xdb);
LcdWriteData('0'+d);
SBUF='0'+d;
TI=0;
LcdWriteCom(0x8b);
LcdWriteData('0'+f);
SBUF='0'+f;
TI=0;
LcdWriteCom(0xc);
LcdWriteData('0'+g);
SBUF='0'+g;
TI=0;
LcdWriteCom(0x8d);
LcdWriteData('0'+h);
SBUF='0'+h;
TI=0;
LcdWriteCom(0x94);
LcdWriteData('0'+high_Z);
SBUF='0'+high_Z;
TI=0;
LcdWriteCom(0x95);
LcdWriteData('0'+high_H);
SBUF='0'+high_H;
TI=0;
LcdWriteCom(0x96);
LcdWriteData('0'+high_L);
SBUF='0'+high_L;
TI=0;
LcdWriteCom(0x9e);
LcdWriteData('0'+lowr_H);
SBUF='0'+lowr_H;
TI=0;
LcdWriteCom(0x9f);
LcdWriteData('0'+lowr_L);
SBUF='0'+lowr_L;
TI=0;
LcdWriteCom(0xcb);
LcdWriteData('0'+fen_H);
SBUF='0'+fen_H;
TI=0;
LcdWriteCom(0xcc);
LcdWriteData('0'+fen_L);
SBUF='0'+fen_L;
TI=0;
LcdWriteCom(0xce);
LcdWriteData('0'+miao_H);
SBUF='0'+miao_H;
TI=0;
LcdWriteCom(0xcf);
LcdWriteData('0'+miao_L);
SBUF='0'+miao_L;
TI=0;
}
void zf() //字符显示
{ uchar code table1[]="20wlw2JXJ";
uchar code table2[]="StartTime:" ;
uchar code table3[]="Hig:" ;
uchar code table4[]="Low:" ;
uchar code table5[]="Temp:" ;
LcdWriteCom(0x80);
for(r=0;r<10;r++)
{
LcdWriteData(table1[r]);
DelayMs(1);
}
//第二行
LcdWriteCom(0xc0);
for(r=0;r<16;r++)
{
LcdWriteData(table2[r]);
DelayMs(1);
}
LcdWriteCom(0xcd);
LcdWriteData(':');
//第三行
LcdWriteCom(0x90);
for(r=0;r<5;r++)
{
LcdWriteData(table3[r]);
DelayMs(1);
}
LcdWriteCom(0x9a);
for(r=0;r<16;r++)
{
LcdWriteData(table4[r]);
DelayMs(1);
}
//第四行
LcdWriteCom(0xd0);
for(r=0;r<5;r++)
{
LcdWriteData(table5[r]);
DelayMs(1);
}
LcdWriteCom(0xd9);
LcdWriteData('.');
LcdWriteCom(0xdc);
LcdWriteData(0xdf);
LcdWriteCom(0xdd);
LcdWriteData('C');
}
void DelayMs(uchar n) //1毫秒
{uchar j;
while(n--)
{
for(j=0;j<113;j++);
}}
void LcdWriteCom(uchar com) //LCD写入命令,进行LCD的位置选择
{
LCD1602_E=0;
LCD1602_RS=0; //选择发送命令
LCD1602_RW=0; //选择写入
P0=com; //将数据写到P0端口
DelayMs(1); //等待
LCD1602_E=1; //完成写入
DelayMs(1);
LCD1602_E=0;
}
void LcdWriteData(uchar dat) //LCD写入数据
{
LCD1602_E=0;
LCD1602_RS=1; //选择写入数据
LCD1602_RW=0; //选择写入
P0=dat;
DelayMs(1);
LCD1602_E=1;
DelayMs(1); //等待写入完成
LCD1602_E=0;
}
void LcdInit() //LCD的初始化
{
LcdWriteCom(0x38); //开显示
LcdWriteCom(0x0c); //不显示光标
LcdWriteCom(0x06);
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //指针起点位置
}
void Delay1ms(unsigned int y) //延时
{
unsigned int x;
for(y;y>0;y--)
for(x=110;x>0;x--);
}
/*************************************温度传感器**************************/
unsigned char Ds18b20Init() //DS18B20初始化
{
unsigned int i;
DQ=0; //将总线拉低480~960us
i=70;
while(i--);
DQ=1;
i=0;
while(DQ) //等待DS18B20拉拉低总线
{
i++;
if(i>5000) //等待>5MS
return 0; //初始化失败
}
return 1; //成功
}
void Ds18b20WriteByte(unsigned char dat) //写入一个字节
{
unsigned int i,j;
for(j=0;j<8;j++)
{
DQ=0; //根据时序
i++; //1us
DQ=dat&0x01; //最低为开始
i=6;
while(i--); //释放总线最少60us
DQ=1;
dat>>=1;
}
}
unsigned char Ds18b20ReadByte() //都一个字节
{
unsigned char byte,bi;
unsigned int i,j;
for(j=8;j>0;j--)
{DQ=0;
i++;
DQ=1;
i++; i++;
bi=DQ;
byte=(byte>>1)|(bi<<7);
i=4;
while(i--);
}
return byte;
}
int Ds18b20ReadTemp() //温度读取
{
int temp=0;
unsigned char hig,low;
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0x44); //温度转换命令
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0xbe); //发送读取温度命令
low=Ds18b20ReadByte(); //低8位
hig=Ds18b20ReadByte(); //高8位
temp=hig;
temp<<=8;
temp|=low; //合并
return temp;
}