#define CAL_PPM 10 // 校准环境中PPM值
#define RL 10 // RL阻值
static float R0 = 8.00;
//得到ADC采样内部传感器的值
//取10次,然后平均
vu16 Get_ADCValue_MQ7(void)
{
u32 val = 0;
u8 times = 10;
u8 count;
for(count = 0; count < times; count++)
{
val += ADC_ConvertedValue[1];
delay_ms(5);
}
return val/times;
}
// 传感器校准函数
void MQ7_PPM_Calibration(float RS)
{
R0 = RS / pow(CAL_PPM / 98.322, 1 / -1.458f);
}
// 获取传感器的值
float MQ7_GetPPM(void)
{
float Vrl = 3.3f * Get_ADCValue_MQ7() / 4096.f;
Vrl = ( (float)( (int)( (Vrl+0.005)*100 ) ) )/100;
float RS = (3.3f - Vrl) / Vrl * RL;
// printf("MQ7_VRL = %.2f\n", Vrl);
if(times_mq < 6000) // 获取系统执行时间,3s前进行校准
{
MQ7_PPM_Calibration(RS);
}
float ppm = 98.322f * pow(RS/R0, -1.458f);
return ppm;
}
|