#include "reg52.H"
#include "LCD5510.H"
#include "intrins.h"
#include "absacc.h"
#define uint unsigned int
#define uchar unsigned char
uchar tem0,tem1; //用于存放50ms中所記脈沖個數的高低字節
uchar temp0,temp1; //用于存放最終換算出的濕度的高低字節
uint f=0; //頻率初值
void delay1s(void)
{
unsigned char i,j,k;
for(i=167; i>0; i--)
for(j=171; j>0; j--)
for(k=16; k>0; k--)
{
;
}
}
void Init_timer()
{
TMOD=0x51; //0101 0001 定時器1在方式1下工作16位計數器 定時器0在方式1下工作16位定時器
TL0=0x00; //定時器0初值 定時50000us
TH0=0x4C;
TL1=0x00; //定時器1清零
TH1=0x00;
ET0=1; //使能定時器0中斷
ET1=1; //使能定時器1中斷
EA=1; //使能總中斷
TR0=1; //開始計時
TR1=1;
}
void tran()
{
f=tem1;
f=(f<<8)|tem0; //計算出50ms中所記脈沖個數
f=f*20; //50ms*20=1000ms=1s 1秒鐘所記脈沖個數等于頻率
if((5623<=f)&&(f<=6852)) //不同頻率對于不同相對濕度 相對濕度的有效范圍為(0%--100%)
{
if((6734<f)&&(f<=6852))
{temp0=0;temp1=(6852-f)*10/118;}
if((6618<f)&&(f<=6734))
{temp0=1;temp1=(6734-f)*10/116;}
if((6503<f)&&(f<=6618))
{temp0=2;temp1=(6618-f)*10/115;}
if((6388<f)&&(f<=6503))
{temp0=3;temp1=(6503-f)*10/115;}
if((6271<f)&&(f<=6388))
{temp0=4;temp1=(6388-f)*10/117;}
if((6152<f)&&(f<=6271))
{temp0=5;temp1=(6271-f)*10/119;}
if((6029<f)&&(f<=6152))
{temp0=6;temp1=(6152-f)*10/123;}
if((5901<f)&&(f<=6029))
{temp0=7;temp1=(6029-f)*10/128;}
if((5766<f)&&(f<=5901))
{temp0=8;temp1=(5901-f)*10/135;}
if((5623<f)&&(f<=5766))
{temp0=9;temp1=(5766-f)*10/143;}
}
else
{
temp0=0;temp1=0;
}
}
void main(void)
{
uchar i, humi=0;
uchar LCD_contrast = 0xc8; //此值對比度合適
Init_timer();
LCD5510_Init();
LCD_write_cmd(0x21);//工作模式, 水平尋址, 擴展指令
LCD_write_cmd(0xbe);//對比度調節
LCD_write_cmd(0x20);//工作模式, 水平尋址, 常規指令
while(1)
{
tran();
temp0&=0x0F;
temp1&=0x0F;
temp0=temp0<<4;
humi=temp0|temp1;
LCD_clr_scr(); //清屏
// LCD_printn(2, 2, humi, 3);
delay1s();
delay1s();
}
}
void timer0() interrupt 1
{
EA =0;
TR0=0;
TR1=0;
TL0=0xFF; //重裝初值 定時50000us OX4BFFH
TH0=0x4B;
tem0=TL1; //讀取50ms中所記脈沖個數
tem1=TH1;
TL1=0x00; //定時器1清零
TH1=0x00;
//f=1; //作標注位
TR0=1;
TR1=1;
EA=1;
}
void timer1() interrupt 3 //T1中斷,表示計數的脈沖溢出,超出了可測量的頻率范圍(在50ms內計數達65535)
{ //顯然在這里不可能。所以重新啟動。
EA =0;
TR0=0;
TR1=0;
TL0=0x00; //重裝初值 定時50000us
TH0=0x4C;
TL1=0x00; //定時器1清零
TH1=0x00;
TR0=1;
TR1=1;
EA=1;
}
|