这部分出错了,不知道怎么改,还请各位码友解惑下,感谢!
AD.c文件
#include "stm32f10x.h" // Device header
#include "math.h"
#include "Delay.h"
#define CAL_PPM 10;
#define RL 10;
float RS;
static float R0;
void AD_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
GPIO_InitTypeDef GPIO_Initstructure;
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_AIN ;
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Initstructure);
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//单次转换
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//非扫描
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);//给ADC上电
//进行校准
ADC_ResetCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1) == SET);
ADC_StartCalibration(ADC1);
ADC_GetCalibrationStatus(ADC1);
while (ADC_GetCalibrationStatus(ADC1) == SET);
}
uint16_t AD_GetValue(uint8_t ADC_Channel)
{
ADC_RegularChannelConfig(ADC1, ADC_Channel, 1, ADC_SampleTime_239Cycles5);//输入通道
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//ADC转换启动
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);//等待转换完成
return ADC_GetConversionValue(ADC1);//读取结果
}
// 传感器校准函数,根据当前环境PPM值与测得的RS电压值,反推出R0值
void MQ7_PPM_Calibration(float RS)
{
float Vrl = 3.3f * AD_GetValue / 4095.f;
float RS = (3.3f - Vrl) / Vrl * RL;
R0 = RS / pow(CAL_PPM / 98.322, 1 / -1.458f);
}
// 获取传感器的值
float MQ7_GetPPM(void)
{
float Vrl = 3.3f * AD_GetValue / 4095.f;
float RS = (3.3f - Vrl) / Vrl * RL;
float ppm = 98.322f * pow(RS/R0, -1.458f);
return ppm;
}
AD.h文件
#ifndef __AD_H
#define __AD_H
void AD_Init(void);
uint16_t AD_GetValue(uint8_t ADC_Channel);
void MQ7_PPM_Calibration(float RS);
float MQ7_GetPPM(void);
#endif