IRremoteInt.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * IRremote
  3. * Version 0.1 July, 2009
  4. * Copyright 2009 Ken Shirriff
  5. * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
  6. *
  7. * Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
  8. *
  9. * Interrupt code based on NECIRrcv by Joe Knapp
  10. * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
  11. * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
  12. *
  13. * JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
  14. */
  15. #ifndef IRremoteint_h
  16. #define IRremoteint_h
  17. #if defined(ARDUINO) && ARDUINO >= 100
  18. #include <Arduino.h>
  19. #else
  20. #include <WProgram.h>
  21. #endif
  22. // define which timer to use
  23. //
  24. // Uncomment the timer you wish to use on your board. If you
  25. // are using another library which uses timer2, you have options
  26. // to switch IRremote to use a different timer.
  27. // Arduino Mega
  28. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  29. //#define IR_USE_TIMER1 // tx = pin 11
  30. #define IR_USE_TIMER2 // tx = pin 9
  31. //#define IR_USE_TIMER3 // tx = pin 5
  32. //#define IR_USE_TIMER4 // tx = pin 6
  33. //#define IR_USE_TIMER5 // tx = pin 46
  34. // Teensy 1.0
  35. #elif defined(__AVR_AT90USB162__)
  36. #define IR_USE_TIMER1 // tx = pin 17
  37. // Teensy 2.0
  38. #elif defined(__AVR_ATmega32U4__)
  39. //#define IR_USE_TIMER1 // tx = pin 14
  40. //#define IR_USE_TIMER3 // tx = pin 9
  41. #define IR_USE_TIMER4_HS // tx = pin 10
  42. // Teensy++ 1.0 & 2.0
  43. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  44. //#define IR_USE_TIMER1 // tx = pin 25
  45. #define IR_USE_TIMER2 // tx = pin 1
  46. //#define IR_USE_TIMER3 // tx = pin 16
  47. // Sanguino
  48. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  49. //#define IR_USE_TIMER1 // tx = pin 13
  50. #define IR_USE_TIMER2 // tx = pin 14
  51. // Atmega8
  52. #elif defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__)
  53. #define IR_USE_TIMER1 // tx = pin 9
  54. // Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
  55. #else
  56. //#define IR_USE_TIMER1 // tx = pin 9
  57. #define IR_USE_TIMER2 // tx = pin 3
  58. #endif
  59. #ifdef F_CPU
  60. #define SYSCLOCK F_CPU // main Arduino clock
  61. #else
  62. #define SYSCLOCK 16000000 // main Arduino clock
  63. #endif
  64. #define ERR 0
  65. #define DECODED 1
  66. // defines for setting and clearing register bits
  67. #ifndef cbi
  68. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  69. #endif
  70. #ifndef sbi
  71. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  72. #endif
  73. // Pulse parms are *50-100 for the Mark and *50+100 for the space
  74. // First MARK is the one after the long gap
  75. // pulse parameters in usec
  76. #define NEC_HDR_MARK 9000
  77. #define NEC_HDR_SPACE 4500
  78. #define NEC_BIT_MARK 560
  79. #define NEC_ONE_SPACE 1600
  80. #define NEC_ZERO_SPACE 560
  81. #define NEC_RPT_SPACE 2250
  82. #define SONY_HDR_MARK 2400
  83. #define SONY_HDR_SPACE 600
  84. #define SONY_ONE_MARK 1200
  85. #define SONY_ZERO_MARK 600
  86. #define SONY_RPT_LENGTH 45000
  87. #define SONY_DOUBLE_SPACE_USECS 500 // usually ssee 713 - not using ticks as get number wrapround
  88. // SA 8650B
  89. #define SANYO_HDR_MARK 3500 // seen range 3500
  90. #define SANYO_HDR_SPACE 950 // seen 950
  91. #define SANYO_ONE_MARK 2400 // seen 2400
  92. #define SANYO_ZERO_MARK 700 // seen 700
  93. #define SANYO_DOUBLE_SPACE_USECS 800 // usually ssee 713 - not using ticks as get number wrapround
  94. #define SANYO_RPT_LENGTH 45000
  95. // Mitsubishi RM 75501
  96. // 14200 7 41 7 42 7 42 7 17 7 17 7 18 7 41 7 18 7 17 7 17 7 18 7 41 8 17 7 17 7 18 7 17 7
  97. // #define MITSUBISHI_HDR_MARK 250 // seen range 3500
  98. #define MITSUBISHI_HDR_SPACE 350 // 7*50+100
  99. #define MITSUBISHI_ONE_MARK 1950 // 41*50-100
  100. #define MITSUBISHI_ZERO_MARK 750 // 17*50-100
  101. // #define MITSUBISHI_DOUBLE_SPACE_USECS 800 // usually ssee 713 - not using ticks as get number wrapround
  102. // #define MITSUBISHI_RPT_LENGTH 45000
  103. #define RC5_T1 889
  104. #define RC5_RPT_LENGTH 46000
  105. #define RC6_HDR_MARK 2666
  106. #define RC6_HDR_SPACE 889
  107. #define RC6_T1 444
  108. #define RC6_RPT_LENGTH 46000
  109. #define SHARP_BIT_MARK 245
  110. #define SHARP_ONE_SPACE 1805
  111. #define SHARP_ZERO_SPACE 795
  112. #define SHARP_GAP 600000
  113. #define SHARP_TOGGLE_MASK 0x3FF
  114. #define SHARP_RPT_SPACE 3000
  115. #define DISH_HDR_MARK 400
  116. #define DISH_HDR_SPACE 6100
  117. #define DISH_BIT_MARK 400
  118. #define DISH_ONE_SPACE 1700
  119. #define DISH_ZERO_SPACE 2800
  120. #define DISH_RPT_SPACE 6200
  121. #define DISH_TOP_BIT 0x8000
  122. #define PANASONIC_HDR_MARK 3502
  123. #define PANASONIC_HDR_SPACE 1750
  124. #define PANASONIC_BIT_MARK 502
  125. #define PANASONIC_ONE_SPACE 1244
  126. #define PANASONIC_ZERO_SPACE 400
  127. #define JVC_HDR_MARK 8000
  128. #define JVC_HDR_SPACE 4000
  129. #define JVC_BIT_MARK 600
  130. #define JVC_ONE_SPACE 1600
  131. #define JVC_ZERO_SPACE 550
  132. #define JVC_RPT_LENGTH 60000
  133. #define SHARP_BITS 15
  134. #define DISH_BITS 16
  135. #define TOLERANCE 25 // percent tolerance in measurements
  136. #define LTOL (1.0 - TOLERANCE/100.)
  137. #define UTOL (1.0 + TOLERANCE/100.)
  138. #define _GAP 5000 // Minimum map between transmissions
  139. #define GAP_TICKS (_GAP/USECPERTICK)
  140. #define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK))
  141. #define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1))
  142. #ifndef DEBUG
  143. int MATCH(int measured, int desired) {return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);}
  144. int MATCH_MARK(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us + MARK_EXCESS));}
  145. int MATCH_SPACE(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us - MARK_EXCESS));}
  146. // Debugging versions are in IRremote.cpp
  147. #endif
  148. // receiver states
  149. #define STATE_IDLE 2
  150. #define STATE_MARK 3
  151. #define STATE_SPACE 4
  152. #define STATE_STOP 5
  153. // information for the interrupt handler
  154. typedef struct {
  155. uint8_t recvpin; // pin for IR data from detector
  156. uint8_t rcvstate; // state machine
  157. uint8_t blinkflag; // TRUE to enable blinking of pin 13 on IR processing
  158. unsigned int timer; // state timer, counts 50uS ticks.
  159. unsigned int rawbuf[RAWBUF]; // raw data
  160. uint8_t rawlen; // counter of entries in rawbuf
  161. }
  162. irparams_t;
  163. // Defined in IRremote.cpp
  164. extern volatile irparams_t irparams;
  165. // IR detector output is active low
  166. #define MARK 0
  167. #define SPACE 1
  168. #define TOPBIT 0x80000000
  169. #define NEC_BITS 32
  170. #define SONY_BITS 12
  171. #define SANYO_BITS 12
  172. #define MITSUBISHI_BITS 16
  173. #define MIN_RC5_SAMPLES 11
  174. #define MIN_RC6_SAMPLES 1
  175. #define PANASONIC_BITS 48
  176. #define JVC_BITS 16
  177. // defines for timer2 (8 bits)
  178. #if defined(IR_USE_TIMER2)
  179. #define TIMER_RESET
  180. #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))
  181. #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1)))
  182. #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))
  183. #define TIMER_DISABLE_INTR (TIMSK2 = 0)
  184. #define TIMER_INTR_NAME TIMER2_COMPA_vect
  185. #define TIMER_CONFIG_KHZ(val) ({ \
  186. const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
  187. TCCR2A = _BV(WGM20); \
  188. TCCR2B = _BV(WGM22) | _BV(CS20); \
  189. OCR2A = pwmval; \
  190. OCR2B = pwmval / 3; \
  191. })
  192. #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000)
  193. #if (TIMER_COUNT_TOP < 256)
  194. #define TIMER_CONFIG_NORMAL() ({ \
  195. TCCR2A = _BV(WGM21); \
  196. TCCR2B = _BV(CS20); \
  197. OCR2A = TIMER_COUNT_TOP; \
  198. TCNT2 = 0; \
  199. })
  200. #else
  201. #define TIMER_CONFIG_NORMAL() ({ \
  202. TCCR2A = _BV(WGM21); \
  203. TCCR2B = _BV(CS21); \
  204. OCR2A = TIMER_COUNT_TOP / 8; \
  205. TCNT2 = 0; \
  206. })
  207. #endif
  208. #if defined(CORE_OC2B_PIN)
  209. #define TIMER_PWM_PIN CORE_OC2B_PIN /* Teensy */
  210. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  211. #define TIMER_PWM_PIN 9 /* Arduino Mega */
  212. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  213. #define TIMER_PWM_PIN 14 /* Sanguino */
  214. #else
  215. #define TIMER_PWM_PIN 3 /* Arduino Duemilanove, Diecimila, LilyPad, etc */
  216. #endif
  217. // defines for timer1 (16 bits)
  218. #elif defined(IR_USE_TIMER1)
  219. #define TIMER_RESET
  220. #define TIMER_ENABLE_PWM (TCCR1A |= _BV(COM1A1))
  221. #define TIMER_DISABLE_PWM (TCCR1A &= ~(_BV(COM1A1)))
  222. #if defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__)
  223. #define TIMER_ENABLE_INTR (TIMSK = _BV(OCIE1A))
  224. #define TIMER_DISABLE_INTR (TIMSK = 0)
  225. #else
  226. #define TIMER_ENABLE_INTR (TIMSK1 = _BV(OCIE1A))
  227. #define TIMER_DISABLE_INTR (TIMSK1 = 0)
  228. #endif
  229. #define TIMER_INTR_NAME TIMER1_COMPA_vect
  230. #define TIMER_CONFIG_KHZ(val) ({ \
  231. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  232. TCCR1A = _BV(WGM11); \
  233. TCCR1B = _BV(WGM13) | _BV(CS10); \
  234. ICR1 = pwmval; \
  235. OCR1A = pwmval / 3; \
  236. })
  237. #define TIMER_CONFIG_NORMAL() ({ \
  238. TCCR1A = 0; \
  239. TCCR1B = _BV(WGM12) | _BV(CS10); \
  240. OCR1A = SYSCLOCK * USECPERTICK / 1000000; \
  241. TCNT1 = 0; \
  242. })
  243. #if defined(CORE_OC1A_PIN)
  244. #define TIMER_PWM_PIN CORE_OC1A_PIN /* Teensy */
  245. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  246. #define TIMER_PWM_PIN 11 /* Arduino Mega */
  247. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  248. #define TIMER_PWM_PIN 13 /* Sanguino */
  249. #else
  250. #define TIMER_PWM_PIN 9 /* Arduino Duemilanove, Diecimila, LilyPad, etc */
  251. #endif
  252. // defines for timer3 (16 bits)
  253. #elif defined(IR_USE_TIMER3)
  254. #define TIMER_RESET
  255. #define TIMER_ENABLE_PWM (TCCR3A |= _BV(COM3A1))
  256. #define TIMER_DISABLE_PWM (TCCR3A &= ~(_BV(COM3A1)))
  257. #define TIMER_ENABLE_INTR (TIMSK3 = _BV(OCIE3A))
  258. #define TIMER_DISABLE_INTR (TIMSK3 = 0)
  259. #define TIMER_INTR_NAME TIMER3_COMPA_vect
  260. #define TIMER_CONFIG_KHZ(val) ({ \
  261. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  262. TCCR3A = _BV(WGM31); \
  263. TCCR3B = _BV(WGM33) | _BV(CS30); \
  264. ICR3 = pwmval; \
  265. OCR3A = pwmval / 3; \
  266. })
  267. #define TIMER_CONFIG_NORMAL() ({ \
  268. TCCR3A = 0; \
  269. TCCR3B = _BV(WGM32) | _BV(CS30); \
  270. OCR3A = SYSCLOCK * USECPERTICK / 1000000; \
  271. TCNT3 = 0; \
  272. })
  273. #if defined(CORE_OC3A_PIN)
  274. #define TIMER_PWM_PIN CORE_OC3A_PIN /* Teensy */
  275. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  276. #define TIMER_PWM_PIN 5 /* Arduino Mega */
  277. #else
  278. #error "Please add OC3A pin number here\n"
  279. #endif
  280. // defines for timer4 (10 bits, high speed option)
  281. #elif defined(IR_USE_TIMER4_HS)
  282. #define TIMER_RESET
  283. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1))
  284. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1)))
  285. #define TIMER_ENABLE_INTR (TIMSK4 = _BV(TOIE4))
  286. #define TIMER_DISABLE_INTR (TIMSK4 = 0)
  287. #define TIMER_INTR_NAME TIMER4_OVF_vect
  288. #define TIMER_CONFIG_KHZ(val) ({ \
  289. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  290. TCCR4A = (1<<PWM4A); \
  291. TCCR4B = _BV(CS40); \
  292. TCCR4C = 0; \
  293. TCCR4D = (1<<WGM40); \
  294. TCCR4E = 0; \
  295. TC4H = pwmval >> 8; \
  296. OCR4C = pwmval; \
  297. TC4H = (pwmval / 3) >> 8; \
  298. OCR4A = (pwmval / 3) & 255; \
  299. })
  300. #define TIMER_CONFIG_NORMAL() ({ \
  301. TCCR4A = 0; \
  302. TCCR4B = _BV(CS40); \
  303. TCCR4C = 0; \
  304. TCCR4D = 0; \
  305. TCCR4E = 0; \
  306. TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \
  307. OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \
  308. TC4H = 0; \
  309. TCNT4 = 0; \
  310. })
  311. #if defined(CORE_OC4A_PIN)
  312. #define TIMER_PWM_PIN CORE_OC4A_PIN /* Teensy */
  313. #elif defined(__AVR_ATmega32U4__)
  314. #define TIMER_PWM_PIN 13 /* Leonardo */
  315. #else
  316. #error "Please add OC4A pin number here\n"
  317. #endif
  318. // defines for timer4 (16 bits)
  319. #elif defined(IR_USE_TIMER4)
  320. #define TIMER_RESET
  321. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1))
  322. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1)))
  323. #define TIMER_ENABLE_INTR (TIMSK4 = _BV(OCIE4A))
  324. #define TIMER_DISABLE_INTR (TIMSK4 = 0)
  325. #define TIMER_INTR_NAME TIMER4_COMPA_vect
  326. #define TIMER_CONFIG_KHZ(val) ({ \
  327. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  328. TCCR4A = _BV(WGM41); \
  329. TCCR4B = _BV(WGM43) | _BV(CS40); \
  330. ICR4 = pwmval; \
  331. OCR4A = pwmval / 3; \
  332. })
  333. #define TIMER_CONFIG_NORMAL() ({ \
  334. TCCR4A = 0; \
  335. TCCR4B = _BV(WGM42) | _BV(CS40); \
  336. OCR4A = SYSCLOCK * USECPERTICK / 1000000; \
  337. TCNT4 = 0; \
  338. })
  339. #if defined(CORE_OC4A_PIN)
  340. #define TIMER_PWM_PIN CORE_OC4A_PIN
  341. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  342. #define TIMER_PWM_PIN 6 /* Arduino Mega */
  343. #else
  344. #error "Please add OC4A pin number here\n"
  345. #endif
  346. // defines for timer5 (16 bits)
  347. #elif defined(IR_USE_TIMER5)
  348. #define TIMER_RESET
  349. #define TIMER_ENABLE_PWM (TCCR5A |= _BV(COM5A1))
  350. #define TIMER_DISABLE_PWM (TCCR5A &= ~(_BV(COM5A1)))
  351. #define TIMER_ENABLE_INTR (TIMSK5 = _BV(OCIE5A))
  352. #define TIMER_DISABLE_INTR (TIMSK5 = 0)
  353. #define TIMER_INTR_NAME TIMER5_COMPA_vect
  354. #define TIMER_CONFIG_KHZ(val) ({ \
  355. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  356. TCCR5A = _BV(WGM51); \
  357. TCCR5B = _BV(WGM53) | _BV(CS50); \
  358. ICR5 = pwmval; \
  359. OCR5A = pwmval / 3; \
  360. })
  361. #define TIMER_CONFIG_NORMAL() ({ \
  362. TCCR5A = 0; \
  363. TCCR5B = _BV(WGM52) | _BV(CS50); \
  364. OCR5A = SYSCLOCK * USECPERTICK / 1000000; \
  365. TCNT5 = 0; \
  366. })
  367. #if defined(CORE_OC5A_PIN)
  368. #define TIMER_PWM_PIN CORE_OC5A_PIN
  369. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  370. #define TIMER_PWM_PIN 46 /* Arduino Mega */
  371. #else
  372. #error "Please add OC5A pin number here\n"
  373. #endif
  374. #else // unknown timer
  375. #error "Internal code configuration error, no known IR_USE_TIMER# defined\n"
  376. #endif
  377. // defines for blinking the LED
  378. #if defined(CORE_LED0_PIN)
  379. #define BLINKLED CORE_LED0_PIN
  380. #define BLINKLED_ON() (digitalWrite(CORE_LED0_PIN, HIGH))
  381. #define BLINKLED_OFF() (digitalWrite(CORE_LED0_PIN, LOW))
  382. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  383. #define BLINKLED 13
  384. #define BLINKLED_ON() (PORTB |= B10000000)
  385. #define BLINKLED_OFF() (PORTB &= B01111111)
  386. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  387. #define BLINKLED 0
  388. #define BLINKLED_ON() (PORTD |= B00000001)
  389. #define BLINKLED_OFF() (PORTD &= B11111110)
  390. #else
  391. #define BLINKLED 13
  392. #define BLINKLED_ON() (PORTB |= B00100000)
  393. #define BLINKLED_OFF() (PORTB &= B11011111)
  394. #endif
  395. #endif