LPC Comparator Function

This sample code guide is designed to help users easily and comprehensively understand LPC sample. This sample demonstrates LPC comparator function.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P0_0 of EVB to an external DC voltage source.

Configurations

  1. The following macros can be configured to modify pin definitions.

    • #define LPC_CAPTURE_PIN         ADC_0

  2. The following macros can be configured to modify the LPC comparator value.

    • #define LPC_COMP_VALUE          0x4

  3. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    lpc_comparator_demo();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

Press the Reset button on the EVB, adjust the DC voltage source repeatedly, when the input voltage on P0_0 is detected to be higher than 400mV for 4 times, it triggers the interrupt and prints log in Debug Analyzer.

lpc_rtc_handler: lpc_counter_value 4

Code Overview

This section introduces the code and process description for initialization and corresponding function implementation in the sample.

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\lpc\comparator\lpc_comparator_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

LPC initialization flow is shown in the following figure.

../../../_images/LPC_Comparator_Function_Initialization_Flow_Chart.png

LPC Initialization Flow Chart

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

    static void board_lpc_init(void)
    {
       Pad_Config(LPC_CAPTURE_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE,
                   PAD_OUT_HIGH);
       Pinmux_Config(LPC_CAPTURE_PIN, IDLE_MODE);
    }
    
  2. Initialize the LPC peripheral:

    1. Define the LPC_InitTypeDef type LPC_InitStruct, and call LPC_StructInit() to pre-fill LPC_InitStruct with default values.

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

    3. Call LPC_Init() to initialize the LPC peripheral.

    LPC Initialization Parameters

    LPC Hardware Parameters

    Setting in the LPC_InitStruct

    LPC

    Channel

    LPC_InitTypeDef::LPC_Channel

    LPC_CAPTURE_PIN

    Edge

    LPC_InitTypeDef::LPC_Edge

    LPC_Vin_Over_Vth

    Threshold Voltage

    LPC_InitTypeDef::LPC_Threshold

    LPC_400_mV

  3. Call LPC_CounterReset() to reset the LPC counter.

  4. Call LPC_WriteComparator() to set LPC comparator value.

  5. Call LPC_INTConfig() to enable the LPC comparator interrupt LPC_INT_COUNT_COMP.

  6. For RTL87x3D, call LPC_INTConfig() to enable interrupt signal to CPU NVIC, for RTL87x3E and RTL87x3EP, call RTC_CpuNVICEnable() to enable interrupt signal to CPU NVIC.

  7. For RTL87x3D, call NVIC_Init() to enable NVIC of LPC, for RTL87x3E and RTL87x3EP, call NVIC_Init() to enable NVIC of RTC.

  8. Call LPC_Cmd() to enable the LPC.

  9. Call LPC_CounterCmd() to enable the LPC counter.

Functional Implementation

Interrupt Handle

LPC interrupt handle flow is shown in the figure below.

../../../_images/LPC_Comparator_Function_Interrupt_Handle_Flow_Chart.png

LPC Interrupt Handle Flow Chart

When the input voltage on P0_0 is detected to be higher than 400mV for 4 times, it triggers an interrupt:

  1. Call LPC_GetINTStatus() to check LPC_INT_COUNT_COMP interrupt status flag.

  2. Call LPC_ReadCounter() to read LPC counter value.

  3. Call LPC_WriteComparator() to set the LPC comparator value.

  4. Call LPC_ClearINTPendingBit() to clear the LPC comparator interrupt LPC_INT_COUNT_COMP.