T2-U development board realizes infrared remote control receiving and sending


Infrared remote control is a control method that uses near-infrared light for data transmission. It is widely used in industrial control, home appliances, lighting and other fields. Many products still retain the infrared control method after adding intelligent control functions.

This time we use the Tuya T2-U development board to develop smart products, adding infrared receiving and sending functions. Not only can the device be controlled by APP, voice, etc., but also can be controlled by infrared remote control.

1. Infrared overview

The wavelength of near-infrared light is 0.76um~1.5um, and the wavelength of infrared remote control transceiver devices is generally 0.8um~0.94um. It has the characteristics of high transmission efficiency, low cost, simple circuit implementation, and strong anti-interference.

Infrared remote control generally consists of two parts: transmitting and receiving. The transmitting element is an infrared transmitting tube. The receiving generally uses an integrated infrared receiving head, but the transmitting carrier frequency must be consistent with the fixed frequency of the receiving head to receive correctly.
insert image description here

2. Launch

1. Modulation

Infrared remote control uses modulation to transmit data, and the data is modulated to a fixed carrier for transmission. The modulation and transmission have stronger anti-interference ability and longer transmission distance.
The first thing to be solved in infrared transmission is the modulation problem. Currently, the mainstream modulation methods include PPM and PWM.

  • PPM: pulse position modulation, the modulation pulse width is constant, and the pulse interval is used to distinguish 0 and 1. As shown in the figure below, the pulse width remains unchanged and is 560us, and the pulse interval changes. The total time of logic 1 is 2.25ms, and the length of time in logic 0 is 1.12ms.

insert image description here
Figure 1: PPM modulation

  • PWM: pulse width modulation, the pulse interval remains unchanged, and the modulation pulse width changes. As shown in the figure below, the pulse interval is 600us, and the pulse width is different. The high level time of logic 1 is 1.2ms, and the high level time of logic 0 is 0.6ms.

insert image description here
Figure 2: PWM modulation

The modulation carrier frequency is generally between 30KHz and 60KHz. Commonly used carriers are 33K, 36K, 36.6K, 38K, 40K, 56K, etc., of which 38K is the most used.
Commonly used duty ratios are 1/3, 1/2, and 1/3 is the most.

2. Infrared transmission protocol

Commonly used infrared transmission protocols include ITT protocol, NEC protocol, Nokia NRC protocol, Sharp protocol, Philips RC-5, RC-6 RECS-80 protocol, Sony SIRC protocol, etc. The most common one is NEC protocol.
Common NEC protocol analysis:

  • Carrier 38KHz, logic 1 is 2.25ms, pulse time 560us; logic 0 is 1.12ms, pulse time 560us

insert image description here
Figure 3: NEC logic '0' vs logic '1'

  • Protocol Format
    insert image description here
    Figure 4: NEC Infrared Carrier Transmission Protocol

(1) First send a 9ms carrier pulse
(2) Then send a 4.5ms low level
(3) Next is the 8bit address code (lower bit first)
(4) Then the 8bit address inverse code for checking the address Whether the code is wrong
(5) The next is the 8-bit command code (low order first)
(6) Then the 8-bit command inverse code is used to check whether the command code is wrong.

  • Repeat code

insert image description here
Figure 5: NEC Repeatedly Send Carrier Protocol

If you keep pressing a key, the repetition code will be sent at a cycle of 110ms, and the repetition code is composed of 9ms carrier, 2.25ms low level and 560us carrier.
insert image description here
Figure 6: NEC repetition code

3. Coding

Although different protocols have different definitions for their respective protocol formats, in general there is still a string of data consisting of high and low levels.
For infrared transmission, it is to output a fixed frequency carrier at the infrared output port during the high level time specified in the agreement; when the low level is low, it directly outputs low. When the infrared receiving head receives the carrier, it outputs a high level, and when there is no carrier, it outputs a low level to complete data decoding.

insert image description here
Figure 7: Protocol after NEC decoding

