PIC to PC RS232 Interface
This project demonstrates PIC microcontroller to PC Interface to control series of LEDs from a computer via the RS232 serial port.
PIC micro to PC Interface |
Hardware
I used the Universal Robot Controller Board and LED Test Board that have been built in previous projects for the hardwares (Please refer to the previous articles for the details). Microchip PIC16F872 will be used as the ‘brain’ of the controller board.
Hardwares |
Connection
Normally a single-chip RS232 level converter is used for the serial interface circuits. However, for this application I simplified the circuits by using an alternative method. A special serial cable has been designed for the task. It’s using two current limiting resistors – 22K for TX and 1K for RX.
RS232 connection cable |
RS232 pcb |
Connection Detail
Description | DB9 | PIC | Resistor |
---|---|---|---|
RX | Pin 2 | PORTC.0 | 1K |
TX | Pin 3 | PORTC.1 | 22K |
GND | Pin 5 | GND |
PIC Code
To make the PIC work with the PC, we first need to program the controller with some instructions. This can be easily achieved using SERIN2 and SEROUT2 commands in PICBASIC PRO. Demo version of PICBASIC PRO and the editor software MicroCode Studio can be downloaded from the Mecanique website.
The code below will wait for the computer’s signal from the serial port. Command should start with “TX” for validation, followed by the command type (0 = off or 1 = on) and the pin number (0 to 7).
'Mode = 16780 (2400 baud, no parity, inverted, driven)
N2400 CON 16780
B0 VAR BYTE
B1 VAR BYTE
RX VAR portc.0 'portc.0 is set to receive
TX VAR portc.1 'portc.1 is set to transmit
mainLoop:
'Wait for the command started with "TX" for about 600 ms
'If command is received, put the next characters into B0 and B1
SERIN2 RX, N2400, 600, mainLoop, [Wait("TX"), DEC1 B0, DEC1 B1]
'Jump to lo if B0 = 0 / hi if B0 = 1
BRANCH B0,[ledOff, ledOn]
ledOff:
'Turn off the led B1
HIGH B1
'Send the message out TX pin serially at 2400 baud
SEROUT2 TX, N2400, ["Light", DEC1 B1, " is off"]
GOSUB mainLoop
ledOn:
'Turn on the led B1
LOW B1
SEROUT2 TX, N2400, ["Light", DEC1 B1, " is on"]
GOSUB mainLoop
END
PC Command
On the computer end, I used the Serial Communicator that comes with the MicroCode Studio to interface. It can be launced from the MicroCode Studio editor by pressing F4. We need to configure the program i.e PortNumber, BaudRate and Parity to match the PIC configurations.
Now, let’s send a command to the PIC as shown below.
Serial communicator interface |
As the result, the LED that is attached to PORTB.0 lights up and the status is displayed on the Receive window of the Serial Comunicator.
Commands received - devices in action |
What’s Next?
This article has covered the basic of the PIC to PC RS232 interface, we have learned on how to send and receive serial data trough the serial port. From here, we can move further to interfacing with devices like servo, sensor etc. We can even build a smart home system that’s controlled by a computer.