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

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.

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

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.

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

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.

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.

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.

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.

Related

Data sheet for chip does not state how to communicate with it

So to start off I am definitely not a Computer Engineer, but I am trying to learn. I found a couple of (93C46CB3) chips along with some other insignifcant chips in a bag, thanks Dad! I studied the data sheet and I figured out which pins do what on the chip, but I have yet to figure out how to read and write to it. It says it's serial but it does not say what baud rate it is. Also, it does not say how fast I should be turning on and off the pins. Does it use PWM? If so, how fast? The data sheet is here
http://www.datasheetspdf.com/datasheet/93C46CB3.html
PG. 7 is where the chart is for reading and writing but it does not say how long those intervals are. "S" "D" and "Q" are all pins btw.
I am trying to read and write its content using an Arduino, and or a Raspberry Pi, whichever works I just need it to work. Thanks in advance!
tldr; How fast do I turn my pins on and off for this chip, and what is the baud rate on this if it has serial communication?
The manufacturer has application notes on the wiring and protocol for their 93 Series Microwire devices
http://ww1.microchip.com/downloads/en/AppNotes/01004a.pdf
http://ww1.microchip.com/downloads/en/AppNotes/01020B.pdf
http://ww1.microchip.com/downloads/en/AppNotes/01029A.pdf
http://ww1.microchip.com/downloads/en/AppNotes/00993a.pdf
and the source in C for PIC microcontrollers is in:
http://ww1.microchip.com/downloads/en/AppNotes/AN1004.zip
TLDR:
Supports SPI or Microwire protocols
The speed for your chip is stated in the datasheet to have a clock frequency of 3MHz but I would recommend 2MHz as that covers all chips in this series.
The most significant bit is sent first
Clock polarity is type 0 (positive)
Clock phase is type 0 (rising edge)
Arduino init example:
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
This will work with pin 2 connected to SCK, pin 3 connected to MOSI, and pin 4 connected to MISO.
Seems like your chip is actually a 93C46, the CB probably stands for some chip options.
datasheet 93C46 Microchip
This chip is manufactured by many manufacturer, so you may try to find out which manufacturer to get the particular manufacturer datasheet to be sure there is no differences, even there should not be.
If this is true, it's a serial EEPROM which is basically a non-volatile memory. This chip is just a simple memory you can write and read to, but does not contain anything else so you cannot "program" it.
This chip communicate using a SPI bus (Serial peripheral bus) which has one clock line, and two data lines. The chip is a slave, passive component and you need a microcontroller to communicate with it.
I suggest you do further reading on the SPI bus and then on microcontrollers, then you'll be able to write and read its memory.
Serial Peripheral Bus

Using GPIO input instead of TDO/DI on the FTDI 2232H Mini-Module

I’m using the FTDI 2232H Mini Module as a module for a system I’m working on. I’m attempting to read data serially from a device using the FTDI’s GPIOs instead of the TDO/DI pin (MPSSE mode). So, for example, my idea of it would be to perhaps connect a GPIO input to the device’s output, and at each SCLK that the FTDI sends, check whether the GPIO input is high or low. By doing this I would be able to store these highs or lows as 1s or 0s, respectively, into a buffer.
Is it possible to do this, or something similar with the FTDI?
Is there a way to keep count of SCLK pulses or rising/falling edges?
I'm using C++ on a Windows environment.
Thanks
What you are describing is using bit banging which FTDI chips support
including FT232H.
This is the technology I use for my device Nusbio and all the bit banging
computation is written in C#.
Here some source executing SPI bit banging to read data from an EEPROM.
EEPROM/SPI/EEPROM_25AAXXX_BASE.cs
Bit banging is good for chip like the FT232RL or FT231X.
But if you have an FT232H, you can do better.
To answer you question the key is know what is the communication
protocol used by the device your are trying to read data from.
If the protocol is SPI, I2C, JTAG or Serial Port then you should use the native
mode supported by the FT232H.
As mentioned it is called mpsse, Multi-Protocol Synchronous Serial Engine
to simplify synchronous serial protocol (USB to JTAG, I2C, SPI (MASTER) or bit-bang)
design.
If your device support some exoteric protocol with a CLOCK wire which you mentioned, you might have to write your own bit banging.
Is there a way to keep count of SCLK pulses or rising/falling edges?
The Windows PC moslt likely would considered the master so your program is in charge of clocking HIGH and LOW and keeping track of it.
I2C Bit banging Adafruit 16x9 LED Matrix with Nusbio
mpsse SPI with the FT232H and OLED 128x64 display

RPi_utils is not recognizing remote, how to change code?

