AD-STM32

AD-STM32

ADC (Analog-Digital Converter) Analog-Digital Converter A
DC can convert the continuously changing analog voltage on the pin into a digital variable stored in the memory, and build a bridge from analog circuit to digital circuit.
12-bit successive approximation ADC, 1us conversion Time
Input voltage range: 0-3.3V, conversion result range: 0~409518 input channels,
can measure 16 external and 2 internal signal sources
Rule group and injection group Two conversion units Analog watchdog automatically monitors the input voltage range
STM32F103C8T6 ADC resources: ADC1, ADC2, 10 external input channels
insert image description here
insert image description here
insert image description here

insert image description here
The first step is to turn on the RCC clock, including the clock of ADC and GPIO. In addition, the frequency divider of ADCCLK here also needs to be configured
. The second step is to configure GPIO, and configure the GPIO to be used as an analog input mode.
The third step is to configure the multi-channel switch here, and connect the channel on the left to the rule group list on the right.
The fourth step is to configure the ADC converter.
The next step is switch control, call the ADC Cmd function to turn on the ADC.

insert image description here
The 72MHz clock of APB2 can be divided by 2, 4, 6, or 8 and input to ADCCLK. (in RCC.h file)
insert image description here
insert image description here
Switch control in PPT
insert image description here

insert image description here
In the PPT, it is used to control a certain interrupt, whether it can lead to the NVIC
insert image description here
insert image description here
insert image description here
ADC_GetsoftwarestartConvstatus(ADC_TypeDef* ADCx) function is generally useless
insert image description here

insert image description here
insert image description here
Key points
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
The first is whether to start the analog watchdog
The second is to configure the high and low values
​​The third is to configure the channel of the watchdog
insert image description here
insert image description here
insert image description here

#include "stm32f10x.h"                  // Device header

void AD_Init(void)
{
	//第一步,开启RCC时钟,包括ADC和GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//开启ADC时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA的时钟
	//接着不要忘了,还有一个ADCCLK需要配置
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);//分频之后,ADCCLK=72MHz/6=12MHz
	

	//第二步,配置GPIO,把需要用的GPIO配置成模拟输入的模式。
	GPIO_InitTypeDef GPIO_InitStruct;
	//所以AIN模式就是ADC的专属模式
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;//选择AIN,模拟输入,在AIN模式下,GPIO口是无效的
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	//第三步,配置这里的多路开关,把左边的通道接入到右边的规则组列表里。
	//在规则组菜单列表的第一个位置,写入通道0这个通道,采样时间就是55.5个ADCCLK的周期
	ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);
	//如果需要在规则组菜单列表的第二个位置,写入通道3这个通道,采样时间就是55.5个ADCCLK的周期
	//ADC_RegularChannelConfig(ADC1,ADC_Channel_3,2,ADC_SampleTime_55Cycles5);
	
	//第四步,就是配置ADC转换器了。
	ADC_InitTypeDef ADC_InitStruct;
	ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;//连续转换模式;选择单次转换
	ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;//选择右对齐
	ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//外部触发选择,软件触发
	ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;//配置为独立模式
	ADC_InitStruct.ADC_NbrOfChannel = 1;//在指定扫描模式下,会使用几个通道;选择1个通道
	ADC_InitStruct.ADC_ScanConvMode = DISABLE;//扫描模式,选择非扫描的模式
	ADC_Init(ADC1,&ADC_InitStruct);
	
	//开启ADC
	ADC_Cmd(ADC1,ENABLE);
	//对ADC进行校准
	ADC_ResetCalibration(ADC1);//开始复位校准
	//等待复位校准完成
	while(ADC_GetResetCalibrationStatus(ADC1) == SET);//返回校准的状态
	ADC_StartCalibration(ADC1);//启动校准
	//等待校准完成
	while(ADC_GetCalibrationStatus(ADC1) == SET);//返回校准的状态
	
}

uint16_t AD_GetValue(void)
{
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);//软件触发
	//等待转换完成
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);//规则组转换完成标志位
	return ADC_GetConversionValue(ADC1);//获取转换值
}

#ifndef _AD_H
#define _AD_H

void AD_Init(void);
uint16_t AD_GetValue(void);
#endif

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "KEY.h"
#include "AD.h"

uint16_t ADValue;
float Voltage;
int main(void)
{
	AD_Init();

	
	while(1)
	{
			ADValue = AD_GetValue();//获取AD值
		  Voltage = (float)ADValue/4095*3.3;//获取电压值
	}
}

Guess you like

Origin blog.csdn.net/qq_45159887/article/details/130674689