第一部分
来源:https://www.amobbs.com/thread-5561786-1-1.html?_dsign=d5a64904
第二部分
#include 'SystemInit.h'
#include 'ADC.h'
#include 'Delay.h'
#include 'math.h'
unsigned char humidity(unsigned char tp)
{
float xdata voltage,hr202_r;
float xdata humidity;
voltage=ADC0_conver(0);
hr202_r=333*voltage/(1.370435-voltage);
if(tp>32)
{
if(tp>42)
{
if(tp>52)
{
if(tp>57)//58,59,60
{
humidity=-0.0772*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.7654*log(hr202_r)*log(hr202_r)-19.4763*log(hr202_r)+95.8524;
}
else//53,54,55,56,57
{
humidity=-0.0329*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.2334*log(hr202_r)*log(hr202_r)-17.807*log(hr202_r)+96.2048;
}
}
else
{
if(tp>47)//48,49,50,51,52
{
humidity=-0.0481*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.3541*log(hr202_r)*log(hr202_r)-17.9649*log(hr202_r)+97.8534;
}
else//43,44,45,46,47
{
humidity=-0.0582*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.4489*log(hr202_r)*log(hr202_r)-18.2932*log(hr202_r)+100.2677;
}
}
}
else
{
if(tp>37)//38,39,40,41,42
{
humidity=-0.0707*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.624*log(hr202_r)*log(hr202_r)-18.9898*log(hr202_r)+102.8252;
}
else//33,34,35,36,37
{
humidity=-0.0904*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.8378*log(hr202_r)*log(hr202_r)-19.4935*log(hr202_r)+104.6549;
}
}
}
else
{
if(tp>12)
{
if(tp>22)
{
if(tp>27)//28,29,30,31,32
{
humidity=-0.0761*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.6564*log(hr202_r)*log(hr202_r)-18.916*log(hr202_r)+106.2522;
}
else//23,24,25,26,27
{
humidity=-0.0709*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.56*log(hr202_r)*log(hr202_r)-18.4088*log(hr202_r)+107.6625;
}
}
else
{
if(tp>17)//18,19,20,21,22
{
humidity=-0.0728*log(hr202_r)*log(hr202_r)*log(hr202_r)+1.5919*log(hr202_r)*log(hr202_r)-18.6313*log(hr202_r)+110.4371;
}
else//13,14,15,16,17
{
humidity=0.4834*log(hr202_r)*log(hr202_r)-13.751*log(hr202_r)+106.816;
}
}
}
else
{
if(tp>2)
{
if(tp>7)//8,9,10,11,12
{
humidity=0.5149*log(hr202_r)*log(hr202_r)-14.0457*log(hr202_r)+109.7371;
}
else//3,4,5,6,7
{
humidity=0.4746*log(hr202_r)*log(hr202_r)-13.6355*log(hr202_r)+111.0428;
}
}
else//0,1,2
{
humidity=0.5167*log(hr202_r)*log(hr202_r)-14.1712*log(hr202_r)+115.1111;
}
}
}
return humidity;
// humidity=69.292-0.11094*tp-0.017414*hr202_r-0.0013279*tp*tp-0.00020088*tp*hr202_r+0.0000016633*hr202_r*hr202_r;
}
main()
{
Init();
ADC_init(0);
delay_ms(1);
humidity(25);
for(;;);
}
第三部分
#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;
}
上一篇:STM8 HSI与HSE自动切换时钟源
下一篇:复位电路异同及操作
设计资源 培训 开发板 精华推荐
- MIKROE-2806,降压-升压 CLICK 板采用 LTC3129-1 高效降压-升压 DC/DC 转换器
- NBC124XXEVB,PLL 合成时钟发生器评估板
- 使用 ROHM Semiconductor 的 BD95841MUV 的参考设计
- S5U13506B00C000,基于S1D13705F00A嵌入式存储器LCD控制器的开发套件
- LT3973EDD-3.3 1.21V 降压转换器的典型应用
- 具有平均模式恒流控制的 TS19460 AC/DC WLED 驱动器的典型应用
- 使用 Analog Devices 的 LTC1450CN 的参考设计
- 【训练营】仿生机器狗
- WIFI无线温湿度传感节点
- SPT7852 是一款单片双路 10 位 ADC,可以以高达 20 MSPS 的速率对输入进行采样