3. Receive

Infrared receiving often adopts an integrated infrared receiving head, which integrates infrared receiving, amplification, filtering, comparator output and other functions, and outputs a TTL signal that can be recognized by the MCU. Commonly used integrated infrared receivers include SCR638, HS0038, VS1838, etc.
insert image description here
SCR638
insert image description here
HS0038

Figure 8: Integrated receiving head

Infrared receiving application circuit diagram:
insert image description here
Figure 9: Typical application of infrared receiving

4. T2-U development board hardware connection

This time, the T2-U2 development board is used to support the infrared remote control function board to realize the infrared receiving and sending functions of the device.
Infrared remote control function board introduction:
https://developer.tuya.com/cn/docs/iot/tuya-sandwich-infrared-remote-remote-control-board?id=K97o1wfxi7v9l
insert image description here
Infrared remote control function board Connect
insert image description here
the remote control receiving studypin to T2 development Board GPIO8, connect to 3.3V power supply.
If the infrared emission function is required, connect the CRTL pin to GPIO7 of the T2 development board, and connect to the 5V power supply at the same time.

5. TuyaOS infrared function introduction

TuyaOS infrared receiver currently supports NEC code, infrared learning time code (can be used for universal infrared remote control), infrared sending function.

TuyaOS infrared receiver adopts interrupt + hardware timer mode, and can use all GPIOs that support external interrupts, without GPIO pin restrictions, which brings great convenience to product design.

Infrared receiving function

  1. Support query mode and interrupt mode, which can be applied in different scenarios; for example, the query mode is currently used for the learning status of the universal infrared remote control, and the interrupt mode is mostly used for device infrared control.

  2. Support NEC decoding, support 高位之前/低位在前, 引导码最大误差率, 高电平最大误差率, 低电平最大误差率, 重复码最大误差率setting

  3. It supports press and release notifications. In the interrupt mode, it can be s_frame_finishobtained .
    is_frame_finishIt is 1, indicating that the frame of data has been received and is in the released state, and 0, indicating that the frame of data may not be finished yet and is still being received.

  4. Supports long press and short press notifications. In the interrupt mode, the repeat_cnt of the recv_data interrupt can be used in the callback function to determine the number of repeated transmissions of the current remote control.
    In interrupt mode, the callback function is tdl_ir_config(ir_handle, IR_CMD_RECV_CB_REGISTER, app_ir_recv_cb); registered .

tuyaos_ir_driver_demo_quickstartThe project has been provided on the T2 development board to quickly realize infrared receiving and sending. Infrared receiver currently supports NEC code, infrared learning time code (can be used for universal infrared remote control). tuyaos_ir_driver_demo_quickstartIt can be downloaded first through wind_ideor githubfrom the warehouse.

6. Infrared function code usage process

The initial process is: hardware registration->find device->open device->operate device (infrared sending, introduction)

1. Hardware Registration

Select the drive mode, the following three drive modes are supported:

  • Single hardware timer driver

  • Dual hardware timer drivers

  • PWM capture driver

The capture mode driver may not be supported by all hardware. Except for the single hardware timer drive mode, the other two drive modes support self-sending and self-receiving. The timer selected here must be a micro-level (us) timer. The send pin should support 38KHz PWM output.

The sample code for hardware registration is as follows:

char dev_name[] = {
    
    "IR_DEVICE"};

    IR_DRV_CFG_T ir_hw_cfg = {
    
    
        .send_pin = GPIO_NUM_7,
        .recv_pin = GPIO_NUM_8,
        .send_timer = TIMER_NUM_0,
        .recv_timer = TIMER_NUM_0,
        .send_duty = 50,
    };

    TUYA_CALL_ERR_RETURN(tdd_ir_driver_register(IR_DEV_NAME, IR_DRV_SINGLE_TIMER, ir_hw_cfg)); 

2. Device search

According to the device name, get the device operation handle. The sample code is as follows:

IR_HANDLE_T ir_handle;

