使用C/C++程序制作带LED闪光灯的小电路有哪些最简单的步骤?
我更喜欢所需的最少数量的依赖项和包.
编辑:对任何特定于操作系统的解决方案感兴趣.
mwi*_*ams 17
这是一个使用并行端口进行操作的教程.
虽然我会推荐一款可以非常便宜地购买的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
}
Run Code Online (Sandbox Code Playgroud)

http://www.arduino.cc/en/Tutorial/BlinkingLED
哪个港口? 并行端口是我最喜欢的选择,因为它输出+ 5V(TTL逻辑电平)并且编程非常简单.大多数并行端口具有足够的功率来驱动LED.重要的是要记住,计算机端口通常设计为仅输出信号电压,而不是产生足够的电流来实际为大多数设备供电.
哪个编译器? 无所谓.然而,这种硬件黑客在Linux下更有趣和容易,因此GCC是一个不错的选择.
我如何发送数据? 取决于端口和操作系统.对于一个简单的项目来说,USB非常复杂,所以算了吧.串行和并行端口可以通过各种不同的接口进行控制.我的偏好是ioctl()在Linux下使用系统调用来直接控制并行端口引脚.以下是有关如何执行此操作的信息:http://www.linuxfocus.org/common/src/article205/ppdev.html
我需要微处理器吗? 不,您不需要外部设备中的微处理器(显然您的计算机有一个微处理器:-P).如果使用并行或串行端口,您可以直接使用LED和一个或两个电阻以及必要的部件来连接LED.
(另外:Linux设备驱动程序一书,可在线免费获取,有关于将简单电子设备连接到并行端口以及为它们编写内核驱动程序的信息.)
编辑:在这个线程中似乎存在大量的混淆,关于OP意味着什么,"我需要一个微处理器吗?" 重点是,仅并行端口可以基于计算机中的软件驱动LED .设备中不需要微处理器.但是,如果您希望设备能够在不连接计算机的情况下自行控制,则需要微处理器或其他数字逻辑.