RS232 (or UART) Communication between your PIC and your PC
In a previous post, we went through the different aspects of a UART and how to set it up and use it to communicate either between PICs or other peripherals. In this post, I’d like to address the specifics of communicating between your PIC and your PC through Hyperterminal or PuTTY. It is a simple process but if you miss one thing, you’ll run into problems.
Setting Up the Hardware
First you need to realize that your PIC and your PC run at different voltage levels and you’ll need an RS-232 transceiver. The industry standard seems to be Texas Instruments’ MAX232 but you’ll find different manufacturers make pin to pin compatible ICs if you don’t like Texas Instruments. With the transceiver, your 0V to 5V PIC signal will change to a -15V to +15V PC voltage. In actuality, the voltage will be closer to ±9V, so don’t get too worried when you’re testing it. It should be noted that the MAX232 requires several external capacitors. Refer to the data sheet for more information.
It is important when wiring the MAX232 to make sure there is a common ground between the PIC, the MAX232, and the PC. You will need a female DB9 connector (a D-Sub) unless you want to randomly jab wires into the back of your PC (not recommended). You will need a minimum of three wires connected: Receive, Transmit, and Ground. The other pins have a function also but can be ignored (with the proper setup) until your projects become more complicated. You can learn the pinout by either going here or simply Googling it.
Setting Up the Software
Setting up Hyperterminal or PuTTY is almost the exact same. In Hyperterminal, you can choose your options as you set up a connection whereas PuTTY is setup under Connection->Serial. In your setup, you need to choose your COM port, most likely COM1 but depends on your computer. Set your speed or baud rate to whatever your PIC is running at, and leave your Data bits, Stop bits, and Parity at their default settings (8, 1, and none, respectively). However, the Flow control needs to be disabled, so set that to “None”. If you don’t disable that, the terminal will receive information from the PIC but won’t be able to send.
You’ll note that your terminals communicate in ASCII. If you tell your PIC to send a number, you’ll most likely get an odd symbol. If you’re using HI-TECH C, it has the stdio.h header file that can automatically change your number into its ASCII equivalent so you can actually understand it on the computer without a lookup table. I’ll give a brief description of how it works for an integer here.
Include the stdio.h file in your file. Create a char array with as many elements as digits you expect to display (if you think it’ll be <<999, create a 3 element array). We’ll call this char array my_number_in_ASCII. The number, be it a measurement or whatnot, we’ll call my_number. You then use sprintf like this:
sprintf(my_number_in_ASCII,”%d”,my_number);
Now, to transmit the number, my code would be:
for(i = 0; i < 3; i++)
transmit(my_number_in_ASCII[i]);
And it should show up perfectly in your terminal.
Now, this tutorial isn’t as exhaustive as it could be, so if there are any questions, feel free to post a comment or send me an e-mail and I’ll hopefully be able to help you out.
Comments
Comment from Joshua
Time June 12, 2009 at 8:53 pm
Important addition to this post: If, for some reason, the transmission from your PIC works but you can’t receive, make sure that you’re not sharing your receive pin with something else, specifically an ADC pin. I switched from the 16F877a to the 16F690 and couldn’t even get RCIF to set. After much scrambling about and finally Googling, I found that I hadn’t cleared ANS11 to disable the analogue circuitry.
Hope this tip saves you some time.
Comment from Sariah
Time December 11, 2008 at 11:46 pm
Hi,
By the way, I left a note to your comment on my blog. See you next week.
I see you haven’t jumped on my idea to add eye candy to your blog. C’mon, you could be the first guy who likes to write about Hyperterminals and baud rates, to make a BEAUTIFUL blog! Start out with a great photo cropped to be your header, then we can go from there.