狗趴(GodPub),开源硬件学习与实践

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 6254|回复: 2
打印 上一主题 下一主题

【二叔科技】学习红外接收

[复制链接]

13

主题

33

帖子

155

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
155
跳转到指定楼层
楼主
发表于 2015-1-2 17:27:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. IRrecv::IRrecv(int recvpin)
  2. {
  3.   irparams.recvpin = recvpin;
  4.   irparams.blinkflag = 0;
  5. }
复制代码
  1. // initialization
  2. void IRrecv::enableIRIn() {
  3.   cli();
  4.   // setup pulse clock timer interrupt
  5.   //Prescale /8 (16M/8 = 0.5 microseconds per tick)
  6.   // Therefore, the timer interval can range from 0.5 to 128 microseconds
  7.   // depending on the reset value (255 to 0)
  8.   TIMER_CONFIG_NORMAL();

  9.   //Timer2 Overflow Interrupt Enable
  10.   TIMER_ENABLE_INTR;

  11.   TIMER_RESET;

  12.   sei();  // enable interrupts

  13.   // initialize state machine variables
  14.   irparams.rcvstate = STATE_IDLE;
  15.   irparams.rawlen = 0;

  16.   // set pin modes
  17.   pinMode(irparams.recvpin, INPUT);
  18. }
复制代码
  1. // enable/disable blinking of pin 13 on IR processing
  2. void IRrecv::blink13(int blinkflag)
  3. {
  4.   irparams.blinkflag = blinkflag;
  5.   if (blinkflag)
  6.     pinMode(BLINKLED, OUTPUT);
  7. }
