zoukankan      html  css  js  c++  java
  • Digispark红外接收器

    一、红外协议之NEC协议原理

    NEC协议格式:

    clip_image002

    首次发送的是9ms的高电平脉冲,其后是4.5ms的低电平,接下来就是8bit的地址码(从低有效位开始发),而后是8bit的地址码的反码(主要是用于校验是否出错)。然后是8bit 的命令码(也是从低有效位开始发),而后也是8bit 的命令码的反码。

    /* 以脉宽为低电平0.565ms、间隔高电平0.56ms、周期为1.125ms的组合表示"0"; */

    /* 以脉宽为低电平0.565ms、间隔高电平1.685ms、周期为2.25ms的组合表示"1"。 */

    二、解码KM-001红外遥控器

    平台:Digispark kickstarter 微型 Arduino usb 开发板 ATTINY85

    采集到第17个到第24个高电平的脉冲时间如下:

    706

    1753

    1694

    674

    1694

    639

    702

    571

    01101000=>22

    clip_image002[4]

    #include "DigiKeyboard.h"
    int irPin = 2; //Sensor pin connect to digital pin2 (ATINY85 pin7)
    int start_bit = 2200; //Start bit threshold (Microseconds)
    int bin_1 = 1000; //Binary 1 threshold (Microseconds)
    int bin_0 = 400; //Binary 0 threshold (Microseconds)
    const byte BIT_PER_BLOCK = 32;
     
    void setup() {
      pinMode(irPin, INPUT);
    }
     
    void loop() {
      DigiKeyboard.update(); //keep on updating the keyboard 
      // this is generally not necessary but with some older systems it seems to
      // prevent missing the first character after a delay:
      DigiKeyboard.sendKeyStroke(0);
     
      int key = getIRKey();        //Fetch the key
     
      if(key != 0) //Ignore keys that are zero
      {
        DigiKeyboard.print("=>");  //uncomment this if you want to 
        DigiKeyboard.println(key); //print out the value of the button
      }
    }
     
    /////////////////////////////////////////////////////////////
    // decode infrared signal
    /////////////////////////////////////////////////////////////
    int getIRKey() {
      int data[BIT_PER_BLOCK];
      int i;
      while(pulseIn(irPin, HIGH) < start_bit); //Wait for a start bit
     
      for(i = 0 ; i < BIT_PER_BLOCK ; i++) 
        data[i] = pulseIn(irPin, HIGH); //Start measuring bits, I only want HIGH pulses
     
      delay(100);  
      
      //add by tingpan
      /*  for(i = 16 ; i < 24; i++) { 
        DigiKeyboard.println(data[i]); //print out the value of button in binary form
        //if(data[i] == 1) result |= (1<<i-16); 
      } */ 
      
      for(i = 0 ; i < BIT_PER_BLOCK ; i++) //Parse them
      {   
        if(data[i] > bin_1) //is it a 1?
          data[i] = 1;
        else if(data[i] > bin_0) //is it a 0?
          data[i] = 0;
        else
          return -1; //Flag the data as invalid; Return -1 on invalid data
      }
      //based on NEC protocol, command data started from bit 16
      //and end with bit 24 (8 bits long)
      int result = 0;
      for(i = 16 ; i < 24; i++) { 
        DigiKeyboard.print(data[i]); //print out the value of button in binary form
        if(data[i] == 1) result |= (1<<i-16); 
      }  
      return result; //Return key number
    }

    三、红外控制电脑键盘相关按钮

    平台:平台:Digispark kickstarter 微型 Arduino usb 开发板 ATTINY85

    代码:

    #include "DigiKeyboard.h"
    // not all keys are mapped in the DigiKeyboard.h file.
    // you have to map it here
    #define KEY_HOME   0x4A
    #define KEY_PAGE_UP   0x4B
    #define KEY_PAGE_DOWN   0x4E
    #define KEY_ESCAPE   0x29
    #define KEY_UP_ARROW   0x52
    #define KEY_DOWN_ARROW   0x51
    #define KEY_LEFT_ARROW   0x50
    #define KEY_RIGHT_ARROW   0x4F
     
    //defined by tingpan
    #define KEY_ESC   0x1B
    #define KEY_UP   0x26
    #define KEY_DOWN   0x28
    #define KEY_LEFT  0x25
    #define KEY_RIGHT   0x27 
    #define KEY_LEFT_CLIK  0x01
    #define KEY_RIGHT_CLIk   0x02 
    
    int irPin = 2; //Sensor pin connect to digital pin2 (ATINY85 pin7)
    int start_bit = 2200; //Start bit threshold (Microseconds)
    int bin_1 = 1000; //Binary 1 threshold (Microseconds)
    int bin_0 = 400; //Binary 0 threshold (Microseconds)
    const byte BIT_PER_BLOCK = 32;
     
    void setup() {
      pinMode(irPin, INPUT);
    }
     
    void loop() {
      DigiKeyboard.update(); // keep updating the keyboard
      // this is generally not necessary but with some older systems it seems to
      // prevent missing the first character after a delay:
      DigiKeyboard.sendKeyStroke(0);
     
      int key = getIRKey();        //Fetch the key
     
      if(key != 0) //Ignore keys that are zero
      {
        //DigiKeyboard.print("=>");  //uncomment this if you want to 
        //DigiKeyboard.println(key); //print out the value of the button
     
        switch(key)
        {
          case 12: DigiKeyboard.println("1"); break;
          case 24: DigiKeyboard.println("2"); break;
          case 94: DigiKeyboard.println("3"); break;
          case 8: DigiKeyboard.println("4"); break;
          case 28: DigiKeyboard.println("5"); break;
          case 90: DigiKeyboard.println("6"); break;
          case 66: DigiKeyboard.println("7"); break;
          case 82: DigiKeyboard.println("8"); break;
          case 74: DigiKeyboard.println("9"); break;    
          case 22: DigiKeyboard.println("0"); break;
          case 68: DigiKeyboard.sendKeyStroke(KEY_SPACE); break;//can stop or continue
          case  7: DigiKeyboard.sendKeyStroke(KEY_ENTER); break; // enter  
          case  69: DigiKeyboard.sendKeyStroke(KEY_ESC); break; //can't use
         case 70: DigiKeyboard.sendKeyStroke(KEY_HOME); break; //Mode
          case 64: DigiKeyboard.sendKeyStroke(KEY_LEFT_ARROW); break;// <--
          case 67: DigiKeyboard.sendKeyStroke(KEY_RIGHT_ARROW); break;//-->
          case 21: DigiKeyboard.sendKeyStroke(KEY_DOWN_ARROW); break;  //down
          case 9: DigiKeyboard.sendKeyStroke(KEY_UP_ARROW); break;    //up
          case 25: DigiKeyboard.sendKeyStroke(KEY_LEFT_CLIK); break;    //can't use
          case 13: DigiKeyboard.sendKeyStroke(KEY_RIGHT_CLIk); break;    //can't use
    
        }    
      }
    }
     
    /////////////////////////////////////////////////////////////
    // decode infrared signal
    /////////////////////////////////////////////////////////////
    int getIRKey() {
      int data[BIT_PER_BLOCK];
      int i;
      while(pulseIn(irPin, HIGH) < start_bit); //Wait for a start bit
     
      for(i = 0 ; i < BIT_PER_BLOCK ; i++) 
        data[i] = pulseIn(irPin, HIGH); //Start measuring bits, I only want HIGH pulses
     
      delay(100);  
      for(i = 0 ; i < BIT_PER_BLOCK ; i++) //Parse them
      {   
        if(data[i] > bin_1) //is it a 1?
          data[i] = 1;
        else if(data[i] > bin_0) //is it a 0?
          data[i] = 0;
        else
          return -1; //Flag the data as invalid; Return -1 on invalid data
      }
     
      //based on NEC protocol, command data started from bit 16
      //and end with bit 24 (8 bits long)
      int result = 0;
      for(i = 16 ; i < 24; i++) { 
        //DigiKeyboard.print(data[i]); //print out the value of button in binary form
        if(data[i] == 1) result |= (1<<i-16);// convert="" data="" bits="" to="" integer
      }  
      return result; //Return key number
    }

    参考:

    STC的51单片机红外遥控器读码、发射程序,已试成功 - 第1页 - wxleasyland's Blog - EDN China电子设计技术

    红外协议之NEC协议_Linux教程_Linux公社-Linux系统门户网站

    Digispark红外接收器 - Powered by Discuz!

  • 相关阅读:
    CentOS 中安装和更新 git 客户端工具
    Python 快速入门笔记(2):数据类型
    Python 快速入门笔记(1):简介
    JavaScript快速入门笔记(15):Web storage 之 localStorage 和 sessionStorage
    JavaScript快速入门笔记(14):session
    JavaScript快速入门笔记(13):cookie
    JavaScript快速入门笔记(12):Ajax之XMLHttpRequest、jQuery、fetch()
    JavaScript快速入门笔记(11):事件处理
    python的线程和进程
    python中字符串编码转换
  • 原文地址:https://www.cnblogs.com/smbx-ztbz/p/4457246.html
Copyright © 2011-2022 走看看