是否有一种干净的方法可以解除leonardo对USBCore的RX控制?

时间:2013-10-06 03:38:46

标签: c++ arduino

目前我正在尝试使用sparkfun promicro来控制RX引脚,使用此草图https://www.sparkfun.com/tutorials/338

然而,我遇到了一个问题,即尽管控制了RX和TX,它仍然受到arduino中USBCore.cpp的干扰。我想知道是否有一种干净的方法来禁用USBCore通过RX和TX引脚进行控制,同时仍然只保留USB串口,这样我就可以直接控制这些引脚,即使在接收和发送串行数据时也是如此。

/* Pro Micro Test Code
   by: Nathan Seidle
   modified by: Jim Lindblom
   SparkFun Electronics
   date: January 20, 2012
   license: Public Domain - please use this code however you'd like.
   It's provided as a learning tool.

   This code is provided to show how to control the SparkFun
   ProMicro's TX and RX LEDs within a sketch. It also serves
   to explain the difference between Serial.print() and
   Serial1.print().
*/
int RXLED = 17;  // The RX LED has a defined Arduino pin
// The TX LED was not so lucky, we'll need to use pre-defined
// macros (TXLED1, TXLED0) to control that.

void setup()
{
 pinMode(RXLED, OUTPUT);  // Set RX LED as an output
 // TX LED is set as an output behind the scenes

 Serial.begin(9600); //This pipes to the serial monitor
 Serial1.begin(9600); //This is the UART, pipes to sensors attached to board
}

void loop()
{
 Serial.println("Hello world");  // Print "Hello World" to the Serial Monitor
 Serial1.println("Hello!");  // Print "Hello!" over hardware UART

 digitalWrite(RXLED, HIGH);   // set the LED on
 TXLED1; //TX LED is not tied to a normally controlled pin
 delay(1000);              // wait for a second
 digitalWrite(RXLED, LOW);    // set the LED off
 TXLED0;
 delay(1000);              // wait for a second
}

如果没有修改arduino环境就无法彻底解决这个问题,那么我将修改USBCore.cpp。然而,这可能是不好的做法。

1 个答案:

答案 0 :(得分:1)

如果在你的场景中可能,你可以使用引脚17作为INPUT,希望释放另一个引脚,然后你可以将其用作输出。

为此,只需使用pinMode()将引脚17设置为INPUT。

这有效地禁用了RXLED功能。当USBCore向该引脚写入高电平时,它仅打开上拉电阻。只要驱动输入的设备即使在上拉开启时也能够吸收足够的电流,这将没有任何效果。因此无需修改USBCore。

编辑:当引脚17为低电平时,LED亮起,这意味着信号源需要吸收电流。如果通过切断LED旁边的PCB轨道或者通过拆焊LED或电阻器来解决这个问题,就可以避免这种情况。