简体   繁体   English

从C / C ++程序中使LED闪烁的步骤?

[英]Steps to make a LED blink from a C/C++ program?

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. 编辑:对任何特定于操作系统的解决方案感兴趣。

Here's a tutorial on doing it with a parallel port . 这是一个使用并行端口进行操作的教程。

Though I would recommend an Arduino which 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
}

替代文字

http://www.arduino.cc/en/Tutorial/BlinkingLED http://www.arduino.cc/en/Tutorial/BlinkingLED

Which port? 哪个港口? Parallel port is my favorite choice since it outputs +5V (TTL logic level) and is very straightforward to program. 并行端口是我最喜欢的选择,因为它输出+ 5V(TTL逻辑电平)并且编程非常简单。 Most parallel ports have enough power to drive an LED. 大多数并行端口具有足够的功率来驱动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. 重要的是要记住,计算机端口通常设计为仅输出信号电压,而不是产生足够的电流来实际为大多数设备供电。

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. 对于一个简单的项目来说,USB非常复杂,所以算了吧。 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. 我的偏好是在Linux下使用ioctl()系统调用来直接控制并行端口引脚。 Here's info on how to do that: http://www.linuxfocus.org/common/src/article205/ppdev.html 以下是有关如何执行此操作的信息: 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). 不,您不需要外部设备中的微处理器(显然您的计算机有一个微处理器:-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. 如果使用并行或串行端口,您可以直接使用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?" 编辑:在这个线程中似乎存在大量的混淆,关于OP意味着什么,“我需要一个微处理器吗?” Emphatically, the parallel port alone can drive an LED based on the software in the computer . 重点是,仅并行端口可以基于计算机中的软件驱动LED。 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 is required. 但是,如果您希望设备能够在不连接计算机的情况下自行控制, 需要微处理器或其他一些数字逻辑。

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. 如果你想在没有微处理器的情况下使LED闪烁(这意味着没有C / C ++),使用555定时器IC的简单电路就可以实现。 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: 这些是初学者爱好者电子书或套件中的常见项目,因为它们非常简单,您可以在任何Radio Shack类型的地方获得零件:

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的文章:

You could try to put an LED and a 300 Ohm resistor on the serial port transmit (pin 3) to Ground (pin 5). 您可以尝试在串行端口上将LED和300欧姆电阻器(引脚3)接地(引脚5)。 Then send data to turn it on. 然后发送数据将其打开。

The serial port can probably only source 10mA. 串口可能只能提供10mA电流。

Good luck. 祝好运。

for quick and dirty operations, you have 2 easy options: serial or parallel port. 对于快速和脏操作,您有2个简单的选项:串行或并行端口。 The serial port is easier, but is limited in the number of LEDs. 串行端口更容易,但LED数量有限。

To connect the LEDs, you need a shell connector (DB25/DB9) of the correct sex, the LED's and a resistor. 要连接LED,您需要一个性能正确的外壳连接器(DB25 / DB9),LED和电阻器。 You would have to look up the value for your resistor yourself. 您必须自己查找电阻的值。

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. 使用Tx线有点复杂,因为输出的信号是写入发送寄存器的数据的串行比特流。 I would stick to the CTS and DSR signals. 我会坚持使用CTS和DSR信号。

For quick-and-dirty debugging, I have just written to the registers and watched the modem lights. 对于快速和脏调试,我刚刚写入寄存器并观看了调制解调器指示灯。

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上,它要复杂得多,因为操作系统不允许用户应用程序直接与端口通信。

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串联,否则你会把它烧掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM