电赛菜鸟营培训(四)——STM32F103CB之ADC转换

时间:2021-11-25 17:31:22

一、ADC概念

实现模拟信号转换成数字信号就是这样子= =

二、代码框架

#include "stm32f10x.h"
void delay(u32 kk)
{
while(kk--);
}
int main()
{
u16 ad=0,adc0=0; GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_InitStructure.ADC_Mode= ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode=DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel=1;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0, 1,ADC_SampleTime_1Cycles5);
ADC_Cmd(ADC1,ENABLE);
ADC_SoftwareStartConvCmd(ADC1,ENABLE); while(1)
{
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)==RESET);
ad=ADC_GetConversionValue(ADC1);
adc0=ad;
} }