op_ret = tdl_ir_dev_find(dev_name, &ir_handle);
if (OPRT_OK != op_ret) {
    
    
    PR_NOTICE("tdl_ir_dev_find error:%d", op_ret);
    goto __EXIT;
}

3. Turn on the device

Here you can configure and open the device through different protocols. Currently, the following two methods are supported:

  • timecode type
  • NEC protocol type

Configure infrared device mode, support the following three mode configurations:

  • send only
  • receive only
  • Send and receive simultaneous support

The time code type (timecode) is to send and receive the infrared code value according to the high and low level time when sending. The sample code is as follows:

ir_cfg.ir_mode = IR_MODE_SEND_RECV;  // 红外设备模式
ir_cfg.recv_queue_num = 3;    // 最多接收缓存红外数据条数
ir_cfg.recv_buf_size = 1024; // 单条红外码值最大支持空间大小
ir_cfg.recv_timeout = 1000; // 接收超时时间

ir_cfg.prot_opt = IR_PROT_TIMECODE;  // 时间码协议类型

op_ret = tdl_ir_dev_open(ir_handle, &ir_cfg);
if (OPRT_OK != op_ret) {
    
    
    PR_ERR("open failed, %d", op_ret);
    goto __EXIT;
}

NEC protocol initialization sample code:

ir_cfg.ir_mode = IR_MODE_SEND_RECV;
ir_cfg.recv_queue_num = 3;
ir_cfg.recv_buf_size = 1024;
ir_cfg.recv_timeout = 1000;

ir_cfg.prot_opt = IR_PROT_NEC; // NEC 协议类型

ir_cfg.prot_cfg.nec_cfg.is_nec_msb = 0,
ir_cfg.prot_cfg.nec_cfg.lead_err = 70; // 引导码最大误差率
ir_cfg.prot_cfg.nec_cfg.logics_err = 50; // 逻辑 0/1 高电平最大误差率
ir_cfg.prot_cfg.nec_cfg.logic0_err = 45; // 逻辑 0 低电平最大误差率
ir_cfg.prot_cfg.nec_cfg.logic1_err = 70; // 逻辑 1 低电平最大误差率
ir_cfg.prot_cfg.nec_cfg.repeat_err = 50; // 重复码最大误差率

op_ret = tdl_ir_dev_open(ir_handle, &ir_cfg);
if (OPRT_OK != op_ret) {
    
    
    PR_ERR("open failed, %d", op_ret);
    goto __EXIT;
}

4. Infrared sending

It should be noted here that the infrared transmission adopts the blocking method , because the data used during transmission is the space for sending the incoming data, so the following procedures must be executed after the infrared code value transmission is completed.

The transmission here is related to the protocol of tdl_ir_dev_openthe time device, and ir_cfg.prot_opt = IR_PROT_TIMECODEthe example of time infrared transmission is as follows:

UINT32_T data[] = {
    
    560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 1690, 1690};
ir_send_data.timecode.data = data;
ir_send_data.timecode.len = strlen(data);

op_ret = tdl_ir_dev_send(ir_handle, 38000, ir_send_data, 1);
if (OPRT_OK != op_ret) {
    
    
    PR_ERR("ir send error, %d", op_ret);
}
PR_DEBUG("ir send start");

ir_cfg.prot_opt = IR_PROT_NECAn example of real-time infrared transmission is as follows:

ir_send_data.nec_data.addr = 0x807F;
ir_send_data.nec_data.cmd = 0x1DE2;
ir_send_data.nec_data.repeat_cnt = 1;

op_ret = tdl_ir_dev_send(ir_handle, 38000, ir_send_data, 1);
if (OPRT_OK != op_ret) {
    
    
    PR_ERR("ir send error, %d", op_ret);
}
PR_DEBUG("ir send start");

5. Infrared receiver

Infrared receiving is also blocked by default, but it also supports operations through callbacks. If IR_PROT_TIMECODEthe protocol , the received data will be the decoded data directly. An example of infrared reception is as follows:

