- #define CAL_PPM 20 // 校准环境中PPM值
- #define RL 5 // RL阻值
- static float R0 = 6.00; // 元件在洁净空气中的阻值
- //得到ADC采样内部传感器的值
- //取10次,然后平均
- vu16 Get_ADCValue_MQ2(void)
- {
- u32 val = 0;
- u8 times = 10;
- u8 count;
- for(count = 0; count < times; count++)
- {
- val += ADC_ConvertedValue[0];//获取DMA通道值
- delay_ms(5);
- }
- return val/times;
- }
-
- // 传感器校准函数
- void MQ2_PPM_Calibration(float RS)
- {
- R0 = RS / pow(CAL_PPM / 613.9f, 1 / -2.074f);
- }
-
- // MQ2传感器数据处理
- float MQ2_GetPPM(void)
- {
- float Vrl = 3.3f * Get_ADCValue_MQ2() / 4096.f;
- Vrl = ( (float)( (int)( (Vrl+0.005)*100 ) ) )/100;
- float RS = (3.3f - Vrl) / Vrl * RL;
- if(times_mq < 6000) // 获取系统执行时间,300ms前进行校准
- {
- R0 = RS / pow(CAL_PPM / 613.9f, 1 / -2.074f);
- }
-
- float ppm = 613.9f * pow(RS/R0, -2.074f);
-
- return ppm;
- }
|