复制代码
  1. void IRrecv::resume() {
  2.   irparams.rcvstate = STATE_IDLE;
  3.   irparams.rawlen = 0;
  4. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏

13

主题

33

帖子

155

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
155
沙发
 楼主| 发表于 2015-1-2 17:35:27 | 只看该作者
  1. // Decodes the received IR message
  2. // Returns 0 if no data ready, 1 if data ready.
  3. // Results of decoding are stored in results
  4. int IRrecv::decode(decode_results *results) {
  5.   results->rawbuf = irparams.rawbuf;
  6.   results->rawlen = irparams.rawlen;
  7.   if (irparams.rcvstate != STATE_STOP) {
  8.     return ERR;
  9.   }
  10. #ifdef DEBUG
  11.   Serial.println("Attempting NEC decode");
  12. #endif
  13.   if (decodeNEC(results)) {
  14.     return DECODED;
  15.   }
  16. #ifdef DEBUG
  17.   Serial.println("Attempting Sony decode");
  18. #endif
  19.   if (decodeSony(results)) {
  20.     return DECODED;
  21.   }
  22. #ifdef DEBUG
  23.   Serial.println("Attempting Sanyo decode");
  24. #endif
  25.   if (decodeSanyo(results)) {
  26.     return DECODED;
  27.   }
  28. #ifdef DEBUG
  29.   Serial.println("Attempting Mitsubishi decode");
  30. #endif
  31.   if (decodeMitsubishi(results)) {
  32.     return DECODED;
  33.   }
  34. #ifdef DEBUG
  35.   Serial.println("Attempting RC5 decode");
  36. #endif  
  37.   if (decodeRC5(results)) {
  38.     return DECODED;
  39.   }
  40. #ifdef DEBUG
  41.   Serial.println("Attempting RC6 decode");
  42. #endif
  43.   if (decodeRC6(results)) {
  44.     return DECODED;
  45.   }
  46. #ifdef DEBUG
  47.     Serial.println("Attempting Panasonic decode");
  48. #endif
  49.     if (decodePanasonic(results)) {
  50.         return DECODED;
  51.     }
  52. #ifdef DEBUG
  53.     Serial.println("Attempting LG decode");
  54. #endif
  55.     if (decodeLG(results)) {
  56.         return DECODED;
  57.     }
  58. #ifdef DEBUG
  59.     Serial.println("Attempting JVC decode");
  60. #endif
  61.     if (decodeJVC(results)) {
  62.         return DECODED;
  63.     }
  64. #ifdef DEBUG
  65.   Serial.println("Attempting SAMSUNG decode");
  66. #endif
  67.   if (decodeSAMSUNG(results)) {
  68.     return DECODED;
  69.   }
  70.   // decodeHash returns a hash on any input.
  71.   // Thus, it needs to be last in the list.
  72.   // If you add any decodes, add them before this.
  73.   if (decodeHash(results)) {
  74.     return DECODED;
  75.   }
  76.   // Throw away and start over
  77.   resume();
  78.   return ERR;
  79. }
复制代码


13

主题

33

帖子

155

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
155
板凳
 楼主| 发表于 2015-1-2 17:36:13 | 只看该作者
  1. // TIMER2 interrupt code to collect raw data.
  2. // Widths of alternating SPACE, MARK are recorded in rawbuf.
  3. // Recorded in ticks of 50 microseconds.
  4. // rawlen counts the number of entries recorded so far.
  5. // First entry is the SPACE between transmissions.
  6. // As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues.
  7. // As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts
  8. ISR(TIMER_INTR_NAME)
  9. {
  10.   TIMER_RESET;

  11.   uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);

  12.   irparams.timer++; // One more 50us tick
  13.   if (irparams.rawlen >= RAWBUF) {
  14.     // Buffer overflow
  15.     irparams.rcvstate = STATE_STOP;
  16.   }
  17.   switch(irparams.rcvstate) {
  18.   case STATE_IDLE: // In the middle of a gap
  19.     if (irdata == MARK) {
  20.       if (irparams.timer < GAP_TICKS) {
  21.         // Not big enough to be a gap.
  22.         irparams.timer = 0;
  23.       }
  24.       else {
  25.         // gap just ended, record duration and start recording transmission
  26.         irparams.rawlen = 0;
  27.         irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  28.         irparams.timer = 0;
  29.         irparams.rcvstate = STATE_MARK;
  30.       }
  31.     }
  32.     break;
  33.   case STATE_MARK: // timing MARK
  34.     if (irdata == SPACE) {   // MARK ended, record time
  35.       irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  36.       irparams.timer = 0;
  37.       irparams.rcvstate = STATE_SPACE;
  38.     }
  39.     break;
  40.   case STATE_SPACE: // timing SPACE
  41.     if (irdata == MARK) { // SPACE just ended, record it
  42.       irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  43.       irparams.timer = 0;
  44.       irparams.rcvstate = STATE_MARK;
  45.     }
  46.     else { // SPACE
  47.       if (irparams.timer > GAP_TICKS) {
  48.         // big SPACE, indicates gap between codes
  49.         // Mark current code as ready for processing
  50.         // Switch to STOP
  51.         // Don't reset timer; keep counting space width
  52.         irparams.rcvstate = STATE_STOP;
  53.       }
  54.     }
  55.     break;
  56.   case STATE_STOP: // waiting, measuring gap
  57.     if (irdata == MARK) { // reset gap timer
  58.       irparams.timer = 0;
  59.     }
  60.     break;
  61.   }

  62.   if (irparams.blinkflag) {
  63.     if (irdata == MARK) {
  64.       BLINKLED_ON();  // turn pin 13 LED on
  65.     }
  66.     else {
  67.       BLINKLED_OFF();  // turn pin 13 LED off
  68.     }
  69.   }
  70. }

  71. void IRrecv::resume() {
  72.   irparams.rcvstate = STATE_IDLE;
  73.   irparams.rawlen = 0;
  74. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|狗趴(GodPub) Arduino&Raspberry Pi开源硬件学习与实践[QQ群:20085629]  

GMT+8, 2024-5-2 12:49 , Processed in 0.036002 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表