4
4
* Copyright 2009 Ken Shirriff
5
5
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
6
6
*
7
+ * Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
8
+ *
7
9
* Interrupt code based on NECIRrcv by Joe Knapp
8
10
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
9
11
* 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)
169
171
void IRsend::mark (int time) {
170
172
// Sends an IR mark for the specified number of microseconds.
171
173
// 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
173
175
delayMicroseconds (time);
174
176
}
175
177
176
178
/* Leave pin off for time (given in microseconds) */
177
179
void IRsend::space (int time) {
178
180
// Sends an IR space for the specified number of microseconds.
179
181
// 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
181
183
delayMicroseconds (time);
182
184
}
183
185
@@ -195,21 +197,17 @@ void IRsend::enableIROut(int khz) {
195
197
196
198
197
199
// Disable the Timer2 Interrupt (which is used for receiving IR)
198
- TIMSK2 &= ~_BV (TOIE2) ; // Timer2 Overflow Interrupt
200
+ TIMER_DISABLE_INTR ; // Timer2 Overflow Interrupt
199
201
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
202
204
203
205
// COM2A = 00: disconnect OC2A
204
206
// COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted
205
207
// WGM2 = 101: phase-correct PWM with OCRA as top
206
208
// CS2 = 000: no prescaling
207
- TCCR2A = _BV (WGM20);
208
- TCCR2B = _BV (WGM22) | _BV (CS20);
209
-
210
209
// 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);
213
211
}
214
212
215
213
IRrecv::IRrecv (int recvpin)
@@ -220,28 +218,24 @@ IRrecv::IRrecv(int recvpin)
220
218
221
219
// initialization
222
220
void IRrecv::enableIRIn () {
221
+ cli ();
223
222
// setup pulse clock timer interrupt
224
- TCCR2A = 0 ; // normal mode
225
-
226
223
// Prescale /8 (16M/8 = 0.5 microseconds per tick)
227
224
// Therefore, the timer interval can range from 0.5 to 128 microseconds
228
225
// depending on the reset value (255 to 0)
229
- cbi (TCCR2B,CS22);
230
- sbi (TCCR2B,CS21);
231
- cbi (TCCR2B,CS20);
226
+ TIMER_CONFIG_NORMAL ();
232
227
233
228
// Timer2 Overflow Interrupt Enable
234
- sbi (TIMSK2,TOIE2) ;
229
+ TIMER_ENABLE_INTR ;
235
230
236
- RESET_TIMER2 ;
231
+ TIMER_RESET ;
237
232
238
233
sei (); // enable interrupts
239
234
240
235
// initialize state machine variables
241
236
irparams.rcvstate = STATE_IDLE;
242
237
irparams.rawlen = 0 ;
243
238
244
-
245
239
// set pin modes
246
240
pinMode (irparams.recvpin , INPUT);
247
241
}
@@ -261,9 +255,9 @@ void IRrecv::blink13(int blinkflag)
261
255
// First entry is the SPACE between transmissions.
262
256
// As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues.
263
257
// 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 )
265
259
{
266
- RESET_TIMER2 ;
260
+ TIMER_RESET ;
267
261
268
262
uint8_t irdata = (uint8_t )digitalRead (irparams.recvpin );
269
263
@@ -320,10 +314,10 @@ ISR(TIMER2_OVF_vect)
320
314
321
315
if (irparams.blinkflag ) {
322
316
if (irdata == MARK) {
323
- PORTB |= B00100000 ; // turn pin 13 LED on
317
+ BLINKLED_ON () ; // turn pin 13 LED on
324
318
}
325
319
else {
326
- PORTB &= B11011111 ; // turn pin 13 LED off
320
+ BLINKLED_OFF () ; // turn pin 13 LED off
327
321
}
328
322
}
329
323
}
0 commit comments