STM32 Of ADC yes :12 position ADC It is a successive approximation analog-to-digital converter . It has 18 Channels , Measurable 量 16 External and external 2 Internal signal sources . For each channel A/D
The conversion can be done once , continuity , Scanning or intermittent mode 行.ADC The results of can be left aligned or right aligned in the 16 Bit data register .
The analog watchdog feature allows the application to detect if the input voltage exceeds a user-defined high value / Low threshold .【 For details, please refer to the Chinese version of the data manual 16 chapter 】
Using atomic elite development board to realize ADC1 Access to 1 Single conversion , Measure the external voltage .

void Adc_Init(void) { // The first step : open PA Clock ( belong to GPIOA Namely APB2 My clock ) and ADC1 My clock , set up APB1 Input mode
GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Pin =
GPIO_Pin_1; GPIO_Init(GPIOA,&GPIO_InitStructure); // Step two : reset iADC1, And set ADC1 The frequency division factor of the system
RCC_ADCCLKConfig(RCC_PCLK2_Div6); ADC_DeInit(ADC1);
// The third step : initialization ADC1 Related parameters of , set up ADC1 The working mode and rule sequence related information of .
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // Working in the first mock exam mode instead of the cycle mode.
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;// Align right
ADC_InitStructure.ADC_ExternalTrigConv =
ADC_ExternalTrigConv_None;// Conversion is software conversion, not hardware conversion ADC_InitStructure.ADC_Mode =
ADC_Mode_Independent; // Independent mode ADC_InitStructure.ADC_NbrOfChannel = 1;
// Number of channels for rule transformation in sequence ADC_InitStructure.ADC_ScanConvMode = DISABLE;
// Scan mode off , Scan mode is y Used to scan a set of analog channels , We use a passageway here , So the first mock exam is the single mode. ok, Scan mode not available .
ADC_Init(ADC1,&ADC_InitStructure);//ADC1 initialization // Step four : Enable ADC1 And calibration
ADC_Cmd(ADC1,ENABLE); ADC_GetResetCalibrationStatus(ADC1);// Calibration reset ADC1
while(ADC_GetResetCalibrationStatus(ADC1)); // testing ADC1 Is calibration reset complete
ADC_StartCalibration(ADC1);//, Footnotes reset complete , Start calibration
while(ADC_GetCalibrationStatus(ADC1));// Check whether the calibration is completed } u16 Get_Adc(u8 ch) {
// Step five : Configure rule channel parameters , Setting assignments ADC Regular channels for , A sequence , Sampling time
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);//ADC1,ADC passageway 1, Sampling time HSI239.5 Cycles
// Step six : Turn on software conversion ADC_SoftwareStartConvCmd(ADC1,ENABLE);// Enable specified ADC Software conversion function of , What we need is ADC1
while(ADC_GetSoftwareStartConvStatus(ADC1));// Check whether the software conversion is completed return
ADC_GetDualModeConversionValue();// Returns the last read AD value . Returns the result of the last rule group conversion . }
// in order to AD The conversion value of is more accurate , We use the method of multiple collection and average u16 Get_Adc_Average(u8 ch,u8 times) { u32
temp_val=0; u8 t; for(t=0;t<times;t++) { temp_val+=Get_Adc(ch); delay_ms(5); }
return temp_val/times; }

Technology