I have installed a 433Mhz transmitter and receiver, wiringpi, 433Utils and RPi_utils on my raspberry.
I'm trying to control power sockets.
When I run RFSniffer the RF-code from my grey remote shows up and I can easily send it out again from the Pi to control the grey sockets. BUT I also have a white "Proof" remote and sockets, which do NOT show up, and thus, can't be controlled.
So the question.
What options do I have? Altering the code for different timings? Setting up my own script to send the signals? How do I even control the HIGH and LOW from a script and add timings?
I don't have much knowledge about python or C++ but I can learn!
I have already tried changing settings like pulselength and writing a different send.cpp. Tried sending in different forms, binary, decimal and tri-state.
Looked on many sites but have not found any details that can fix my issue.
I have connected the data output from the 433 receiver to my PCs line in and recorded the different pulses, and found the signal, but don't know what to do with it...
All help would be much appreciated!
You've asked quite a few questions here - and "how to change code" isn't a good question.
I can try to answer one of your questions - how to send HIGH and LOW via a script.
HIGH and LOW are basically +5V = HIGH, 0V = LOW, although there are small voltage thresholds that apply. The remote control sockets on a transmitter, usually a 25 pin SUB-D or similar connector, have ground and other pins, like POWER OFF, POWER ON, RAISE POWER, LOWER POWER.
Those those POWER pins are usually the ones that need a HIGH or LOW signal. Changing a signal between HIGH and LOW is by connecting that pin either to the GROUND pin or to a +5V pin or power source. For instance, you could probably put a pin HIGH by using 3 AA batteries (4.5V) and connecting + to the POWER pin, and - to the GROUND pin. Or if you have a +5V PIN on the transmitter, connecting that to the POWER pin should raise it to HIGH. That will cause, if say you connected to POWER OFF, the transmitter to turn off.
Using a computer to control those signals requires wiring some sort of output of the computer to a switching mechanism. Most computers come with a serial port, but the power output of a serial connector might be too low, or too brief, to work.
Using a remote power controller, turning a 5V power supply on and off, and connected to the computer via its RS232 port, will allow you to output signals to turn on/off a power source connected to your transmitter pins.
You could also just go buy a SINE Systems, Broadcast Tools or other remote provider, and code to its protocol. Connect the RS232 port to the computer, and you can easily script commands to it. Linux RS232 ports are on /dev/something, e.g.
echo "command" > /dev/ttySMX0

Serial Port Counter

I want to connect a simple switch to my computer using a serial port. Whenever the switch gets closed, I want to increment a variable. I am using Visual C++ for the project.
Can anyone show me an approach for this task? I used Google and found examples for reading and writing data over via serial interface but I don't know how to implement a counter.
I don't think this works on a normal serial port, as the serial port in general uses a pulses (several pulses may be the same level for consecutive ones or zeros, but there will at least be pulses for the start/end of a sequence, and typically in the middle). There is a set number of bits in a message, typically 1 start, 8 data and 1 stop bit.
You could do this with a parallel port, or a GPIO pin, if there is such a thing on the system you are working on.
Or you have to implement some more logic than a simple switch, such that the thing sends a sequence of pulses to make up a complete packet, and have a message for "close" and one for "open".
One can connect DTR pin of the serial port to pins like DSR or RI via a switch or wire. It's easy to controll the serial port with the serial port class of .NET framework. Code samples can be found on msdn serial port class entry.

controling individual pins on a serial port

