欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Ardino基础教程 20_红外遥控

发布时间:2025/4/5 编程问答 62 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Ardino基础教程 20_红外遥控 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

实验二十:红外遥控

库文件下载链接:ArduinoCode20-红外遥控库代码.zip
注意:把 IRremote 文件夹放到 编译器安装目录下的\Arduino\libraries里。不然编译不过。
例如我的:C:\Program Files\Arduino\libraries

一、什么是红外接收头?
红外遥控器发出的信号是一连串的二进制脉冲码。为了使其在无线传输过程中免受其他红外信号的干扰,通常都是先将其调制在特定的载波频率上,然后再经红外发射二极管发射出去,而红外线接收装置则要滤除其他杂波,叧接收该特定频率的信号并将其还原成二进制脉冲码,也就是解调。

二、工作原理
内置接收管将红外发射管发射出来癿光信号转换为微弱的电信号,此信号经由IC内部放大器进行放大,然后通过自动增益控制、带通滤波、解调变、波形整形后还原为遥控器发射出的原始编码,经由接收头的信号输出脚输入到电器上的编码识别电路。

三、红外接收头的引脚与连线
红外接收头有三个引脚如下图:
用的时候将VOUT接到模拟口,GND接到实验板上的GND,VCC接到实验板上的+5v。