op_ret = tdl_ir_dev_recv(ir_handle, &ir_recv_data, 3000);
if (OPRT_OK == op_ret) {
    
    
#if USE_NEC_DEMO
    PR_DEBUG("demo addr:%04x, cmd:%04x, cnt:%d", ir_recv_data->nec_data.addr, ir_recv_data->nec_data.cmd, ir_recv_data->nec_data.repeat_cnt);
#else
    PR_DEBUG("demo addr:%p, len:%d", ir_recv_data, ir_recv_data->timecode.len);
    for (i=0; i<ir_recv_data->timecode.len; i++) {
    
    
        PR_DEBUG_RAW("%d ", ir_recv_data->timecode.data[i]);
    }
    PR_DEBUG_RAW("\r\n");
#endif
    tdl_ir_dev_recv_release(ir_handle, ir_recv_data);
}

6. Receive callback registration

After hardware registration->find device->open the device , you can register the receiving callback. After the infrared receives the data, it will immediately notify the application through the registered callback function, and it can no longer be used to tdl_ir_dev_recv()obtain data.

After receiving the valid data, the callback function will send out two parameters is_frame_finishand recv_data, is_frame_finishif it is 1, it means that this frame of data has been received, and if it is 0, it means that this frame of data may not be finished and is still being received; recv_datait means the received data. The callback function types are as follows:

typedef void (*IR_APP_RECV_CB)(UINT8_T is_frame_finish, IR_DATA_U *recv_data);

The data type here is also configured according to the protocol format you selected during initialization. For example: if you configure the infrared protocol to be in the NEC encoding format during initialization, then the infrared will convert the received infrared code into the NEC format after receiving the data, and then send the data out through the callback function.

An example of use is as follows:

void app_ir_recv_cb(UINT8_T is_frame_finish, IR_DATA_U *recv_data)
{
    
    
    TAL_PR_NOTICE("is_finish: %d, addr: 0x%04x, cmd: 0x%04x, repeat: %d", \
                    is_frame_finish, recv_data->nec_data.addr, recv_data->nec_data.cmd, recv_data->nec_data.repeat_cnt);
}

tdl_ir_config(ir_handle, IR_CMD_RECV_CB_REGISTER, app_ir_recv_cb);

The log is as follows:

[01-01 00:00:29 ty N][app_main.c:43] is_finish: 0, addr: 0x807f, cmd: 0x0cf3, repeat: 0
[01-01 00:00:29 ty N][app_main.c:43] is_finish: 0, addr: 0x807f, cmd: 0x0cf3, repeat: 1
[01-01 00:00:29 ty N][app_main.c:43] is_finish: 0, addr: 0x807f, cmd: 0x0cf3, repeat: 2
[01-01 00:00:29 ty N][app_main.c:43] is_finish: 0, addr: 0x807f, cmd: 0x0cf3, repeat: 3
[01-01 00:00:30 ty N][app_main.c:43] is_finish: 1, addr: 0x807f, cmd: 0x0cf3, repeat: 3
[01-01 00:00:31 ty N][app_main.c:43] is_finish: 0, addr: 0x807f, cmd: 0x0cf3, repeat: 0
[01-01 00:00:31 ty N][app_main.c:43] is_finish: 0, addr: 0x807f, cmd: 0x0cf3, repeat: 1
[01-01 00:00:31 ty N][app_main.c:43] is_finish: 1, addr: 0x807f, cmd: 0x0cf3, repeat: 1
[01-01 00:00:33 ty N][app_main.c:43] is_finish: 0, addr: 0x807f, cmd: 0x0cf3, repeat: 0
[01-01 00:00:33 ty N][app_main.c:43] is_finish: 1, addr: 0x807f, cmd: 0x0cf3, repeat: 0

In the application, you can repeat_cntuse judge whether the infrared button is pressed, long pressed or released.

Guess you like

Origin blog.csdn.net/sandwich_iot/article/details/131129638