从 C/C++ 程序使 LED 闪烁的步骤?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/209603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Steps to make a LED blink from a C/C++ program?
提问by Brian R. Bondy
What are the easiest steps to make a small circuit with an LED flash from a C/C++ program?
从 C/C++ 程序制作带有 LED 闪光灯的小电路的最简单步骤是什么?
I would prefer the least number of dependencies and packages needed.
我更喜欢所需的最少数量的依赖项和包。
- What port would I connect something into?
- Which compiler would I use?
- How do I send data to that port?
- Do I need to have a micro-processor? If not I don't want to use one for this simple project.
- 我会将某些东西连接到哪个端口?
- 我会使用哪个编译器?
- 如何将数据发送到该端口?
- 我需要一个微处理器吗?如果不是,我不想在这个简单的项目中使用一个。
EDIT: Interested in any OS specific solutions.
编辑:对任何特定于操作系统的解决方案感兴趣。
回答by mwilliams
Here's a tutorial on doing it with a parallel port.
这是有关使用并行端口执行此操作的教程。
Though I would recommend an Arduinowhich can be purchased very cheaply and would only involve the following code:
虽然我会推荐一个可以非常便宜地购买的Arduino,并且只涉及以下代码:
/* Blinking LED
* ------------
*
* turns on and off a light emitting diode(LED) connected to a digital
* pin, in intervals of 2 seconds. Ideally we use pin 13 on the Arduino
* board because it has a resistor attached to it, needing only an LED
*
* Created 1 June 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
* based on an orginal by H. Barragan for the Wiring i/o board
*/
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
回答by Dan Lenski
Which port?Parallel port is my favorite choice since it outputs +5V (TTL logic level) and is very straightforward to program. Most parallel ports have enough power to drive an LED. It's important to remember that computer ports in general are designed to only output signaling voltages, and not to produce enough current to actually power most devices.
哪个港口?并口是我最喜欢的选择,因为它输出 +5V(TTL 逻辑电平)并且编程非常简单。大多数并行端口有足够的功率来驱动 LED。重要的是要记住,计算机端口通常设计为仅输出信号电压,而不是产生足够的电流来实际为大多数设备供电。
Which compiler?Doesn't matter. This kind of hardware hacking is more fun and easy under Linux, though, so GCC is a good choice.
哪个编译器?没关系。不过,这种硬件黑客在 Linux 下更有趣也更容易,所以 GCC 是一个不错的选择。
How do I send data?Depends on the port and the operating system. USB is frightfully complicated for a simple project, so forget it. Serial and parallel ports can be controlled via a variety of different interfaces. My preference is to use the ioctl()
system call under Linux to directly control the parallel-port pins. Here's info on how to do that: http://www.linuxfocus.org/common/src/article205/ppdev.html
如何发送数据?取决于端口和操作系统。USB 对于一个简单的项目来说是非常复杂的,所以忘记它吧。串行和并行端口可以通过各种不同的接口进行控制。我的偏好是ioctl()
在 Linux 下使用系统调用来直接控制并口引脚。以下是有关如何执行此操作的信息:http: //www.linuxfocus.org/common/src/article205/ppdev.html
Do I need a microprocessor?No, you don't need a microprocessor in the external device (obviously your computer has a microprocessor :-P). If you use the parallel or serial ports, you can just use the LED and a resistor or two and the necessary parts to connect the LED directly.
我需要微处理器吗?不,您不需要外部设备中的微处理器(显然您的计算机有一个微处理器:-P)。如果您使用并行或串行端口,您可以只使用 LED 和一两个电阻器以及必要的部件来直接连接 LED。
(Also: The Linux Device Drivers book, available for free online, has information on interfacing simple electronic devices to parallel ports and writing kernel drivers for them.)
(另外:Linux 设备驱动程序一书,可免费在线获取,提供有关将简单电子设备连接到并行端口以及为它们编写内核驱动程序的信息。)
EDIT:There seems to be massive confusion in this thread about what the OP means by, "Do I need a microprocessor?" Emphatically, the parallel port alone can drive an LED based on the software in the computer. No microprocessor is needed in the device. However, if you want the device to be able to control itself without being connected to the computer, a microprocessor or some other digital logic isrequired.
编辑:在这个线程中似乎对 OP 的含义有很大的困惑,“我需要一个微处理器吗?” 强调的是,仅并口就可以根据计算机中的软件驱动LED 。设备中不需要微处理器。但是,如果您希望设备能够在不连接计算机的情况下进行自我控制,则需要微处理器或其他一些数字逻辑。
回答by Michael Burr
If you want to blink an LED without a microprocessor (which implies no C/C++), a simple circuit using a 555 timer IC will do the trick. These are common projects in beginner hobbyist electronics books or kits because they're really simple and you can get the parts at any Radio Shack type of place:
如果你想在没有微处理器的情况下使 LED 闪烁(这意味着没有 C/C++),一个使用 555 定时器 IC 的简单电路就可以做到这一点。这些是初学者电子书或套件中的常见项目,因为它们非常简单,您可以在任何 Radio Shack 类型的地方获得零件:
- http://www.kpsec.freeuk.com/projects/flashl.htm
- http://www.electronics-project-design.com/LED-Flasher-Circuit.html
- http://www.kpsec.freeuk.com/projects/flashl.htm
- http://www.electronics-project-design.com/LED-Flasher-Circuit.html
If you want to do it in software, as Vlion mentions, everything depends on the hardware being used and the design of the circuit that hooks up the LED.
如果你想用软件来做,正如Vlion 提到的,一切都取决于所使用的硬件和连接 LED 的电路设计。
If you want to try and mess around with something on your PC, here's an article on how to blink LEDs that are hooked up to pins on the PC parallel port:
如果您想尝试弄乱 PC 上的某些东西,这里有一篇关于如何使连接到 PC 并行端口上的引脚的 LED 闪烁的文章:
回答by dar7yl
for quick and dirty operations, you have 2 easy options: serial or parallel port. The serial port is easier, but is limited in the number of LEDs.
对于快速和肮脏的操作,您有 2 个简单的选择:串行或并行端口。串行端口更容易,但受 LED 数量限制。
To connect the LEDs, you need a shell connector (DB25/DB9) of the correct sex, the LED's and a resistor. You would have to look up the value for your resistor yourself.
要连接 LED,您需要一个正确性别的外壳连接器 (DB25/DB9)、LED 和一个电阻器。您必须自己查找电阻器的值。
The serial port has control-flow signals which are under programmer control. It's a simple matter of outputting the correct bits to the MCR register (after opening the serial port).
串行端口具有受程序员控制的控制流信号。将正确的位输出到 MCR 寄存器是一件简单的事情(打开串行端口后)。
The parallel port is a little bit harder, in that there is a bit more handshaking to do, but is generally the same principle of writing to a register.
并口有点困难,因为需要做更多的握手,但通常与写入寄存器的原理相同。
You may have to fight your OS to gain control of the port.
您可能必须与操作系统抗争才能获得对端口的控制权。
Using the Tx line is somewhat complex, as the signal coming out is the serial bitstream of the data written to the transmit register. I would stick to the CTS and DSR signals.
使用 Tx 线有些复杂,因为输出的信号是写入发送寄存器的数据的串行比特流。我会坚持 CTS 和 DSR 信号。
For quick-and-dirty debugging, I have just written to the registers and watched the modem lights.
为了快速和肮脏的调试,我刚刚写入寄存器并观察调制解调器灯。
回答by Robert
You could try to put an LED and a 300 Ohm resistor on the serial port transmit (pin 3) to Ground (pin 5). Then send data to turn it on.
您可以尝试在串行端口传输(引脚 3)到地(引脚 5)上放置一个 LED 和一个 300 欧姆的电阻器。然后发送数据将其打开。
The serial port can probably only source 10mA.
串口可能只能提供 10mA 的电流。
Good luck.
祝你好运。
回答by Tanj
The easiest port to do this on would be serial or parallel. Always remember to put a resistor in series with the LED or you will burn it out.
最简单的端口是串行或并行。永远记住在 LED 上串联一个电阻,否则你会烧坏它。
回答by nobody
It also depends on the OS. On Linux, you could wire an LED directly to the parallel port (with an appropriate current-limiting resistor, of course) and simply use the C function "outb()" to turn it on and off.
这也取决于操作系统。在 Linux 上,您可以将 LED 直接连接到并行端口(当然,使用适当的限流电阻),然后只需使用 C 函数“outb()”来打开和关闭它。
On Windows, it's a lot more complicated because the OS doesn't let user applications talk to ports directly.
在 Windows 上,它要复杂得多,因为操作系统不允许用户应用程序直接与端口通信。