室内环境检测系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.3 KiB

/*
* @Author: your name
* @Date: 2022-04-23 14:47:47
* @LastEditTime: 2022-04-26 21:25:14
* @LastEditors: your name
* @Description: koroFileHeader查看配置 : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \USERe:\\\20220423-IndoorEnvSystem\stm32\HARDWARE\ADC\adc.c
*/
#include "ADC.h"
3 years ago
u32 ADC_SUM_BUF = 0;
3 years ago
u16 ADC_AVG = 0;
void ADC_Init_MY(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_ADC1,ENABLE);
3 years ago
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
3 years ago
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
3 years ago
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//adc模式独立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//连续模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//配置规则组触发模式软件出发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//数据对其方式16位寄存器12位数据左对齐右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1;//规则通道序列长度
ADC_InjectedChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);
ADC_InjectedChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);
ADC_Init(ADC1, &ADC_InitStructure);
3 years ago
ADC_Cmd(ADC1,ENABLE);
//下面四个校准函数
ADC_ResetCalibration(ADC1);//重置ADC校准寄存器
while(ADC_GetResetCalibrationStatus(ADC1));//等待重置结束
ADC_StartCalibration(ADC1);//开始校准ADC
while(ADC_GetCalibrationStatus(ADC1));//等待校准结束
}
u16 ADC_Get_Data(void)//获取adc某一通道一次转换结果
{
ADC_ExternalTrigInjectedConvConfig(ADC1,ADC_ExternalTrigInjecConv_None);//配置注入组触发模式
ADC_InjectedSequencerLengthConfig(ADC1, 1);//注入组长度
ADC_InjectedChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);//配置规则通道
ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);//开启软件转换
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_JEOC)); // 清除转换结束标志位等待转换结束
return(ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1));//得到转换结果
}
u16 ADC_Get_AVG_Data(void)
3 years ago
{
u16 i = 0;
for(i = 0;i < 500;i++)
{
ADC_SUM_BUF = ADC_SUM_BUF + ADC_Get_Data();
}
ADC_AVG = ADC_SUM_BUF /500;
ADC_SUM_BUF = 0;
return(ADC_AVG);
}
u16 ADC_Get_Data1(void)//获取adc某一通道一次转换结果
{
ADC_ExternalTrigInjectedConvConfig(ADC1,ADC_ExternalTrigInjecConv_None);//配置注入组触发模式
ADC_InjectedSequencerLengthConfig(ADC1, 1);//注入组长度
ADC_InjectedChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);//配置规则通道
ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);//开启软件转换
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_JEOC)); // 清除转换结束标志位等待转换结束
return(ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1));//得到转换结果
}