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

戏子无情 提交于 2019-12-20 10:01:52

问题


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.


回答1:


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
}

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




回答2:


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.




回答3:


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:

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

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:

  • http://www.codeproject.com/KB/cs/csppleds.aspx



回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/209603/steps-to-make-a-led-blink-from-a-c-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!