#include<regx52.h>
#include<absacc.h>
#define ADCPORT XBYTE[0x1eff]
#define uchar unsigned char
#define uint unsigned int
//void delay(uint i){while(i--);}
sbit k1=P2^4; sbit k2=P2^5;
sbit k3=P2^6; sbit k4=P2^7;
sbit EOC=P3^4;
sbit RS=P2^3; //1=數據;0=命令
sbit RW=P2^4; //1=讀;0=寫
sbit E=P2^5; //1=使能;0=禁止
#define Dat1602 P0 /** 數據端口 **/
sbit BF=Dat1602^7; /** 忙信號線 **/
uint s=0,min=0,c=0,tc=250,co=20,q=0;
/** LCM忙檢測 **/
bit busy1602(){
bit busy=0; //暫存標志
Dat1602=0xff; //口線置1,防止干擾
RS=0; RW=1; //置“命令、讀”模式
E=1; E=1; //使能,寫兩次做短延時
busy=BF;E=0; //暫存標志,關閉使能
return busy; //返回忙信號,1=忙
}
/** 寫命令 參數:comDat為要發送的命令 **/
void WriteComm(uchar comDat){
while (busy1602()); //忙,則等待
RS=0;RW=0; //置“命令、寫”模式
Dat1602=comDat;E=1;E=0; //送出命令,并使之有效
}
/** 寫數據 參數:dat為要發送的數據 **/
void WriteData(uchar dat){
while (busy1602()); //忙,則等待
RS=1;RW=0; //置“數據、寫”模式
Dat1602=dat;E=1;E=0; //送出數據,并使之有效
}
/** 初始化 **/
void Init1602(){
WriteComm(0x38);//8位,兩行,5*7點陣
WriteComm(0x0c);//顯示開,光標關
WriteComm(0x06);//默認,光標右移
WriteComm(1); //清顯示
}
/** 顯示字符串 **/
void showString(uchar row,uchar col,uchar code *s){
uchar i=0;
row%=2;col%=40; //防止越界
WriteComm(0x80+row*0x40+col);//光標定位
for(;col<40&&s[i]!=0;i++,col++){WriteData(s[i]);}
}
void ADC()
{
ADCPORT=0x1e;
while(!EOC);
P0=ADCPORT;
co=P1;
}
void main()
{
TMOD=0x01;
EA=ET0=1;
TR0=1;
Init1602();
showString(0,0,"Time:12:0");
showString(1,0,"T:");
showString(1,6,"C");
showString(1,8,"Co2:");
showString(1,14,"%");
while(1)
{
//co=co*500./200;
//co=co/100;
WriteComm(0x80+9); //光標定位
WriteData(min/100+'0'); //時間顯示
WriteData(':');
WriteData(s/10%10+'0');
WriteData(s%10+'0');
WriteComm(0xc0+2); //溫度設定
WriteData(tc/100+'0');
WriteData(tc/10%10+'0');
WriteData('.');
WriteData(tc%10+'0');
WriteComm(0xc0+12);
WriteData(co/10%10+'0'); //二氧化碳濃度設定
WriteData(co%10+'0');
while(q==1)
{
WriteComm(0xc0+5);
WriteComm(0x0f);
if(k1==0)
{
while(k1==0);
tc+=5;
break;
}
if(k2==0)
{
while(k2==0);
tc-=5;
break;
}
}
while(q==2)
{
WriteComm(0xc0+13);
WriteComm(0x0f);
if(k1==0)
{
while(k1==0);
co++;
break;
}
if(k2==0)
{
while(k2==0);
co--;
break;
}
}
}
}
void time0() interrupt 1
{
TH0=0xfc;
TL0=0x17;
ADC();
if(++c==1000)
{
c=0;
s++;
if(s>59)
{
s=0;
min++;
}
}
if(k3==0)
{
while(k3==0);
q=(q+1)%3;
}
if(k4==0)
{
while(k4==0);
q=0;
}
}
|