zoukankan      html  css  js  c++  java
  • 基于Arduino的红外遥控

    1、红外接收头介绍 

    一、什么是红外接收头? 

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

    二、工作原理 

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

    三、红外接收头的引脚与连线 

     

    红外接收头有三个引脚如下图: 

    用的时候将VOUT接到模拟口,GND接到实验板上的GND,VCC接到实验板上的+5v。

    红外遥控实验 

    1、实验器件 

    红外遥控器:1个

    红外接收头:1个

    LED灯:6个

    220Ω电阻:6个

    多彩面包线:若干 

     

    2、实验连线 

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

    3、实验原理 

    要想对某一遥控器进行解码必须要了解该遥控器的编码方式。本产品使用的控器的码方式为: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癿高电平组成。

    ·重复脉冲

     

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

    线路连接图:

      1 #include <IRremote.h>
      2 int RECV_PIN = 11;
      3 int LED1 = 2;
      4 int LED2 = 3;
      5 int LED3 = 4;
      6 int LED4 = 5;
      7 int LED5 = 6;
      8 int LED6 = 7;
      9 long on1  = 0x00FFA25D;
     10 long off1 = 0x00FFE01F;
     11 long on2 = 0x00FF629D;
     12 long off2 = 0x00FFA857;
     13 long on3 = 0x00FFE21D;
     14 long off3 = 0x00FF906F;
     15 long on4 = 0x00FF22DD;
     16 long off4 = 0x00FF6897;
     17 long on5 = 0x00FF02FD;
     18 long off5 = 0x00FF9867;
     19 long on6 = 0x00FFC23D;
     20 long off6 = 0x00FFB047;
     21 IRrecv irrecv(RECV_PIN);
     22 decode_results results;
     23 // Dumps out the decode_results structure.
     24 // Call this after IRrecv::decode()
     25 // void * to work around compiler issue
     26 //void dump(void *v) {
     27 //  decode_results *results = (decode_results *)v
     28 void dump(decode_results *results) {
     29   int count = results->rawlen;
     30   if (results->decode_type == UNKNOWN) 
     31     {
     32      Serial.println("Could not decode message");
     33     } 
     34   else 
     35    {
     36     if (results->decode_type == NEC) 
     37       {
     38        Serial.print("Decoded NEC: ");
     39       } 
     40     else if (results->decode_type == SONY) 
     41       {
     42        Serial.print("Decoded SONY: ");
     43       } 
     44     else if (results->decode_type == RC5) 
     45       {
     46        Serial.print("Decoded RC5: ");
     47       } 
     48     else if (results->decode_type == RC6) 
     49       {
     50        Serial.print("Decoded RC6: ");
     51       }
     52      Serial.print(results->value, HEX);
     53      Serial.print(" (");
     54      Serial.print(results->bits, DEC);
     55      Serial.println(" bits)");
     56    }
     57      Serial.print("Raw (");
     58      Serial.print(count, DEC);
     59      Serial.print("): ");
     60 
     61   for (int i = 0; i < count; i++) 
     62      {
     63       if ((i % 2) == 1) {
     64       Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
     65      } 
     66     else  
     67      {
     68       Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
     69      }
     70     Serial.print(" ");
     71      }
     72       Serial.println("");
     73      }
     74 
     75 void setup()
     76  {
     77   pinMode(RECV_PIN, INPUT);   
     78   pinMode(LED1, OUTPUT);
     79   pinMode(LED2, OUTPUT);
     80   pinMode(LED3, OUTPUT);
     81   pinMode(LED4, OUTPUT);
     82   pinMode(LED5, OUTPUT);
     83   pinMode(LED6, OUTPUT);  
     84   pinMode(13, OUTPUT);
     85   Serial.begin(9600);
     86   
     87   irrecv.enableIRIn(); // Start the receiver
     88  }
     89 
     90 int on = 0;
     91 unsigned long last = millis();
     92 
     93 void loop() 
     94 {
     95   if (irrecv.decode(&results)) 
     96    {
     97     // If it's been at least 1/4 second since the last
     98     // IR received, toggle the relay
     99     if (millis() - last > 250) 
    100       {
    101        on = !on;
    102 //       digitalWrite(8, on ? HIGH : LOW);
    103        digitalWrite(13, on ? HIGH : LOW);
    104        dump(&results);
    105       }
    106     if (results.value == on1 )
    107        digitalWrite(LED1, HIGH);
    108     if (results.value == off1 )
    109        digitalWrite(LED1, LOW); 
    110     if (results.value == on2 )
    111        digitalWrite(LED2, HIGH);
    112     if (results.value == off2 )
    113        digitalWrite(LED2, LOW); 
    114     if (results.value == on3 )
    115        digitalWrite(LED3, HIGH);
    116     if (results.value == off3 )
    117        digitalWrite(LED3, LOW);
    118     if (results.value == on4 )
    119        digitalWrite(LED4, HIGH);
    120     if (results.value == off4 )
    121        digitalWrite(LED4, LOW); 
    122     if (results.value == on5 )
    123        digitalWrite(LED5, HIGH);
    124     if (results.value == off5 )
    125        digitalWrite(LED5, LOW); 
    126     if (results.value == on6 )
    127        digitalWrite(LED6, HIGH);
    128     if (results.value == off6 )
    129        digitalWrite(LED6, LOW);        
    130     last = millis();      
    131     irrecv.resume(); // Receive the next value
    132   }
    133 }

     

    五、程序功能 

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

    实验截图: 

    诚者,君子之所守也。
  • 相关阅读:
    Java 单测 回滚
    Java四种线程池的使用
    git gc
    Git 常用命令
    JPA 批量新增
    SpringData JPA 排除 扫描 exclude-filter 不能使用解决
    开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
    深入浅出 RPC
    阿里 Java面试 知识点
    Eclipse Egit 安装
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12180793.html
Copyright © 2011-2022 走看看