44 * Copyright 2009 Ken Shirriff 
55 * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html 
66 * 
7+  * Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers 
8+  * 
79 * Interrupt code based on NECIRrcv by Joe Knapp 
810 * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 
911 * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ 
@@ -169,15 +171,15 @@ void IRsend::sendRC6(unsigned long data, int nbits)
169171void  IRsend::mark (int  time) {
170172 //  Sends an IR mark for the specified number of microseconds.
171173 //  The mark output is modulated at the PWM frequency.
172-  TCCR2A |=  _BV (COM2B1) ; //  Enable pin 3 PWM output
174+  TIMER_ENABLE_PWM ; //  Enable pin 3 PWM output
173175 delayMicroseconds (time);
174176}
175177
176178/*  Leave pin off for time (given in microseconds) */ 
177179void  IRsend::space (int  time) {
178180 //  Sends an IR space for the specified number of microseconds.
179181 //  A space is no output, so the PWM output is disabled.
180-  TCCR2A &= ~( _BV (COM2B1)) ; //  Disable pin 3 PWM output
182+  TIMER_DISABLE_PWM ; //  Disable pin 3 PWM output
181183 delayMicroseconds (time);
182184}
183185
@@ -195,21 +197,17 @@ void IRsend::enableIROut(int khz) {
195197
196198
197199 //  Disable the Timer2 Interrupt (which is used for receiving IR)
198-  TIMSK2 &=  ~_BV (TOIE2) ; // Timer2 Overflow Interrupt
200+  TIMER_DISABLE_INTR ; // Timer2 Overflow Interrupt
199201
200-  pinMode (3 , OUTPUT);
201-  digitalWrite (3 , LOW); //  When not sending PWM, we want it low
202+  pinMode (TIMER_PWM_PIN , OUTPUT);
203+  digitalWrite (TIMER_PWM_PIN , LOW); //  When not sending PWM, we want it low
202204
203205 //  COM2A = 00: disconnect OC2A
204206 //  COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted
205207 //  WGM2 = 101: phase-correct PWM with OCRA as top
206208 //  CS2 = 000: no prescaling
207-  TCCR2A = _BV (WGM20);
208-  TCCR2B = _BV (WGM22) | _BV (CS20);
209- 
210209 //  The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A.
211-  OCR2A = SYSCLOCK / 2  / khz / 1000 ;
212-  OCR2B = OCR2A / 3 ; //  33% duty cycle
210+  TIMER_CONFIG_KHZ (khz);
213211}
214212
215213IRrecv::IRrecv (int  recvpin)
@@ -220,28 +218,24 @@ IRrecv::IRrecv(int recvpin)
220218
221219//  initialization
222220void  IRrecv::enableIRIn () {
221+  cli ();
223222 //  setup pulse clock timer interrupt
224-  TCCR2A = 0 ; //  normal mode
225- 
226223 // Prescale /8 (16M/8 = 0.5 microseconds per tick)
227224 //  Therefore, the timer interval can range from 0.5 to 128 microseconds
228225 //  depending on the reset value (255 to 0)
229-  cbi (TCCR2B,CS22);
230-  sbi (TCCR2B,CS21);
231-  cbi (TCCR2B,CS20);
226+  TIMER_CONFIG_NORMAL ();
232227
233228 // Timer2 Overflow Interrupt Enable
234-  sbi (TIMSK2,TOIE2) ;
229+  TIMER_ENABLE_INTR ;
235230
236-  RESET_TIMER2 ;
231+  TIMER_RESET ;
237232
238233 sei (); //  enable interrupts
239234
240235 //  initialize state machine variables
241236 irparams.rcvstate  = STATE_IDLE;
242237 irparams.rawlen  = 0 ;
243238
244- 
245239 //  set pin modes
246240 pinMode (irparams.recvpin , INPUT);
247241}
@@ -261,9 +255,9 @@ void IRrecv::blink13(int blinkflag)
261255//  First entry is the SPACE between transmissions.
262256//  As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues.
263257//  As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts
264- ISR (TIMER2_OVF_vect )
258+ ISR (TIMER_INTR_NAME )
265259{
266-  RESET_TIMER2 ;
260+  TIMER_RESET ;
267261
268262 uint8_t  irdata = (uint8_t )digitalRead (irparams.recvpin );
269263
@@ -320,10 +314,10 @@ ISR(TIMER2_OVF_vect)
320314
321315 if  (irparams.blinkflag ) {
322316 if  (irdata == MARK) {
323-  PORTB |= B00100000 ; //  turn pin 13 LED on
317+  BLINKLED_ON () ; //  turn pin 13 LED on
324318 } 
325319 else  {
326-  PORTB &= B11011111 ; //  turn pin 13 LED off
320+  BLINKLED_OFF () ; //  turn pin 13 LED off
327321 }
328322 }
329323}
0 commit comments