# IR - nec

Source: [https://docs.qualcomm.com/doc/80-58740-4/topic/ir-nec.html](https://docs.qualcomm.com/doc/80-58740-4/topic/ir-nec.html)

This demo mainly introduces IR to send and receive data using the nec protocol.

## Hardware connection

The GPIO used in this demo refers to `board_ir_gpio_init`. Connect the
                infrared transmitting diode and receiving head to the IR pin. The specific
                connection method is as follows (taking QCC74x\_undef as an example):

| IR signal | External module |
| --- | --- |
| VCC | Infrared receiver VCC |
| GND | Infrared receiver GND |
| Rx | Infrared receiver OUT |
| VCC | Infrared emitting diode anode |
| Tx | Infrared emitting diode cathode |

## Software implementation

For more detailed code, please refer to
                    examples/peripherals/ir/ir\_nec.

    board_init();Copy to clipboard

- `board_init` turns on the IR clock and select the IR clock
                        source and frequency division

    board_ir_gpio_init();Copy to clipboard

- Configure related pins as `IR` function

    uint32_t tx_buffer[1] = { 0xE916FF00 };
    struct qcc74x_ir_tx_config_s tx_cfg;
    
    irtx = qcc74x_device_get_by_name("irtx");
    
    /* TX init */
    tx_cfg.tx_mode = IR_TX_NEC;
    qcc74x_ir_tx_init(irtx, &tx_cfg);Copy to clipboard

- Get `irtx` handle
- Set tx\_mode to NEC mode and call `qcc74x_ir_tx_init(irtx,
                            &tx_cfg)` to initialize IR Tx

    uint64_t rx_data;
    uint8_t rx_len;
    struct qcc74x_ir_rx_config_s rx_cfg;
    
    irrx = qcc74x_device_get_by_name("irrx");
    
    /* RX init */
    rx_cfg.rx_mode = IR_RX_NEC;
    rx_cfg.input_inverse = true;
    rx_cfg.deglitch_enable = false;
    qcc74x_ir_rx_init(irrx, &rx_cfg);
    
    /* Enable rx, wait for sending */
    qcc74x_ir_rx_enable(irrx, true);Copy to clipboard

- Get `irrx` handle
- Set rx\_mode to NEC mode and call `qcc74x_ir_rx_init(irrx,
                            &rx_cfg)` to initialize IR Rx
- Call `qcc74x_ir_rx_enable(irrx, true)` to enable IR Rx and
                        wait for data to be sent

    qcc74x_ir_send(irtx, tx_buffer, 1);
    rx_len = qcc74x_ir_receive(irrx, &rx_data);Copy to clipboard

- Call `qcc74x_ir_send(irtx, tx_buffer, 1)` to send the data in
                        tx\_buffer
- Call `qcc74x_ir_receive(irrx, &rx_data)` to store the
                        received data in rx\_data

    /* Check data received */
    if (rx_data != tx_buffer[0]) {
        printf("Data error! receive bit: %d, value: 0x%016lx\r\n", rx_len, rx_data);
    } else {
        printf("Received correctly. receive bit: %d, value: 0x%016lx\r\n", rx_len, rx_data);
    }Copy to clipboard

- Check whether the sent and received data are consistent

## Experimental phenomena

Press the RST button on the development board and the serial port prints the received
                data.

**Parent Topic:** [IR](https://docs.qualcomm.com/doc/80-58740-4/topic/ir.html)

Last Published: Feb 10, 2025

[Previous Topic
IR](https://docs.qualcomm.com/bundle/publicresource/80-58740-4/topics/ir.md) [Next Topic
IR - rc5](https://docs.qualcomm.com/bundle/publicresource/80-58740-4/topics/ir-rc5.md)