ADC One Shot and Polling Mode
This sample code guide is designed to help users easily and comprehensively understand ADC sample. This sample demonstrates how ADC samples data by polling mode in one shot mode. This sample uses one shot mode of ADC peripheral to measure voltage on P0_1, VBAT, and VADP by polling mode.
Requirements
For hardware requirements, please refer to the Requirements.
Wiring
Connect P0_1 of EVB to external DC voltage source and input voltage of P0_1 must range from 0 to 3.3V.
Connect Lithium-ion battery to EVB.
Connect VADP to 5V of EVB.
Configurations
The entry function is as follows, call this function in main()
to run this sample code. For more details, please refer to the Initialization.
adc_polling_demo();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Press the Reset button on the EVB board, after initialization is complete, the ADC begins sampling. Once ADC sampling is finished, the raw data collected and the converted voltage values will be printed in Debug Analyzer.
driver_adc_init: data[0] xxx, data[1] xxx, data[2] xxx
driver_adc_init: res[0] xxx, res[1] xxx, res[2] xxx
Code Overview
This section introduces the code and process description for initialization and corresponding function implementation in the sample.
Note
To use the mode, please turn off Charger auto enable and Battery detection support on the MCUConfig Tool. As shown in Turn Off Charger on the MCUConfig Tool.

Turn Off Charger on the MCUConfig Tool
Source Code Directory
For project directory, please refer to Source Code Directory.
Source code directory:
sdk\src\sample\io_demo\adc\one-shot\adc_polling_demo.c
.
Initialization
The initialization flow for peripherals can refer to Initialization Flow.
The flow of ADC sampling in one shot mode by polling mode is shown in the following figure.

ADC One Shot Sampling by Polling Mode
Call
Pad_Config()
andPinmux_Config()
to initialize the pin.static void board_adc_init(void) { Pad_Config(ADC_1, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_LOW); Pinmux_Config(ADC_1, IDLE_MODE); }
Call
RCC_PeriphClockCmd()
to enable the ADC clock and function.Initialize the ADC peripheral:
Define the
ADC_InitTypeDef
typeadc_init_struct
, and callADC_StructInit()
to pre-filladc_init_struct
with default values.Modify the
adc_init_struct
parameters as needed. The ADC initialization parameter configuration is shown in the table below.Call
ADC_Init()
to initialize the ADC peripheral.
ADC Initialization Parameters ADC Hardware Parameters
Setting in the
adc_init_struct
VariablesADC
Sample Time
Bit Map
0x07
Schedule Index
Index 0 is set to
EXT_SINGLE_ENDED(1)
.Index 1 is set to
INTERNAL_VBAT_MODE
.Index 2 is set to
INTERNAL_VADPIN_MODE
.Call
ADC_INTConfig()
to enable ADC one shot mode done interrupt.Call
NVIC_Init()
to disable NVIC of ADC.Call
ADC_Cmd()
to enable ADC one shot sampling.
Functional Implementation
When ADC sampling is completed, ADC one shot mode end interrupt status flag is set. The handle flow is shown in ADC One Shot Sampling by Polling Mode.
Call
ADC_GetIntFlagStatus()
to checkADC_INT_ONE_SHOT_DONE
interrupt status and wait for the interrupt status flag to be set.Call
ADC_ClearINTPendingBit()
to clearADC_INT_ONE_SHOT_DONE
interrupt.Call
ADC_Cmd()
to disable ADC.Call
ADC_Read()
to read ADC data from schedule table.Call
ADC_GetRes()
to get conversion result.