ADC One Shot and Interrupt Mode

This sample code guide is designed to help users easily and comprehensively understand ADC sample. This sample demonstrates how ADC samples data by interrupt mode in one shot mode. This sample uses one shot mode of ADC peripheral to measure voltage on P0_0, P0_1, VBAT, and VADP by interrupt mode.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

  1. Connect P0_0 and P0_1 of EVB to external DC voltage source. Input voltage of P0_0 must range from 0 to 3.3V, and input voltage of P0_1 must range from 0 to 0.9V.

  2. Connect Lithium-ion battery to EVB.

  3. Connect VADP to 5V of EVB.

The hardware connection of ADC sample code is shown in the figure below.

../../../_images/ADC_Sample_Code_Hardware_Connection_Diagram.png

ADC Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify the sampling interval.

    • #define ADC_TIMER_PERIOD  1000

  2. 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_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, ADC samples every 1000ms. Once ADC sampling is finished, the converted voltage values will be printed in Debug Analyzer.

adc_handler: res[0] xxx, res[1] xxx, res[2] xxx, res[3] 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.

../../../_images/Turn_Off_Charger_on_the_McuConfig_Tool.jpg

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_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

The initialization flow of ADC sampling in one shot mode by interrupt mode is shown in the following figure.

../../../_images/ADC_One_Shot_Sampling_by_Interrupt_Mode.png

ADC One Shot Sampling by Interrupt Mode

  1. Call Pad_Config() and Pinmux_Config() to initialize the pin.

    Pad_Config(ADC_0, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_LOW);
    Pad_Config(ADC_1, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_LOW);
    
    Pinmux_Config(ADC_0, IDLE_MODE);
    Pinmux_Config(ADC_1, IDLE_MODE);
    
  2. Call ADC_HighBypassCmd() to configure the specific channel as bypass mode.

  3. Call RCC_PeriphClockCmd() to enable the ADC clock and function.

  4. Initialize the ADC peripheral:

    1. Define the ADC_InitTypeDef type adcInitStruct, and call ADC_StructInit() to pre-fill adcInitStruct with default values.

    2. Modify the adcInitStruct parameters as needed. The ADC initialization parameter configuration is shown in the table below.

    3. Call ADC_Init() to initialize the ADC peripheral.

    ADC Initialization Parameters

    ADC Hardware Parameters

    Setting in the adcInitStruct Variables

    ADC

    Bit Map

    ADC_InitTypeDef::bitmap

    0x0f

    Schedule Index

    ADC_InitTypeDef::schIndex

    Index 0 is set to EXT_SINGLE_ENDED(0).

    Index 1 is set to EXT_SINGLE_ENDED(1).

    Index 2 is set to INTERNAL_VBAT_MODE.

    Index 3 is set to INTERNAL_VADPIN_MODE.

  5. Call ADC_INTConfig() to enable ADC one shot mode done interrupt.

  6. Call NVIC_Init() to enable NVIC of ADC.

  7. Call ADC_Cmd() to enable ADC one shot sampling.

Functional Implementation

Interrupt Handle

When ADC sampling is completed, ADC one shot mode end interrupt is triggered and enters the interrupt handler. ADC interrupt handle flow is shown in the following figure.

../../../_images/ADC_One_Shot_Interrupt_Handle_Flow.png

ADC Interrupt Handle Flow

  1. Call ADC_GetIntFlagStatus() to check ADC_INT_ONE_SHOT_DONE interrupt status flag.

  2. Call ADC_ClearINTPendingBit() to clear ADC_INT_ONE_SHOT_DONE interrupt.

  3. Call ADC_Cmd() to disable ADC.

  4. Call ADC_Read() to read ADC raw data from schedule table.

  5. Call ADC_GetRes() and ADC_GetHighBypassRes() to get conversion result.