#include <REGX51.H>
#include <intrins.h>
#include <math.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit Bus=P1^0;
sbit k1=P3^4; //按键
sbit k2=P3^5; //按键
sbit k3=P3^6; //按键
sbit k4=P3^7; //按键
sbit beep=P1^1;
sbit RS = P2^0;
sbit RW = P2^1;
sbit E = P2^2;
sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口
sbit CS = P1^2;
sbit Clk = P1^3;
sbit DATI = P1^4;
sbit DATO = P1^4;
unsigned char dat = 0x00;
unsigned char count = 0x00;
unsigned char CH;
//uchar temp,humi;
uchar error; //用于检验是否出现错误
uchar checksum,nn; //CRC
uchar nn; //蜂鸣器
uchar TEMP_data[6]; //用于记录实测的温度
uchar CO2_data[6]; //用于记录实测的温度
uchar HUMI_data[5]; //用于记录实测的湿度
uchar set_temp[5]; //保存设定的温度
uchar set_humi[4]; //保存设定的湿度
uchar set_pm[5];
uint pm_high=400; //湿度上限变量
uint pm_low=100; //湿度下限变量
uint temp_high=30; //温度上限变量
uint temp_low=20; //温度下限变量
uint humi_high=30; //湿度上限变量
uint humi_low=20; //湿度下限变量
uchar presskeynum; //按键次数变量
#define DELAY_TIME 5
uchar value[5];
uint temp,hum;
/******** 1602函数声明 ********/
void LCD_Initial();
void GotoXY(unsigned char x, unsigned char y);
void Print(unsigned char *str);
void LCD_Write(bit style, unsigned char input);
/*************************************************************************/
void Delay10us(void);//10us延时函数
void Delay20us(void);//20us延时函数
void Delay1ms(void);//1ms延时函数
void Read_value(void);//读值子函数
void Show(uint x,y);//显示子函数
/***********************10us延时函数*****************************/
void Delay10us()
{
uchar i;
i=2;
while(--i);
}
/***********************20us延时函数*****************************/
void Delay20us(void) //误差 0us
{
unsigned char a,b;
for(b=3;b>0;b--)
for(a=1;a>0;a--);
}
/***********************1ms延时函数*****************************/
void Delay1ms() //误差 0us
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
/************************读值函数******************************/
void Read_value()
{
uchar i,j=0,mask;
Bus=0;//主机拉低总线至少1ms(20ms)
for(i=0;i<20;i++)
{
Delay1ms();
}
Bus=1;//主机主动拉高20us
while(Bus);//等待从机拉低总线
while(!Bus);//度过从机拉低时间
while(Bus);//度过从机拉高时间
while(j<5)
{
mask=0x80;
for(i=0;i<8;i++)
{
while(!Bus);//度过数据位的低电平
Delay20us();//延迟40us后进行电平检测
Delay20us();
if(Bus==0)
{
value[j]&=(~mask);
}
else
{
value[j]|=mask;
}
mask>>=1;
while(Bus);//度过剩余高电平(如果是1的话)
}
j++;
}
}
//1602程序
//内部等待函数*************************************************************
unsigned char LCD_Wait(void)
{
RS=0;
RW=1; _nop_();
E=1; _nop_();
E=0;
return DBPort;
}
//向LCD写入命令或数据********************************************************
#define LCD_COMMAND 0 // Command
#define LCD_DATA 1 // Data
#define LCD_CLEAR_SCREEN 0x01 // 清屏
#define LCD_HOMING 0x02 // 光标返回原点
void LCD_Write(bit style, unsigned char input)
{
E=0;
RS=style;
RW=0; _nop_();
DBPort=input; _nop_();//注意顺序
E=1; _nop_();//注意顺序
E=0; _nop_();
LCD_Wait();
}
//设置显示模式************************************************************
#define LCD_SHOW 0x04 //显示开
#define LCD_HIDE 0x00 //显示关
#define LCD_CURSOR 0x02 //显示光标
#define LCD_NO_CURSOR 0x00 //无光标
#define LCD_FLASH 0x01 //光标闪动
#define LCD_NO_FLASH 0x00 //光标不闪动
void LCD_SetDisplay(unsigned char DisplayMode)
{
LCD_Write(LCD_COMMAND, 0x08|DisplayMode);
}
//设置输入模式************************************************************
#define LCD_AC_UP 0x02
#define LCD_AC_DOWN 0x00 // default
#define LCD_MOVE 0x01 // 画面可平移
#define LCD_NO_MOVE 0x00 //default
void LCD_SetInput(unsigned char InputMode)
{
LCD_Write(LCD_COMMAND, 0x04|InputMode);
}
//初始化LCD************************************************************
void LCD_Initial()
{
E=0;
LCD_Write(LCD_COMMAND,0x38); //8位数据端口,2行显示,5*7点阵
LCD_Write(LCD_COMMAND,0x38);
LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //开启显示, 无光标
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC递增, 画面不动
}
//液晶字符输入的位置************************
void GotoXY(unsigned char x, unsigned char y)
{
if(y==0)
LCD_Write(LCD_COMMAND,0x80|x);
if(y==1)
LCD_Write(LCD_COMMAND,0x80|(x-0x40));
}
//将字符输出到液晶显示
void Print(unsigned char *str)
{
while(*str!='\0')
{
LCD_Write(LCD_DATA,*str);
str++;
}
}
unsigned char adc0832(unsigned char CH)
{
unsigned char i,test,adval;
adval = 0x00;
test = 0x00;
Clk = 0;
DATI = 1;
_nop_();
CS = 0;
_nop_();
Clk = 1;
_nop_();
if ( CH == 0x00 )
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 0;
_nop_();
Clk = 1;
_nop_();
}
else
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
}
Clk = 0;
DATI = 1;
for( i = 0;i < 8;i++ )
{
_nop_();
adval <<= 1;
Clk = 1;
_nop_();
Clk = 0;
if (DATO)
adval |= 0x01;
else
adval |= 0x00;
}
for (i = 0; i < 8; i++)
{
test >>= 1;
if (DATO)
test |= 0x80;
else
test |= 0x00;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
}
if (adval == test)
dat = test;
_nop_();
CS = 1;
DATO = 1;
Clk = 1;
return dat;
}
void delay(uint z) //延时函数z为毫秒数
{
int a,b;
for(a=z;a>0;a--)
for(b=120;b>0;b--);
}
void key()
{
if (k1==0) //调整按键检测
{
delay(10);
if (k1==0)
{
presskeynum++;
if(presskeynum==7)
presskeynum=0;
while(k1==0);
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
}
}
if(presskeynum==1)//温度上限设置
{
if(k2==0)// 加
{
delay(10);
temp_high++;
if (temp_high>99)
temp_high=0;
while(k2==0); //加上此句必须松按键才处理
}
if(k3==0)//减
{
delay(10);
temp_high--;
if (temp_high<0)
temp_high=99;
while(k3==0); //加上此句必须松按键才处理
}
}
if(presskeynum==2)//温度下限设置
{
if(k2==0)// 加
{
delay(10);
temp_low++;
if (temp_low>99)
temp_low=0;
while(k2==0);
}
if(k3==0)
{
delay(10);
temp_low--;
if (temp_low<0)
temp_low=99;
while(k3==0);
}
}
if(presskeynum==3)//湿度上限设置
{
if(k2==0)// 加
{
delay(10);
humi_high++;
if (humi_high>99)
humi_high=0;
while(k2==0);
}
if(k3==0)
{
delay(10);
humi_high--;
if (humi_high<0)
humi_high=99;
while(k3==0);
}
}
if(presskeynum==4)//湿度下限设置
{
if(k2==0)//加
{
delay(10);
humi_low++;
if (humi_low>99)
humi_low=0;
while(k2==0);
}
if(k3==0)// 减
{
delay(10);
humi_low--;
if (humi_low<0)
humi_low=99;
while(k3==0);
}
}
if(presskeynum==5)//湿度下限设置
{
if(k2==0)//加
{
delay(10);
pm_high++;
if (pm_high>999)
pm_high=0;
while(k2==0);
}
if(k3==0)// 减
{
delay(10);
pm_high--;
if (pm_high<0)
pm_high=999;
while(k3==0);
}
}
if(presskeynum==6)//湿度下限设置
{
if(k2==0)//加
{
delay(10);
pm_low++;
if (pm_low>999)
pm_low=0;
while(k2==0);
}
if(k3==0)// 减
{
delay(10);
pm_low--;
if (pm_low<0)
pm_low=999;
while(k3==0);
}
}
if(k4==0)// 退出键
{
delay(10);
if(k4==0)
{
while(k4==0);
presskeynum=0;
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
}
}
}
void main() //主函数
{
int ch0_temp;
k1=k2=k3=k4=1;
LCD_Initial(); //液晶初始化
while(1)
{
key();
if(presskeynum==0)
{
ch0_temp=adc0832(0)*1.0*500/255;
Read_value();//读值
hum=value[0]*256+value[1];//计算湿度值
temp=(value[2]&0x7f)*256+value[3];//如果温度为负值,则&0x3f去掉符号位后再计算;若为正值则无影响
/* if (((temp)<temp_low*10)|(temp>temp_high*10)|((hum)<humi_low*10))
{
for (nn=0;nn<50;nn++)
{
beep=0;
delay(1);
beep=1;
delay(1);
}
}
if ((hum)>humi_high*10)
{
for (nn=0;nn<10;nn++)
{
beep=0;
delay(1);
beep=1;
delay(1);
}
}
if (ch0_temp>pm_high)
{
for (nn=0;nn<10;nn++)
{
beep=0;
delay(1);
beep=1;
delay(1);
}
}
if (ch0_temp<pm_low)
{
for (nn=0;nn<10;nn++)
{
beep=0;
delay(1);
beep=1;
delay(1);
}
} */
GotoXY(0,0); //选择温度显示位置
Print("T:"); //5格空格
GotoXY(0,1); //选择湿度显示位置
Print("H:"); //5格空格
GotoXY(2,0);
//设置温度显示位置
TEMP_data[0]=temp/1000+'0'; //温度十位
TEMP_data[1]=(temp%1000)/100+'0'; //温度个位
TEMP_data[2]=(temp%1000)%100/10+'0'; //温度个位
TEMP_data[3]='.'; //温度十位
TEMP_data[4]=temp%10+'0'; //温度个位
TEMP_data[5]=0xdf; //显示温度符号℃
TEMP_data[6]='C'; //显示温度符号℃
TEMP_data[7]='\0';
Print(TEMP_data); //输出温度
GotoXY(10,0); //设置温度显示位置
CO2_data[0]='C';
CO2_data[1]=':';
CO2_data[2]=ch0_temp/100+'0';
CO2_data[3]=ch0_temp%100/10+'0';
CO2_data[4]='.';
CO2_data[5]=ch0_temp%10+'0';
CO2_data[6]='\0';
Print(CO2_data); //输出温度
GotoXY(2,1);
HUMI_data[0]=hum/1000+'0'; //湿度十位
HUMI_data[1]=hum%1000/100+'0'; //湿度个位
HUMI_data[2]=hum%1000%100/10+'0'; //湿度个位
HUMI_data[3]='.'; //湿度十位
HUMI_data[4]=hum%10+'0'; //湿度个位
HUMI_data[5]='%';
HUMI_data[6]='\0';
Print(HUMI_data); //输出湿度
}
if(presskeynum==1) //设定温度上限时显示的内容
{
set_temp[0]=temp_high/10+0x30;
set_temp[1]=temp_high%10+0x30;
set_temp[2]=0xdf;
set_temp[3]='C';
set_temp[4]='\0';
GotoXY (0,0);
Print("SET TEMP HIGH:");
GotoXY (0,1);
Print(set_temp);
}
if(presskeynum==2) //设定温度下限时显示的内容
{
set_temp[0]=temp_low/10+0x30;
set_temp[1]=temp_low%10+0x30;
set_temp[2]=0xdf;
set_temp[3]='C';
set_temp[4]='\0';
GotoXY (0,0);
Print("SET TEMP LOW:");
GotoXY (0,1);
Print(set_temp);
}
if(presskeynum==3) //设定湿度上限时显示的内容
{
set_humi[0]=humi_high/10+0x30;
set_humi[1]=humi_high%10+0x30;
set_humi[2]='%';
set_humi[3]='\0';
GotoXY (0,0);
Print("SET HUMI HIGH:");
GotoXY (0,1);
Print(set_humi);
}
if(presskeynum==4) //设定湿度下限时显示的内容
{
set_humi[0]=humi_low/10+0x30;
set_humi[1]=humi_low%10+0x30;
set_humi[2]='%';
set_humi[3]='\0';
GotoXY (0,0);
Print("SET HUMI LOW:");
GotoXY (0,1);
Print(set_humi);
}
if(presskeynum==5) //设定湿度下限时显示的内容
{
set_pm[0]=pm_high/100+0x30;
set_pm[1]=pm_high%100/10+0x30;
set_pm[2]='.';
set_pm[3]=pm_high%10+0x30;
set_pm[4]='\0';
GotoXY (0,0);
Print("SET HCO HIGH:");
GotoXY (0,1);
Print(set_pm);
}
if(presskeynum==6) //设定湿度下限时显示的内容
{
set_pm[0]=pm_low/100+0x30;
set_pm[1]=pm_low%100/10+0x30;
set_pm[2]='.';
set_pm[3]=pm_low%10+0x30;
set_pm[4]='\0';
GotoXY (0,0);
Print("SET HCO LOW:");
GotoXY (0,1);
Print(set_pm);
}
}
}
|