C语言 Arduino 数字输入导致输出问题



我正在做一个简单的项目,涉及读取12位二进制编码器信号并将数字引脚高低循环(取决于编码器的角度位置(。

我的问题是,当数字输出引脚被告知变低时,它只会下降到大约4V。 我还注意到它偶尔会下降到地上,而不仅仅是4V。 令我惊讶的是,当我拔下Arduino时,数字输出引脚仍然读取4V。 当我关闭编码器的电源时,编码器通过 12 个数字输入引脚连接到 Arduino,输出引脚下降到 0V。 当我重新打开编码器时,它会回到大约 4V。

我尝试将输出引脚接地,显然电压变为 0,但一旦我断开接地连接,电压就会回升至 4V 左右。 似乎馈送到数字输入引脚的电压(以~5V数字输入信号的形式(阻止了数字输出引脚接地。 我不知道为什么会这样,我搜索了所有地方,找不到任何类似的投诉。 我的代码在下面提出,非常感谢任何帮助!

unsigned long CamAngle = 0;       // Variable to store encoder value
unsigned long PrevCamAngle = 0;   // Variable to store previous encoder value
int i = 0;                        // Cycle index
int BDC[] = {683,2048,3413};      // BDC angle (12-bit encoder value)
int TDC[] = {0,1365,2731};        // TDC angle (12-bit encoder value)
boolean TDCycle = false;          // TDC Cycle
boolean BDCycle = false;          // BDC Cycle
boolean Cycle = true;             // Encoder rollover cycle (replaces TDC cycle 0)
void setup()
{
  pinMode(37, INPUT);           // Encoder bit 0 (PORTC-0)
  pinMode(36, INPUT);           // Encoder bit 1 (PORTC-1)
  pinMode(35, INPUT);           // Encoder bit 2 (PORTC-2)
  pinMode(34, INPUT);           // Encoder bit 3 (PORTC-3)
  pinMode(22, INPUT);           // Encoder bit 4 (PORTA-0)
  pinMode(23, INPUT);           // Encoder bit 5 (PORTA-1)
  pinMode(24, INPUT);           // Encoder bit 6 (PORTA-2)
  pinMode(25, INPUT);           // Encoder bit 7 (PORTA-3)
  pinMode(26, INPUT);           // Encoder bit 8 (PORTA-4)
  pinMode(27, INPUT);           // Encoder bit 9 (PORTA-5)
  pinMode(28, INPUT);           // Encoder bit 10 (PORTA-6)
  pinMode(29, INPUT);           // Encoder bit 11 (PORTA-7)
  pinMode(30, OUTPUT);          // Set PORTC pin 8 to output (stop pin float)
  pinMode(31, OUTPUT);          // Set PORTC pin 7 to output (stop pin float)
  pinMode(32, OUTPUT);          // Set PORTC pin 6 to output (stop pin float)
  pinMode(33, OUTPUT);          // Set PORTC pin 5 to output (stop pin float)
  digitalWrite(30, LOW);        // Set PORTC pin 8 low (stop pin float)
  digitalWrite(31, LOW);        // Set PORTC pin 7 low (stop pin float)
  digitalWrite(32, LOW);        // Set PORTC pin 6 low (stop pin float)
  digitalWrite(33, LOW);        // Set PORTC pin 5 low (stop pin float)
  pinMode(53, OUTPUT);          // Set pin 53 (PORTB-0) as digital output
  digitalWrite(53, LOW);        // Set pin 53 (PORTB-0) LOW
}
void loop()
{
  // Cam Angle Update
  PrevCamAngle = CamAngle;      // Set variable to previous encoder value
  CamAngle = (PINA << 4) + PINC;    // Read encoder, set variable to value
  if (TDCycle == true && CamAngle >= TDC[i])  // Wait for encoder to reach angle if cycle active
  {
    PORTB = 0;                                  // Set digital pin 53 LOW
    TDCycle = false;
    BDCycle = true;
  }
  if (BDCycle == true && CamAngle >= BDC[i])  // Wait for encoder to reach angle if cycle active
  {
    PORTB = 1;                                  // Set digital pin 53 HIGH
    TDCycle = false;
    BDCycle = true;
    i++;
    if (i > 2)                                  // Reset every 3 cycles (3 cycles per revolution)
    {
      i = 0;
      BDCycle = false;
      Cycle = true;
    }
  }
  if (Cycle == true && CamAngle < (PrevCamAngle + 1))  // Wait for encoder to cycle back to 0
  {
    PORTB = 0;                                  // Set digital pin 53 LOW
    BDCycle = true;
    Cycle = false;
  }      
}

如您所见,我正在使用直接端口操作,但是当我使用 Arduino 库命令时,我也遇到了同样的问题。

事实证明,问题出在Arduino中的内部保护二极管上。 所有引脚均具有接地和 5V 电源轨的内部保护二极管。 因此,当电源断开时,5V 电源轨会降至地(或应该(,但是如果您有来自 IO 引脚的电源,电流将开始流经连接到 5V 电源轨的保护二极管并开始为您的 Arduino 供电。

这看起来是一个更适合 https://arduino.stackexchange.com/或 https://electronics.stackexchange.com/的问题。 但是,根据您提供的信息,我的第一个想法是,这与其说是编码问题,不如说是连接问题 - 特别是当您可以看到没有电源应用于Arduino的问题时。

查看编码器的数据表,确保未将输出连接到 Arduino 的输出。 可能还需要一个外部下拉电阻器。 最后,尝试使用其他引脚 - 我的 Arduino 引脚因超过当前额定值而死亡(哎呀!

希望对您有所帮助!

最新更新