首页 STM32F103——气体质量
文章
取消

STM32F103——气体质量

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "adc.h"

/*******************************************************************************
*函数的原型:void ADC_Pin_Init(void)
*函数的功能:GPIO初始化
*函数的参数:None
*函数返回值:None
*函数的说明:PA0 ADC1_IN0 模拟输入
*函数编写者:庆
*函数编写日期:2021/2/28
*函数的版本号:V1.0
********************************************************************************/
void ADC_Pin_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	ADC_InitTypeDef ADC_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
						| RCC_APB2Periph_ADC1,ENABLE);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;//单次转换
	ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;//数据对齐
	ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;//独立模式
	ADC_InitStruct.ADC_NbrOfChannel = 1;//转换总数
	ADC_InitStruct.ADC_ScanConvMode = DISABLE;//单通道扫描
	ADC_Init(ADC1,&ADC_InitStruct);
	
	//转换通道 第几次转换 采样时间
	ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);//
	
	ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);
	
	ADC_Cmd(ADC1,ENABLE);
}

/*******************************************************************************
*函数的原型:u16 ADC_Trans(void)
*函数的功能:ADC读取数据
*函数的参数:None
*函数返回值:
	@ u16:采样50次的平均值
*函数的说明:
*函数编写者:庆
*函数编写日期:2021/2/28
*函数的版本号:V1.0
********************************************************************************/
u16 ADC_Trans(void)
{
	u16 adc_value = 0;
	u8 i = 0;
	
	for(i = 0; i < 50; i++)
	{ 
		//开始转换
		ADC_SoftwareStartConvCmd(ADC1,ENABLE);
		
		//转换是否结束
		while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) != SET);
		adc_value = adc_value + ADC_GetConversionValue(ADC1);//读ADC中的值
	}
	
	return adc_value / 50;
}

#ifndef _ADC_H_
#define _ADC_H_

#include "stm32f10x.h"

void ADC_Pin_Init(void);
u16 ADC_Trans(void);

#endif

 

本文由作者按照 CC BY 4.0 进行授权