【问题标题】:Send over infrared on Arduino在 Arduino 上通过红外线发送
【发布时间】:2011-12-21 14:19:19
【问题描述】:

我需要使用 Arduino 传输红外信号来运行三星电视。

我尝试了以下代码:

// Lucas Eckels
// Http://lucaseckels.com

// IR remote control emitter for NEC protocol remote, as described at
// Http://www.sbprojects.com/knowledge/ir/nec.htm
// Tested on a Samsung LCD TV.

#include <util/delay.h>

#define IR_PIN 13

// With CONTINOUS defined, the first command is repeated continuously until
// You reset the Arduino. Otherwise, it sends the code once, then waits for
// Another command.
#define CONTINUOUS

// Times are in microseconds
#define ON_START_TIME 4500
#define OFF_START_TIME 4500
#define ON_TIME 580
#define OFF_TIME_ONE 1670
#define OFF_TIME_ZERO 540

#define DEVICE_1 7
#define DEVICE_2 7

void setup() {
  pinMode (IR_PIN, OUTPUT);
  digitalWrite(IR_PIN, LOW);
  Serial.begin(9600);
  delay(1000);
  Serial.write("Starting up..\n");
}

byte command = 0;
int commandCount = 0;
bool commandReady = false;

void loop() {
  if (commandReady) {
    Serial.print("Writing command");
    Serial.print(command, DEC);
    Serial.print("\n");

    writeStart();
    // Writing device code
    writeByte(DEVICE_1);
    writeByte(DEVICE_2);

    // Writing command code
    writeByte(command);
    writeByte(~command);
    writeEnd();
    delay(100);

#ifndef CONTINUOUS
    commandReady = false;
    command = 0;
    commandCount = 0;
#endif
    return;
  }

  if (Serial.available () > 0) {
    // Read in a 3-digit decimal command code.
    byte incoming = Serial.read();
    if (incoming <= '9 ' || incoming >= '0') {
      command *= 10;
      command += incoming - '0 ';
      ++commandCount;
    }
    if (commandCount == 3) {
      commandReady = true;
    }
  }
}

void writeStart() {
  modulate(ON_START_TIME);
  delayMicroseconds(OFF_START_TIME);
}

void writeEnd() {
  modulate(ON_TIME);
}

void writeByte(byte val) {
  // Starting with the LSB, write out the
  for (int i = 0x01; i & 0xFF; i <<= 1) {
    modulate(ON_TIME);
    if (val & i) {
      delayMicroseconds (OFF_TIME_ONE);
    } else {
      delayMicroseconds (OFF_TIME_ZERO);
    }
  }
}

void modulate(int time) {
  int count = time / 26;
  byte portb = PORTB;
  byte portbHigh = portb | 0x20; // Pin 13 is controlled by 0x20 on PORTB.
  byte portbLow = portb & ~0x20;
  for (int i = 0; i <= count; i++) {
    // The ideal version of this loop would be:
    // DigitalWrite(IR_PIN, HIGH);
    // DelayMicroseconds(13);
    // DigitalWrite(IR_PIN, LOW);
    // DelayMicroseconds(13);
    // But I had a hard time getting the timing to work right. This approach was found
    // Through experimentation.
    PORTB = portbHigh;
    _delay_loop_1(64);
    PORTB = portbLow;
    _delay_loop_1(64);
  }
  PORTB = portb;
}

代码可以编译,但对我不起作用。

【问题讨论】:

  • “i++”在我看来很可疑——也许“i++”会更好用?
  • 您的代码中有大量错误,因此我对其进行了编辑和修复,至少使其能够正确编译。并不是说我添加了任何实际上可以使其按预期工作的东西。请参阅我的答案。

标签: arduino infrared


【解决方案1】:

我写这个是为了控制一台 LG 电视和索尼放大器。您只需将自己的原始代码保存到头文件中即可:

https://github.com/gotnull/SiriProxy-TV-Control/blob/master/arduino-remote/Remote/Remote.pde

// This procedure sends a 38KHz pulse to the IRledPin
// for a certain # of microseconds. We'll use this whenever we need to send codes
void pulseIR(long microsecs) {
  // we'll count down from the number of microseconds we are told to wait

  cli(); // this turns off any background interrupts

  while (microsecs > 0) {
    // 38 kHz is about 13 microseconds high and 13 microseconds low
   digitalWrite(IRledPin, HIGH); // this takes about 3 microseconds to happen
   delayMicroseconds(10); // hang out for 10 microseconds
   digitalWrite(IRledPin, LOW); // this also takes about 3 microseconds
   delayMicroseconds(10); // hang out for 10 microseconds

   // so 26 microseconds altogether
   microsecs -= 26;
  }

  sei(); // this turns them back on
}

我还建议阅读 Ladyada 的精彩教程:

Sensor tutorials - IR remote receiver/decoder tutorial

【讨论】:

  • @orenberkovich 这有帮助吗?
【解决方案2】:

DelayMicroseconds 相当准确,对于您的任务来说足够精确。但是,您远离 DigitalWrite 是正确的。与只需要一个时钟周期的直接端口分配(PORTB=...)相比,它需要大约 50 倍的时钟周期才能完成。您将只能以这种方式对 38MHz 脉冲进行计时。我不知道你的 _delay_loop_1 做了什么,但其他一切似乎都还好。 (除了“i ++”,但我猜这是一个剪切粘贴错字)

你检查过它真的亮了吗?手机或便宜的数码相机实际上会在屏幕上显示 IR。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2021-11-25
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多