Using the COM port
In the previous post, we hooked up the DS1820 one wire temperature sensor to the PC’s LPT parallel printer port. In this post, we connect the DS1820 one wire temperature sensor to the PC’s COM serial communications port.
RS232 serial communication standard voltage levels are +12 to +15 volts, and -12 to -15 volts. So the first thing we need to do is to convert these voltage levels to standard TTL voltage levels, about 0 volts to + 5 volts. I am using MC1488 (line driver) and MC1489 (line receiver) to accomplish this simply because I already have these parts. If you don’t have these parts, then it would make more sense to buy MAX232 to make things simpler. It works with +5 volts and generates +12v and -12v internally.
The biggest problem with using the COM port is that we need +12v and -12v for MC1488, and +5v for MC1489. I used an old PC power supply to get these voltages. The large connector that plugs to the mother board provides all three voltages. To activate the PC power supply you need to short the green pin to any of the black pins on that connector as shown in the picture below. The single yellow wire at one corner provides +12v, the single light blue wire provides the -12v, and any of the red wires provide +5v.
PC power supply connector.
The 9 pin COM port in the back of the PC provides a number of input and output connections, but we really need only three: one input, one output, and the ground. Any of the output or input connections can be used, but to simplify things I chose pin 4 (DTR – Data Terminal Ready) for output, pin 1 (DCD – Carrier Detect) for input, and the ground pin 5. The test program given below works with these signals. If you pick other modem lines, then you have to make the corresponding changes in the test program. Note that we are not using the receive and transmit data lines.
PC COM interface schematic
Pull up resistor should be 4.7K, but I used 10K which worked fine. The isolating diode is a generic silicon diode. Here is the built prototype circuit.
The three wires at the top is coming from the COM port. The solid blue wire is the ground, the orange striped wire is the DTR line, and the blue striped wire is the DCD carrier detect line.
Timing micro seconds
The previous version of the test program had an ugly timing function that needed to be tuned. I came across a more reliable way to time micro seconds that is explained in THIS LINK. I tried it and it does work well. Each CPU read or write cycle to any port takes about 1 micro seconds. It is simple and effective.
It gets really confusing to deal with signal levels because they get inverted more than once. The following table shows the signal levels as it travels through the circuit.
|Clearing the Modem Bit||RS232 Line goes Low||TTL goes High||Value read via ioctl() is 0|
|Setting the Modem Bit||RS232 Line goes High||TTL goes Low||Value read via ioctl() is 1|
Changes to the test program
To make the test program work with the COM port, we use the ioctl() call to set or clear the modem line Data Terminal Ready (DTR bit).
On the read side, we use the ioctl() call with TIOCMGET command to read the status of the modem lines. The one we are interested in is the Carrier Detect line. If ‘and’ed with TIOCM_CAR constant, we find out if it was set or cleared.
To run the test program DOWNLOAD it, and compile it with gcc -O com1820.c -o com1820.
Using the COM port does not necessarily simplify things, but it does free up the parallel port for other uses.
What I would like to see next is to clean up the code a little bit so that the LPT or the COM port can be picked at compile time and a higher level interface like pyhon or perl can be used to read temperature values.