实验器材清单

  • 红外遥控器 1个
  • 红外接收头 1个
  • M5直插LED灯 6个
  • 220欧姆直插电阻 6个
  • 面包板 1个
  • 面包板跳线 1扎
  • 实验接线

    首先将板子连接好;接着将红外接收头按照上述方法接好,将VOUT接到数字11口引脚,将LED灯通过电阻接到数字引脚2,3,4,5,6,7。返样就完成了电路部分的连接。

    实验原理

    要想对某一遥控器进行解码必须要了解该遥控器的编码方式。本产品使用的控器的码方式为:NEC协议。下面就介绍一下NEC协议:
    NEC协议介绍:特点:
    (1)8位地址位,8位命令位
    (2)为了可靠性地址位和命令位被传输两次
    (3)脉冲位置调制
    (4)载波频率38khz
    (5)每一位癿时间为1.125ms戒2.25ms
    逻辑 0和1的定义如下图
    协议如下:

    按键按下立刻松开的发射脉冲:

    上面图片显示了NEC的协议典型的脉冲序列。注意:这首先发送LSB(最低位)的协议。在上面癿脉冲传输的地址为0x59命令为0x16。一个消息是由一个9ms的高电平开始,随后有一个4.5ms的低电平,(返两段电平组成引寻码)然后由地址码和命令码。地址和命令传输两次。第二次所有位都取反,可用于对所收到的消息中的确认使用。总传输时间是恒定的,因为每一点与它取反长度重复。如果你不感兴趣,你可以忽略这个可靠性取反,也可以扩大地址和命令,以每16位!

    按键按下一段时间才松开的发射脉冲:

    一个命令发送一次,即使在遥控器上的按键仍然按下。当按键一直按下时,第一个110ms癿脉冲与上图一样,之后每110ms重复代码传输一次。返个重复代码是由一个9ms的高电平脉冲和一个2.25ms低电平和560μs癿高电平组成。

    重复脉冲

    注意:脉冲波形进入一体化接收头以后,因为一体化接收头里要迕解码、信号放大和整形,故要注意:在没有红外信号时,其输出端为高电平,有信号时为低电平,故其输出信号电平正好和发射端相反。接收端脉冲大家可以通过示波器看到,结合看到的波形理解程序。

    实验接线图


    参考程序代码:
    #include <IRremote.h>
    int RECV_PIN = 11;
    int LED1 = 2;
    int LED2 = 3;
    int LED3 = 4;
    int LED4 = 5;
    int LED5 = 6;
    int LED6 = 7;
    long on1 = 0x00FFA25D;
    long off1 = 0x00FFE01F;
    long on2 = 0x00FF629D;
    long off2 = 0x00FFA857;
    long on3 = 0x00FFE21D;
    long off3 = 0x00FF906F;
    long on4 = 0x00FF22DD;
    long off4 = 0x00FF6897;
    long on5 = 0x00FF02FD;
    long off5 = 0x00FF9867;
    long on6 = 0x00FFC23D;
    long off6 = 0x00FFB047;
    IRrecv irrecv(RECV_PIN);
    decode_results results;
    // Dumps out the decode_results structure.
    // Call this after IRrecv::decode()
    // void * to work around compiler issue
    //void dump(void *v) {
    // decode_results *results = (decode_results *)v
    void dump(decode_results *results) {
    int count = results->rawlen;
    if (results->decode_type == UNKNOWN)
    {
    Serial.println(“Could not decode message”);
    }
    else
    {
    if (results->decode_type == NEC)
    {
    Serial.print("Decoded NEC: ");
    }
    else if (results->decode_type == SONY)
    {
    Serial.print(“Decoded SONY: “);
    }
    else if (results->decode_type == RC5)
    {
    Serial.print(“Decoded RC5: “);
    }
    else if (results->decode_type == RC6)
    {
    Serial.print(“Decoded RC6: “);
    }
    Serial.print(results->value, HEX);
    Serial.print(” (”);
    Serial.print(results->bits, DEC);
    Serial.println(” bits)”);
    }
    Serial.print(“Raw (”);
    Serial.print(count, DEC);
    Serial.print(”): “);
    for (int i = 0; i < count; i++)
    {
    if ((i % 2) == 1) {
    Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    }
    else
    {
    Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
    }
    Serial.print(” “);
    }
    Serial.println(””);
    }
    void setup()
    {
    pinMode(RECV_PIN, INPUT);
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    pinMode(LED3, OUTPUT);
    pinMode(LED4, OUTPUT);
    pinMode(LED5, OUTPUT);
    pinMode(LED6, OUTPUT);
    pinMode(13, OUTPUT);
    Serial.begin(9600);
    irrecv.enableIRIn(); // Start the receiver
    }
    int on = 0;
    unsigned long last = millis();
    void loop()
    {
    if (irrecv.decode(&results))
    {
    // If it’s been at least 1/4 second since the last
    // IR received, toggle the relay
    if (millis() - last > 250)
    {
    on = !on;
    // digitalWrite(8, on ? HIGH : LOW);
    digitalWrite(13, on ? HIGH : LOW);
    dump(&results);
    }
    if (results.value == on1 )
    digitalWrite(LED1, HIGH);
    if (results.value == off1 )
    digitalWrite(LED1, LOW);
    if (results.value == on2 )
    digitalWrite(LED2, HIGH);
    if (results.value == off2 )
    digitalWrite(LED2, LOW);
    if (results.value == on3 )
    digitalWrite(LED3, HIGH);
    if (results.value == off3 )
    digitalWrite(LED3, LOW);
    if (results.value == on4 )
    digitalWrite(LED4, HIGH);
    if (results.value == off4 )
    digitalWrite(LED4, LOW);
    if (results.value == on5 )
    digitalWrite(LED5, HIGH);
    if (results.value == off5 )
    digitalWrite(LED5, LOW);
    if (results.value == on6 )
    digitalWrite(LED6, HIGH);
    if (results.value == off6 )
    digitalWrite(LED6, LOW);
    last = millis();
    irrecv.resume(); // Receive the next value
    }
    }


    程序功能

    对遥控器发射出来的编码脉冲进行解码,根据解码结果执行相应的动作。返样大家就可以用遥控器遥控你的器件了,让它听你的指挥。

    实验截图

    程序代码

    #include <IRremote.h> int RECV_PIN = 11; int LED1 = 2; int LED2 = 3; int LED3 = 4; int LED4 = 5; int LED5 = 6; int LED6 = 7; long on1 = 0x00FFA25D; long off1 = 0x00FFE01F; long on2 = 0x00FF629D; long off2 = 0x00FFA857; long on3 = 0x00FFE21D; long off3 = 0x00FF906F; long on4 = 0x00FF22DD; long off4 = 0x00FF6897; long on5 = 0x00FF02FD; long off5 = 0x00FF9867; long on6 = 0x00FFC23D; long off6 = 0x00FFB047; IRrecv irrecv(RECV_PIN); decode_results results; void dump(decode_results *results) {int count = results->rawlen;if (results->decode_type == UNKNOWN) {Serial.println("Could not decode message");} else {if (results->decode_type == NEC) {Serial.print("Decoded NEC: ");} else if (results->decode_type == SONY) {Serial.print("Decoded SONY: ");} else if (results->decode_type == RC5) {Serial.print("Decoded RC5: ");} else if (results->decode_type == RC6) {Serial.print("Decoded RC6: ");}Serial.print(results->value, HEX);Serial.print(" (");Serial.print(results->bits, DEC);Serial.println(" bits)");}Serial.print("Raw (");Serial.print(count, DEC);Serial.print("): ");for (int i = 0; i < count; i++) {if ((i % 2) == 1) {Serial.print(results->rawbuf[i]*USECPERTICK, DEC);} else {Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);}Serial.print(" ");}Serial.println("");}void setup(){pinMode(RECV_PIN, INPUT); pinMode(LED1, OUTPUT);pinMode(LED2, OUTPUT);pinMode(LED3, OUTPUT);pinMode(LED4, OUTPUT);pinMode(LED5, OUTPUT);pinMode(LED6, OUTPUT); pinMode(13, OUTPUT);Serial.begin(9600);irrecv.enableIRIn(); // Start the receiver}int on = 0; unsigned long last = millis();void loop() {if (irrecv.decode(&results)) {// If it's been at least 1/4 second since the last// IR received, toggle the relayif (millis() - last > 250) {on = !on; // digitalWrite(8, on ? HIGH : LOW);digitalWrite(13, on ? HIGH : LOW);dump(&results);}if (results.value == on1 )digitalWrite(LED1, HIGH);if (results.value == off1 )digitalWrite(LED1, LOW); if (results.value == on2 )digitalWrite(LED2, HIGH);if (results.value == off2 )digitalWrite(LED2, LOW); if (results.value == on3 )digitalWrite(LED3, HIGH);if (results.value == off3 )digitalWrite(LED3, LOW);if (results.value == on4 )digitalWrite(LED4, HIGH);if (results.value == off4 )digitalWrite(LED4, LOW); if (results.value == on5 )digitalWrite(LED5, HIGH);if (results.value == off5 )digitalWrite(LED5, LOW); if (results.value == on6 )digitalWrite(LED6, HIGH);if (results.value == off6 )digitalWrite(LED6, LOW); last = millis(); irrecv.resume(); // Receive the next value} }

    总结

    以上是生活随笔为你收集整理的Ardino基础教程 20_红外遥控的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。