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

NEC协议格式:

Digispark红外接收器-LMLPHP

首次发送的是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

Digispark红外接收器-LMLPHP

#include "DigiKeyboard.h"
int irPin = ; //Sensor pin connect to digital pin2 (ATINY85 pin7)
int start_bit = ; //Start bit threshold (Microseconds)
int bin_1 = ; //Binary 1 threshold (Microseconds)
int bin_0 = ; //Binary 0 threshold (Microseconds)
const byte BIT_PER_BLOCK = ; 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(); int key = getIRKey(); //Fetch the key if(key != ) //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 = ; i < BIT_PER_BLOCK ; i++)
data[i] = pulseIn(irPin, HIGH); //Start measuring bits, I only want HIGH pulses delay(); //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 = ; i < BIT_PER_BLOCK ; i++) //Parse them
{
if(data[i] > bin_1) //is it a 1?
data[i] = ;
else if(data[i] > bin_0) //is it a 0?
data[i] = ;
else
return -; //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 = ;
for(i = ; i < ; i++) {
DigiKeyboard.print(data[i]); //print out the value of button in binary form
if(data[i] == ) result |= (<<i-);
}
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 = ; //Sensor pin connect to digital pin2 (ATINY85 pin7)
int start_bit = ; //Start bit threshold (Microseconds)
int bin_1 = ; //Binary 1 threshold (Microseconds)
int bin_0 = ; //Binary 0 threshold (Microseconds)
const byte BIT_PER_BLOCK = ; 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(); int key = getIRKey(); //Fetch the key if(key != ) //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 : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.println(""); break;
case : DigiKeyboard.sendKeyStroke(KEY_SPACE); break;//can stop or continue
case : DigiKeyboard.sendKeyStroke(KEY_ENTER); break; // enter
case : DigiKeyboard.sendKeyStroke(KEY_ESC); break; //can't use
case : DigiKeyboard.sendKeyStroke(KEY_HOME); break; //Mode
case : DigiKeyboard.sendKeyStroke(KEY_LEFT_ARROW); break;// <--
case : DigiKeyboard.sendKeyStroke(KEY_RIGHT_ARROW); break;//-->
case : DigiKeyboard.sendKeyStroke(KEY_DOWN_ARROW); break; //down
case : DigiKeyboard.sendKeyStroke(KEY_UP_ARROW); break; //up
case : DigiKeyboard.sendKeyStroke(KEY_LEFT_CLIK); break; //can't use
case : 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 = ; i < BIT_PER_BLOCK ; i++)
data[i] = pulseIn(irPin, HIGH); //Start measuring bits, I only want HIGH pulses delay();
for(i = ; i < BIT_PER_BLOCK ; i++) //Parse them
{
if(data[i] > bin_1) //is it a 1?
data[i] = ;
else if(data[i] > bin_0) //is it a 0?
data[i] = ;
else
return -; //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 = ;
for(i = ; i < ; i++) {
//DigiKeyboard.print(data[i]); //print out the value of button in binary form
if(data[i] == ) result |= (<<i-);// 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!

05-11 19:41