J15,J16,J17分别和P1.3,P1.4,P1.5相连
实验代码如下,1602的驱动找度娘吧,贴上来有点乱
#include"reg52.h"
#include "typedef.h"
#include "LCD1602.H"
sbit hr202 = P1^3;
sbit ref = P1^4;
sbit test = P1^5;
unsigned long time0int = 0;
unsigned long time_ref = 0; //精确到us
unsigned long time_hr = 0;
unsigned int res_hr;
unsigned char humidity = 0;
void res2humidity(unsigned int res);
void Time0_Init()
{
TMOD = 0X01;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%6;
ET0 = 1;
EA = 1;
}
void main()
{
Lcd_Init();
Write_String(0x80,"humidity:");
Time0_Init();
while(1)
{
P1M1 = 0X00;
test = 0;//都设置为0,让电容放点
hr202 = 0;
ref = 0;
delayms(10);
//标准电阻充电
P1M1 = 0X28; //高阻输入
ref = 1;
TR0 = 1;//start time0
while(!test);
TR0 = 0;//STOP
time_ref = 50000*time0int + (TH0*256+TL0);//精确到us
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%6;
time0int = 0;
//放电
P1M1 = 0X00;
test = 0;//都设置为0,让电容放点
hr202 = 0;
ref = 0;
delayms(10);
//hr202充电
P1M1 = 0X30;
hr202 = 1;
TR0 = 1;
while(!test);
TR0 = 0;
time_hr = 50000*time0int + (TH0*256+TL0);//精确到us
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%6;
time0int = 0;
//计算hr202电阻
res_hr = time_hr*100/time_ref; //结果扩大了10倍
res2humidity(res_hr);
Write_Char(0xc0,humidity/10+48);
Write_Char(0xc1,humidity+48);
}
}
//查表函数
void res2humidity(unsigned int res)
{
unsigned char humidity10 = 0; //十位
unsigned char humidity1 = 0;//个位
if((res>28)&&(res<50000))//湿度范围在20%~90%之间
{
if((res>28)&&(res<40))
{humidity10 = 8;humidity1 = 10 - (res-28)*0.4;} //按直线关系处理 数据不是太准确
if((res>40)&&(res<57))
{humidity10 = 8;humidity1 = 5 - (res-40)*0.29;}
if((res>57)&&(res<84))
{humidity10 = 7;humidity1 = 10 - (res-57)*0.185;}
if((res>84)&&(res<130))
{humidity10 = 7;humidity1 = 5 - (res-84)*0.11;}
if((res>130)&&(res<195))
{humidity10 = 6;humidity1 = 10 - (res-130)*0.076;}
if((res>195)&&(res<310))
{humidity10 = 6;humidity1 = 5 - (res-195)*0.043;}
if((res>310)&&(res<490))
{humidity10 = 5;humidity1 = 10 - (res-310)*0.027;}
if((res>490)&&(res<870))
{humidity10 = 5;humidity1 = 5 - (res-490)*0.0131;}
if((res>870)&&(res<1600))
{humidity10 = 4;humidity1 = 10 - (res-870)*0.0068;}
if((res>1600)&&(res<3100))
{humidity10 = 4;humidity1 = 5 - (res/100-16)*0.33;}
if((res>3100)&&(res<6300))
{humidity10 = 3;humidity1 = 10 - (res/100-31)*0.156;}
if((res>6300)&&(res<13000))
{humidity10 = 3;humidity1 = 5 - (res/100-63)*0.07;}
if((res>13000)&&(res<26000))
{humidity10 = 2;humidity1 = 10 - (res/1000-13)*0.38;}
if((res>26000)&&(res<5000))
{humidity10 = 2;humidity1 = 5 - (res/1000-26)*0.208;}
}
humidity = humidity10*10+humidity1;
}
void time0_ser() interrupt 1
{
time0int++;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%6;
}
感觉有用的话请点个赞