Arduino 已经集成了红外库,其地址如下:
D:Program Filesarduino-1.7.10librariesRobo
tiRremote
我记得以前做格力遥控的分析,这次做起来就很快
原来的地址
红外遥控介绍与接收 http://blog.sina.com.cn/s/blog_7e7fa4c80102vmbc.html
红外编码与解码——GREE格力遥控
http://blog.sina.com.cn/s/blog_7e7fa4c80102vmbd.html
http://blog.sina.com.cn/s/blog_7e7fa4c80102vmv3.html
明白了原理做起来就很快啦
写一下移植过程
1、IRremoteInt.h RAWBUF的宏定义,记录高低电平的时间
怎么算?GREE格力:起始码(S)+35位数据码+连接码(C)+32位数据码 = 69 bit
buf len = 69*2 + 1(头)+1(尾)= 140 长度 ,默认为101
2、
USECPERTICK timer中断间隔时 默认是50 表示中断间隔是50us
3、
GAP_TICKS 表示最大的时间间隔,超过即表示另外一个码,
GREE格力最长的是 连接码C电平宽度为:600us低电平+20000us高电平
_GAP 这里修改为比20000us大一些即可 我设置21000
4、IRremote.h
decode_type_t; 添加IR类型
以及class函数
5、编写ir_GREE.cpp 解码与编码函数
bool IRrecv::decodeGree (decode_results *results)
void IRsend::sendGree (unsigned long data, int nbits)
6、添加Gree的解码程序到irRecv.cpp
int IRrecv::decode (decode_results *results)中
代码在原来博文中都有
- #include
- int RECV_PIN = 11;
- int BUTTON_PIN = 7;
- int STATUS_PIN = 8;
- IRrecv irrecv(RECV_PIN);
- IRsend irsend;
- decode_results results;
- void setup()
- {
- Serial.begin(9600);
- irrecv.enableIRIn(); // Start the receiver
- pinMode(BUTTON_PIN, INPUT);
- pinMode(STATUS_PIN, OUTPUT);
- }
- // Storage for the recorded code
- int codeType = -1; // The type of code
- unsigned long codeValue; // The code value if not raw
- unsigned int rawCodes[RAWBUF]; // The durations if raw
- int codeLen; // The length of the code
- int toggle = 0; // The RC5/6 toggle state
- // Stores the code for later playback
- // Most of this code is just logging
- void storeCode(decode_results *results) {
- codeType = results->decode_type;
- int count = results->rawlen;
- if (codeType == UNKNOWN) {
- Serial.println("Received unknown code, saving as raw");
- codeLen = results->rawlen - 1;
- // To store raw codes:
- // Drop first value (gap)
- // Convert from ticks to microseconds
- // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
- for (int i = 1; i <= codeLen; i++) {
- if (i % 2) {
- // Mark
- rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
- Serial.print(" m");
- }
- else {
- // Space
- rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
- Serial.print(" s");
- }
- Serial.print(rawCodes[i - 1], DEC);
- }
- Serial.println("");
- }
- else {
- if (codeType == NEC) {
- Serial.print("Received NEC: ");
- if (results->value == REPEAT) {
- // Don't record a NEC repeat value as that's useless.
- Serial.println("repeat; ignoring.");
- return;
- }
- }
- else if (codeType == GREE) {
- Serial.print("Received GREE: ");
- }
- else if (codeType == SONY) {
- Serial.print("Received SONY: ");
- }
- else if (codeType == PANASONIC) {
- Serial.print("Received PANASONIC: ");
- }
- else if (codeType == JVC) {
- Serial.print("Received JVC: ");
- }
- else if (codeType == RC5) {
- Serial.print("Received RC5: ");
- }
- else if (codeType == RC6) {
- Serial.print("Received RC6: ");
- }
- else {
- Serial.print("Unexpected codeType ");
- Serial.print(codeType, DEC);
- Serial.println("");
- }
- Serial.println(results->value, HEX);
- codeValue = results->value;
- codeLen = results->bits;
- }
- }
- void sendCode(int repeat) {
- if (codeType == NEC) {
- if (repeat) {
- irsend.sendNEC(REPEAT, codeLen);
- Serial.println("Sent NEC repeat");
- }
- else {
- irsend.sendNEC(codeValue, codeLen);
- Serial.print("Sent NEC ");
- Serial.println(codeValue, HEX);
- }
- }
- else if (codeType == GREE){
- irsend.sendGree(codeValue, codeLen);
- Serial.print("Sent NEC ");
- Serial.println(codeValue, HEX);
- }
- else if (codeType == SONY) {
- irsend.sendSony(codeValue, codeLen);
- Serial.print("Sent Sony ");
- Serial.println(codeValue, HEX);
- }
- else if (codeType == PANASONIC) {
- irsend.sendPanasonic(codeValue, codeLen);
- Serial.print("Sent Panasonic");
- Serial.println(codeValue, HEX);
- }
- else if (codeType == JVC) {
- irsend.sendPanasonic(codeValue, codeLen);
- Serial.print("Sent JVC");
- Serial.println(codeValue, HEX);
- }
- else if (codeType == RC5 || codeType == RC6) {
- if (!repeat) {
- // Flip the toggle bit for a new button press
- toggle = 1 - toggle;
- }
- // Put the toggle bit into the code to send
- codeValue = codeValue & ~(1 << (codeLen - 1));
- codeValue = codeValue | (toggle << (codeLen - 1));
- if (codeType == RC5) {
- Serial.print("Sent RC5 ");
- Serial.println(codeValue, HEX);
- irsend.sendRC5(codeValue, codeLen);
- }
- else {
- irsend.sendRC6(codeValue, codeLen);
- Serial.print("Sent RC6 ");
- Serial.println(codeValue, HEX);
- }
- }
- else if (codeType == UNKNOWN /* i.e. raw */) {
- // Assume 38 KHz
- irsend.sendRaw(rawCodes, codeLen, 38);
- Serial.println("Sent raw");
- }
- }
- int lastButtonState;
- void loop() {
- // If button pressed, send the code.
- int buttonState = digitalRead(BUTTON_PIN);
-
- if (lastButtonState == HIGH && buttonState == LOW) {
- Serial.println("Released");
- irrecv.enableIRIn(); // Re-enable receiver
- }
- if (buttonState==LOW) {
- Serial.println("Pressed, sending");
- digitalWrite(STATUS_PIN, LOW);
- sendCode(lastButtonState == buttonState);
- digitalWrite(STATUS_PIN, HIGH);
- delay(50); // Wait a bit between retransmissions
- }
- else if (irrecv.decode(&results)) {
- digitalWrite(STATUS_PIN, LOW);
- storeCode(&results);
- irrecv.resume(); // resume receiver
- digitalWrite(STATUS_PIN, HIGH);
- }
- lastButtonState = buttonState;
-
- }
其实可以用来做万能遥控器,
Landzo C1 的套件中并没有红外发射模块,并不能对其进行测试