IR Pulse Duty Cycle Detection
This sample code guide is designed to help users easily and comprehensively understand IR sample. This sample demonstrates pulse detection of high level count, low level count and frequency of a PWM with IR.
Requirements
For hardware requirements, please refer to the Requirements.
Wiring
Connect P2_1 to PWM output.
Configurations
The following macros can be configured to modify pin definitions.
#define IR_RECV_PIN P2_1
The following macros can be configured to modify the RX threshold.
#define IR_RX_FIFO_THR_LEVEL 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.ir_pulse_detection_demo();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
When the pulse detection is completed, print PWM frequency, high level count, and low level count information in Debug Analyzer.
ir_pulse_detection_demo: freq xx, high_cnt xx, low_cnt xx
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\pulse_detection\ir_pulse_detection_demo.c
.
Initialization
The initialization flow for peripherals can refer to Initialization Flow.
IR initialization flow is shown in the following figure.

IR RX Mode Initialization Flow Chart
Call
Pad_Config()
andPinmux_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); }
Call
RCC_PeriphClockCmd()
to enable the IR clock and function.Initialize the IR peripheral:
Define the
IR_InitTypeDef
typeIR_InitStruct
, and callIR_StructInit()
to pre-fillIR_InitStruct
with default values.Modify the
IR_InitStruct
parameters as needed. The IR initialization parameter configuration is shown in the table below.Call
IR_Init()
to initialize the IR peripheral.
IR Initialization Parameters IR Hardware Parameters
Setting in the
IR_InitStruct
IR
IR Clock Frequency
40000
IR Mode
IR Rx Mode
IR RX Threshold
IR_RX_FIFO_THR_LEVEL
Data Discard Mode
IR Rx Trigger Mode
IR RX Filter Time
IR Rx Counter Threshold Type
IR Rx Counter Threshold
2000000
Call
IR_INTConfig()
to enable the IR receive FIFO data count greater than the set receive threshold interruptIR_INT_RF_LEVEL
and the receive level timeout interruptIR_INT_RX_CNT_THR
.Call
IR_MaskINTConfig()
to unmask the IR receive FIFO data count greater than the set receive threshold interruptIR_INT_RF_LEVEL
and the receive level timeout interruptIR_INT_RX_CNT_THR
.Call
NVIC_Init()
to enable NVIC of IR.Call
IR_ClearRxFIFO()
to clear IR RX FIFO.Call
IR_Cmd()
to enable IR peripheral.
Functional Implementation
Interrupt Handle
IR interrupt handle flow is shown in the following figure.

IR Interrupt Handle Flow Chart
When the number of data in the IR receive FIFO reaches the set receive threshold, it triggers the
IR_INT_RF_LEVEL
interrupt:Call
IR_MaskINTConfig()
to mask the IR receive FIFO data count greater than the set receive threshold interruptIR_INT_RF_LEVEL
.Call
IR_GetRxDataLen()
to get data size in RX FIFO.Call
IR_ReceiveBuf()
to read data from RX FIFO.Set
rx_finish_flag
to 1.Call
IR_ClearINTPendingBit()
to clear the IR receive FIFO data count greater than the set receive threshold interruptIR_INT_RF_LEVEL_CLR
.Call
IR_MaskINTConfig()
to unmask the IR receive FIFO data count greater than the set receive threshold interruptIR_INT_RF_LEVEL
.Call
IR_Cmd()
to disable IR peripheral.
when the IR detects a low-level duration exceeding the set carrier cycle of
IR_InitTypeDef::IR_RxCntThr
, it triggers theIR_INT_RX_CNT_THR
interrupt:Call
IR_MaskINTConfig()
to mask the receive level timeout interruptIR_INT_RX_CNT_THR
.Call
IR_GetRxDataLen()
to get data size in RX FIFO.Call
IR_ReceiveBuf()
to read data from RX FIFO.Set
rx_finish_flag
to 1.Call
IR_ClearINTPendingBit()
to clear the receive level timeout interruptIR_INT_RX_CNT_THR_CLR
.Call
IR_MaskINTConfig()
to unmask the receive level timeout interruptIR_INT_RX_CNT_THR
.Call
IR_Cmd()
to disable IR peripheral.
High Level Count and Low Level Count and Frequency Analysis
If
rx_finish_flag
is set 1:If bit 31 of the IR RX data
IR_DataStruct[i]
is set to 1, the high level count is equal to the value of the lower 31 bits of the IR RX dataIR_DataStruct[i]
plus 1.If bit 31 of the IR RX data
IR_DataStruct[i]
is set to 0, the low level count is equal to the value of the IR RX dataIR_DataStruct[i]
plus 1.The total count value
total_cnt
is equal to the high level count plus the low level count, iftotal_cnt
is not equal to 0, frequency = 40000000 /total_cnt
.Set
rx_finish_flag
to 0.Call
IR_ClearRxFIFO()
to clear IR RX FIFO.Call
IR_Cmd()
to enable IR peripheral.