PIC talks UART on RS-232
In this follow up of the PIC talk UART series we will be looking at how to use a new IC in combination with our PIC in order to talk to a PC. PCs have been equipped with an RS-232 for many many years before it was phased out in favour of other interfaces. Back then the serial port, the parallel port, the PS/2 and the joystick port where the interfaces you would use to connect the mouse, the printer, the keyboard and of course the joystick. Everything was then replaced by the USB, one standard to rule them all! Until the USB effectively took over the RS2-32 had been used to interface a plethora of devices including various types of instruments and other electronic gadgets. One of its uses comes from the mainframe and teletypewriter times. With DOS time it had even been used to connect two PCs together in a sort of one to one network in order to exchange data.
Wikipedia, as usual, will be able to tell you much more about the RS-232 (TIA-232-F) as will many other sites including Sparkfun with this nice article.
For our tutorial we will be using an USB to RS-232 in order to enable our PC to have its RS-232 back on board (be aware that Win8 might give you problems with some e.g. prolific). In terms of the terminal emulator you have many choices which will also depend on the OS that you are running. Here you can find some examples:
Hyperterminal (if your OS still has it, Win7/8 no longer package it)
Mikroelektronika’s USART terminal (available as a tool in the Mikro compilers)
many others through the GUI and depending on the Linux distro of choice
We will be using TeraTermPro on Windows and Gtkterm on Lubuntu) .
The RS-232 uses has different electric characteristics from the TTL signal provided by the PIC. The RS-232 compared to the TTL signal, will be inverted and range between -15V to +15V. Be aware though that, as reported on Wikipedia “Valid signals are either in the range of +3 to +15 volts or the range −3 to −15 volts with respect to the ground/common pin; consequently, the range between −3 to +3 volts is not a valid RS-232 level“. In order to be able to interface the PIC to an RS-232 of a PC, we will need an IC like the MAX232 whose role is to convert the TTL/CMOS signal into an RS-232 and vice-versa
While RS-232 once enjoyed ubiquitous use throughout the interface world, it has it’s limitations. Namely:
Due to a single signalling wire with a common ground the RS-232 has a limited cable length usually quoted to 15 meters (50 ft). This also allows for current loops when the devices are powered from separate sources.
There is no specification for multiple devices on one bus. This is usually not a problem but can sometime be troublesome in micros when there is usually only one, maybe two UARTs on the micro.
Handshaking is unreliably implemented, many if not most hobbyist projects do not use hardware handshaking.
Based on the above our circuit will look as the one in the picture below.
For me to be able to take the measurements available in the results I have used a DB25 breakout box so that I could create the necessary connections and also probe them with the oscilloscope. To do so I have also used a couple of DE9 to DB25. (It is DE9 not DB9, there is no such a thing as DB9!)
Note that I haven’t crossed pin 2 with pin 3 and vice versa on the DB25 of the breakout connector in respect to the DCE to DTE connection pinout which requires a straight cable. (The very same used between PCs and modems)
Of course I could have used a breakout box like the one in the picture below but it seemed a bit of an overkill for this tutorial!
With regards to the code nothing changed from the main article with the exception that we will only be using the TX part. Nothing would stop us from reverting roles and have the PC communicating with the PIC. We would then need to run the RX part of the code on the PIC but the result would essentially be the same.
In reviewing the results, we will only focus on the measurements from the oscilloscope and the output on the terminal emulation program. There is in fact no need to look at the logic analyser as all of that is unchanged from the main article.
What the oscilloscope shows on channel 1 is the “Hello, world!” in its TTL form. The signal is kept high at 5V when the communication is idle. This measurement can be taken off the bridge linking pin 25 on the PIC to pin 11 on the MAX232.
When moving our probe to pin 14 of the MAX232, the signal we would get is the one of channel 2 shown below which is in line with the IC specifications on the datasheet. In idle state the line is kept at -7.5V. This means that the LOW signal ranges between 0 and -7.5 volts indicating a logic 1. As soon as the message starts, the level will move to 7.5V indicating a HIGH signal which corresponds to a logic 0.
When viewing the two overlapped, it is rather evident that the logic levels are inverted.
Terminal emulator output
Sure enough on the side of the PC, the terminal will show our message in pretty much the same way the Bus Pirate did.
Not sure the results would have been universally right , we have tested things on a Lubuntu instalation. Note the name of the device that gets created when plugging in our USB to RS-232 adapter.
The RS-232 is a really nice way to have your PIC talking to a PC. It is particularly useful when debugging code and to connect to more and more rare devices that come with that interface. You will be surprised though of how many times you can still come across the DE9 connector!
Buying an USB to RS-232 adapter is a good investment especially considering that the majority should sell at less then £10/$16. On the TTL side of your circuit you can also buy ready made TTL to RS-232 module for £5/$8, or you can build your own as I did.
We hope you enjoyed reading this tutorial, more to come soon.