最近闲着写了一个arduino上的红外遥控解析代码库,
该库占用资源少,仅使用一个支持外部中断的引脚即可, 无需专门的定时器, 支持重复码。
该库代码清晰易懂, 调试方便,在stm32f103c8t6, esp32, arduino uno上测试通过。
测试用例
main.cpp
//红外遥控esp32代码
#include <Arduino.h>
#include "cxg_IRremote.h"
//红外遥控, 需要支持中断的引脚
#define IRremote_PIN 2
uint16_t receiveData = 0;
CxgIRremote cxgIRremote;
void setup() {
Serial.begin(115200);
cxgIRremote.attach(IRremote_PIN);
//arduino uno使用中断0或1
//ESP32,stm32等直接使用IRremote_PIN引脚
attachInterrupt(
0, []() {
cxgIRremote.handleRisingInterrupt();
},
RISING);
}
void loop() {
uint16_t data = cxgIRremote.read();
if(data > 0) {
receiveData = data;
Serial.print("receive: ");
Serial.println(data);
}
}