#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);
}
}
}
|