Skip to content

Instantly share code, notes, and snippets.

@taqpan
Created August 19, 2012 10:11
Show Gist options
  • Save taqpan/3394110 to your computer and use it in GitHub Desktop.
Save taqpan/3394110 to your computer and use it in GitHub Desktop.
Raw IR-signal receiver for Arduino with PL-IRM-2161-C438
#define PIN_IR_IN 8
#define SIGNAL_TIMEOUT 8000
#define DATA_LEN 512
short buf[DATA_LEN];
void clearBuf(int range)
{
for (int i = 0; i < range; i++)
buf[i] = 0;
}
// see pulseIn() code. (hardware/arduino/cores/arduino/wiring_pulse.c)
unsigned long switchIn(uint8_t pin, uint8_t state, unsigned long timeout)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
uint8_t stateMask = (state ? bit : 0);
unsigned long width = 0;
unsigned long numloops = 0;
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
// wait for the signal to start
while ((*portInputRegister(port) & bit) != stateMask)
if (numloops++ == maxloops)
return 0;
// wait for the signal to stop
while ((*portInputRegister(port) & bit) == stateMask) {
if (numloops++ == maxloops)
return 0;
width++;
}
return clockCyclesToMicroseconds(width * 21 + 16);
}
int receiveIR()
{
// IR High Signal -> Pin is LOW (0)
// IR Low Signal -> Pin is HIGH (1)
// wait signal
while (digitalRead(PIN_IR_IN) != 0);
int i = 0;
for (i = 0; i < DATA_LEN; i++) {
unsigned long t = switchIn(PIN_IR_IN, (i & 0x1) ? HIGH : LOW, SIGNAL_TIMEOUT);
if (!t) break;
buf[i] = t;
}
Serial.print("Count=");
Serial.println(i);
for (int j = 0; j < i; j++) {
Serial.println(buf[j]);
}
Serial.write('\n');
clearBuf(i);
}
void setup()
{
Serial.begin(9600);
pinMode(PIN_IR_IN, INPUT);
clearBuf(DATA_LEN);
Serial.println("setup");
}
void loop()
{
Serial.println("ready");
receiveIR();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment