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

发布于 2024-07-07 01:41:03 字数 234 浏览 7 评论 0原文

通过 C/C++ 程序制作带有 LED 闪光灯的小电路的最简单步骤是什么?

我希望所需的依赖项和包数量最少。

  • 我应该将某些东西连接到哪个端口?
  • 我会使用哪个编译器?
  • 如何将数据发送到该端口?
  • 我需要微处理器吗? 如果不是,我不想在这个简单的项目中使用它。

编辑:对任何操作系统特定的解决方案感兴趣。

What are the easiest steps to make a small circuit with an LED flash from a C/C++ program?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

司马昭之心 2024-07-14 01:41:03

这是使用并行端口。

不过我会推荐一个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
}

alt text

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

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:

/* 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
}

alt text

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

你是暖光i 2024-07-14 01:41:03

哪个端口? 并行端口是我最喜欢的选择,因为它输出 +5V(TTL 逻辑电平)并且编程非常简单。 大多数并行端口都有足够的功率来驱动 LED。 重要的是要记住,计算机端口通常设计为仅输出信号电压,而不是产生足够的电流来实际为大多数设备供电。

哪个编译器?没关系。 不过,这种硬件黑客在 Linux 下更有趣、更容易,所以 GCC 是一个不错的选择。

如何发送数据? 取决于端口和操作系统。 对于一个简单的项目来说,USB 非常复杂,所以忘记它吧。 串行和并行端口可以通过各种不同的接口进行控制。 我的偏好是使用 Linux 下的 ioctl() 系统调用来直接控制并行端口引脚。 以下是有关如何执行此操作的信息: http://www.linuxfocus.org/common /src/article205/ppdev.html

我需要微处理器吗? 不,您不需要外部设备中的微处理器(显然您的计算机有微处理器:-P)。 如果您使用并行或串行端口,则只需使用 LED 和一个或两个电阻以及必要的部件即可直接连接 LED。

(另外:《Linux 设备驱动程序》一书可在线免费获取,其中包含有关将简单电子设备连接到并行端口以及为其编写内核驱动程序的信息。)

编辑: 似乎在这个帖子中,OP 的含义非常混乱,“我需要微处理器吗?” 强调的是,基于计算机中的软件,单独的并行端口可以驱动LED。。 该设备中不需要微处理器。 但是,如果您希望设备能够在不连接到计算机的情况下进行自我控制,则需要微处理器或其他一些数字逻辑

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.

Which compiler? Doesn't matter. This kind of hardware hacking is more fun and easy under Linux, though, so GCC is a good choice.

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

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.

(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.)

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 is required.

半城柳色半声笛 2024-07-14 01:41:03

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

如果你想用软件来实现,正如Vlion提到的,一切都取决于所使用的硬件以及连接 LED 的电路设计。

如果您想尝试摆弄 PC 上的某些东西,这里有一篇关于如何使连接到 PC 并行端口上的引脚的 LED 闪烁的文章:

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:

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.

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:

窝囊感情。 2024-07-14 01:41:03

您可以尝试在串行端口传输(引脚 3)到接地(引脚 5)上放置一个 LED 和一个 300 欧姆电阻。 然后发送数据将其打开。

串行端口可能只能提供 10mA 的电流。

祝你好运。

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.

The serial port can probably only source 10mA.

Good luck.

余生一个溪 2024-07-14 01:41:03

对于快速和肮脏的操作,您有 2 个简单的选择:串行或并行端口。
串行端口更容易,但受到 LED 数量的限制。

要连接 LED,您需要一个正确性别的外壳连接器 (DB25/DB9)、LED 和一个电阻。 您必须自己查找电阻器的值。

串行端口具有受程序员控制的控制流信号。 将正确的位输出到 MCR 寄存器(打开串行端口后)非常简单。

并行端口有点困难,因为需要进行更多的握手,但通常与写入寄存器的原理相同。

您可能必须与操作系统作斗争才能获得端口的控制权。

使用 Tx 线有些复杂,因为输出的信号是写入发送寄存器的数据的串行比特流。 我会坚持使用 CTS 和 DSR 信号。

为了快速进行调试,我刚刚写入寄存器并观察调制解调器指示灯。

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.

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.

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).

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.

For quick-and-dirty debugging, I have just written to the registers and watched the modem lights.

蓝天 2024-07-14 01:41:03

它还取决于操作系统。 在 Linux 上,您可以将 LED 直接连接到并行端口(当然,使用适当的限流电阻),然后简单地使用 C 函数“outb()”来打开和关闭它。

在 Windows 上,情况要复杂得多,因为操作系统不允许用户应用程序直接与端口通信。

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.

On Windows, it's a lot more complicated because the OS doesn't let user applications talk to ports directly.

白云不回头 2024-07-14 01:41:03

最简单的端口是串行或并行。 一定要记住在 LED 上串联一个电阻,否则会烧坏它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文