I know that serial ports work by sending a single stream of bits in serial. I can write programs to send and receive data from that one pin.
However, there are a lot more other pins on the serial port connection that normal aren't used but from documentation all seem to have some sort of function for signalling as opposed to data transfer.
Is it possible in any way to cause the other pins that are not used for direct data transfer to be controlled individually? If so, how would i go about doing that?
EDIT: more information
I am working with a modern CPU running windows 7 64-bit on an intel core i7 870 processor. I'm using serial to usb ports because its imposable for me to do anything directly with a usb port and my computer does not come with serial ports and also for some inexplicable reason i have a bunch of these usb to serial port adapters lying around.
My goal is to control mutipul stepper motors (200 steps per rotation, 4 phase motors). My simple circuitry accepts single high pulses and interprets it as a command to cause the motor to rotate one step. The circuit itself will handle the power supply and phase switching. I wish to use the data transfer pin to send the rotation signals (we can control position and velocity by altering the number of high pulses and frequency of high pulses through the pin, however there is no real pulse width modulation).
I have many motors to control but they do not need to be controlled simultaneously. I hope to use the rest of the pins and run them through a simple combination logic circuit to identify which motor is being moved and which direction it is to move in. This is part of the power switching circuitry.
The data transfer pin will operate normally at some low end frequency. However, i want to control the other pins to allow me to give a solid on or off signal (they wont be flipping very quickly, only changes when i switch to controlling another motor).
Based of the suggestion of Hans Passant , I'd like to suggest that you use an Arduino instead of an USB-to-serial converter. The "Duemilanove" is an Arduino-based board that provides 6 PWM outputs (as well as 8 other digitial I/Os and 6 analog). Some more specialized boards might be even cheaper (Arduino Pro Mini, $15 in volume, some soldering required).
Using the handshaking pins to send data can work very well, though probably not on a multitasking OS, it's just very processor intensive (because the port needs to be polled constantly) and requires some custom cables. In fact, back in the day, this is exactly how Laplink got such high transfer rates over serial connections (and why to get those rates you needed a special 'Laplink' cable). And you need both sides of hte link to be aware of what's going on and be able to deal with the custom communications. Laplink would send a packet of data over both the normal UART pins while trying to send data from the other end of the packet over the handshaking pins. If the correct cable wasn't used (or there was some other problem with sending over the handshaking pins) there was no problem - all the data would just get send normally.
Embedded developers might know this as 'bit banging' - often on small embedded systems there's no dedicated UART circuitry - to get serial communications to work they have to toggle a general I/O pin with the correct timing. The same can be done on a UART's handshaking pins. But like I said, it can be detrimental to the system if other work needs to be done.
You can use DTR and RTS only, but that is four possible states. You do need to be careful that the device on the other end uses TTL levels. At he end of this link Serial there are tips on hardware if you need it.
What kind of data rate are you thinking of when you say high frequency? What kind of serial port do you have? With the old 9 pin connectors on the back of the computer the best you can do is around 115Kbps. With a USB adapter I have done test where I could push close to 1Mbps through the port.
Here's an article from Microsoft that goes into great detail on how to work with serial ports:
http://msdn.microsoft.com/en-us/library/ms810467.aspx
It mentions EscapeCommFunction for directly controlling the DTR line.
Before you check out this information, I'm joining in with the others that say a serial port is inappropriate for your application.
I´ve been trying to find an answer to your question for 3 hours, seems like there is no "simple way" to get a simple boolean signal from a computer...
But, there is always a way, and jet, as simple (maybe even stupid) as this may sound, have you considered using the audio jack connector as an output?, It is stereo so you would have 2 outputs available,the programming would is not that difficult. and you don#t need to buy expensive shit to make it work.
If you also need an input, just disassemble a mouse... and bridge the sensors to the servos, probably the most cheap and easiest way of doing it...
Another way would be using the leds for the Num-lock, caps-lock and the dspl-lock on the keyboard, these can be activated using software, and you just need to take a cheap external keyboard, and use the connectors for these 3 leds.
you are describing maybe a parallel port - where you can set bit patterns all at once - then toggle the xmit line to send it all...
Lets take a look from the "bottom up" point of view:
The serial port pins
Pins on the serial port may be connected to a "controller" or directly connected to the processor. In order for the processor to have access (control) the pins, there must be an electrical connection from the pins to the processor. If not, the processor nor the program can control the pins.
Using a serial controller
A controller, such as a USART, would be connected between the serial port and the processor. The controller may function as to convert 8 parallel data bits into serial bitstream. In the big picture, the controller must provide access to the port pins in order for them to be controlled. If it doesn't, the pins can't be accessed. The controller must be connected to the processor in order to control the pins if a controller is connected.
The Processor and the Serial port
Assuming that the pins you want to control are connected to the processor, the processor must be able to access them. Sometimes they are mapped as physical addresses (such as with an ARM processor), or they may be connected to a port (such as the intel 8086). A program would access the pins via a pointer or using a i/o instruction. In some processor, the i/o ports must be enabled and initialized before they can be used.
Support from the OS
Here's a big ticket item: If your platform has an Operating System, the Operating System must provide services to access the pins of the serial port. The services could be a driver or an API function call. If the OS doesn't provide services, you can't access the serial port pins.
Permission from the OS
Assuming the OS has support for the serial port, your program must now have permission to access the port. In some operating systems, permission may only be granted to root or drivers and not users. If your account does not have permission to access the pins, you are not going to read them.
Support from the Programming Language
Lastly, the programming language must have support for the port. If the language doesn't provide support for the port you may have to change languages, or even program in assembly.
Accessing the "unused" pins of a serial port require extensive research into the platform. Not all platforms have serial ports. Serial port access is platform dependent and may change across different platforms.
Ask another, more detailed question and you will get more detailed answers. Please provide the kind of platform and OS that you are using.