IR RX Data in Interrupt Mode

This sample code guide is designed to help users easily and comprehensively understand IR sample. This sample demonstrates how IR receives data by interrupt mode.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P0_2 to the transmitting end of the IR transceiver module.

Configurations

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

    • #define IR_RECV_PIN                     P0_2

  2. The following macros can be configured to modify the RX threshold.

    • #define IR_RX_FIFO_THR_LEVEL            30

  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.

    ir_receive_demo();
    

Building and Downloading

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

Experimental Verification

Send IR data to the IR transceiver module by IR remote control. The received data is stored in array IR_DataStruct.irBuf.

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\ir\receive\ir_receive_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

IR initialization flow is shown in the following figure.

../../../_images/IR_Initialization_Flow_Chart_2.png

IR RX Mode Initialization Flow Chart

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

    static void board_ir_init(void)
    {
       Pad_Config(IR_RECV_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_LOW);
       Pinmux_Config(IR_RECV_PIN, IRDA_RX);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the IR clock and function.

  3. Initialize the IR peripheral:

    1. Define the IR_InitTypeDef type IR_InitStruct, and call IR_StructInit() to pre-fill IR_InitStruct with default values.

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

    3. Call IR_Init() to initialize the IR peripheral.

    IR Initialization Parameters

    IR Hardware Parameters

    Setting in the IR_InitStruct

    IR

    IR Clock Frequency

    IR_InitTypeDef::IR_Freq

    38

    IR Duty Cycle

    IR_InitTypeDef::IR_DutyCycle

    2

    IR Mode

    IR_InitTypeDef::IR_Mode

    IR_MODE_RX

    IR Rx Mode

    IR_InitTypeDef::IR_RxStartMode

    IR_RX_AUTO_MODE

    IR RX Threshold

    IR_InitTypeDef::IR_RxFIFOThrLevel

    IR_RX_FIFO_THR_LEVEL

    Data Discard Mode

    IR_InitTypeDef::IR_RxFIFOFullCtrl

    IR_RX_FIFO_FULL_DISCARD_NEWEST

    IR Rx Trigger Mode

    IR_InitTypeDef::IR_RxTriggerMode

    IR_RX_RISING_EDGE

    IR RX Filter Time

    IR_InitTypeDef::IR_RxFilterTime

    IR_RX_FILTER_TIME_50ns

    IR Rx Counter Threshold Type

    IR_InitTypeDef::IR_RxCntThrType

    IR_RX_Count_Low_Level

    IR Rx Counter Threshold

    IR_InitTypeDef::IR_RxCntThr

    0x23a

  4. Call IR_INTConfig() to enable the IR receive FIFO data count greater than the set receive threshold interrupt IR_INT_RF_LEVEL and the receive level timeout interrupt IR_INT_RX_CNT_THR.

  5. Call IR_MaskINTConfig() to unmask the IR receive FIFO data count greater than the set receive threshold interrupt IR_INT_RF_LEVEL and the receive level timeout interrupt IR_INT_RX_CNT_THR.

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

  7. Call IR_ClearRxFIFO() to clear IR RX FIFO.

  8. Call IR_Cmd() to enable IR peripheral.

Functional Implementation

Interrupt Handle

IR interrupt handle flow is shown in the following figure.

../../../_images/IR_receive_flow_in_interrupt_handler.png

IR Interrupt Handle Flow Chart

  1. When the number of data in the IR receive FIFO reaches the set receive threshold, it triggers the IR_INT_RF_LEVEL interrupt:

    1. Call IR_MaskINTConfig() to mask the IR receive FIFO data count greater than the set receive threshold interrupt IR_INT_RF_LEVEL.

    2. Call IR_GetRxDataLen() to get data size in RX FIFO.

    3. Call IR_ReceiveBuf() to read data from RX FIFO.

    4. Call IR_ClearINTPendingBit() to clear the IR receive FIFO data count greater than the set receive threshold interrupt IR_INT_RF_LEVEL_CLR.

    5. Call IR_MaskINTConfig() to unmask the IR receive FIFO data count greater than the set receive threshold interrupt IR_INT_RF_LEVEL.

  2. When the IR detects a low-level duration exceeding the set carrier cycle of IR_InitTypeDef::IR_RxCntThr, it triggers the IR_INT_RX_CNT_THR interrupt:

    1. Call IR_MaskINTConfig() to mask the receive level timeout interrupt IR_INT_RX_CNT_THR.

    2. Call IR_GetRxDataLen() to get data size in RX FIFO.

    3. Call IR_ReceiveBuf() to read data from RX FIFO.

    4. Call IR_ClearINTPendingBit() to clear the receive level timeout interrupt IR_INT_RX_CNT_THR_CLR.

    5. Call IR_MaskINTConfig() to unmask the receive level timeout interrupt IR_INT_RX_CNT_THR.

IR Data Decode

  1. Wait for receiving the whole IR packets.

    while (rx_count <= NEC_LENGTH - 5) {;};
    
  2. Call IR_NECDecode to decode the data by the NEC protocol.