Sie sind auf Seite 1von 4

PIC: Serial Communition in PIC Microcontroller- 1

Many people ask me how to program serial communication using PIC as if they are planning to
cover Mt. Everest and doesn’t from where to start. Well this post is dedicated to all those new
programmers who fear serial communication like they used to fear microcontrollers.

What comes first in your mind when you listen serial communication ? Yes, how to send data in
series. All this time you have been working single bits, turning on and off. Well serial
communication is nothing more than on and off, but the only difference is that it has certain rules
and timings which make it the reliable and accurate communication protocol among all.

I won’t go in much detail about the protocol but will focus more on implementation rather than
theory.

Hardware

The RS232 is a type of serial communication whose voltage levels range from -15v to +15v.
RS232 doesn’t deal with the software protocols. Its just the hardware specifications which are
followed in the serial port of PC. The main governing body is USART/UART. They deal with all
the baudrate,parity,stop/start bits, timings, encoding and all other software specifications of serial
communication.

Good news is that you really don’t have to worry about all this software stuff while setting up
communication. All you need is to build a very common voltage level translator. But hey what
the heck is that ?

Hold on. As I said at the start that PC uses RS232 which works on plus minus fifteen volts. And
our lovely PIC microcontroller features UART (or USART) which workd on zero to five volts
range.

So to translate 0-5 Volts communication into -15 to +15v communication, we need to have a
level translator such as MAX232.

All you need is to circuit it between microcontroller and PC and a connecting cable, and voila
you are done with hardware.
The circuit comprises of ;

1 uF Polarized Caps – Qty 4


10 uf Polarized Cap – Qty 1
MAX232 IC – Qty 1
DB9 Female Connector – Qty 1

Connect the components as shown above. I would recommend you to make a seperate board for
this so that you can use it in various projects.

In our application, the RX will be connected to PIN C6 of PIC and TX to PIN C7. To have
complete list of pin diagrams of all PIC Microcontrollers click here.

The complete schematic is as follows;


The power connections for MAX232 are not shown in above diagram.

Software

Now this is the most interesting part. Skip theory and get into real implementation ;

#use rs232 (baud=57600,rcv=PIN_C7, xmit=PIN_C6)

This line is used just after the oscillator clock declaration. Usually you have to give it Baudrate
and the pins used for communication. I recommend you to not to change PINS at beginner’s
level. The line is more explained at : PIC: PC Interfaced Digital Thermometer using PIC
Microcontroller

To send data to PC we will use the following statement;

printf(“Creative Electron – Your Electronics Resource”);

Yes, this is it ! This statement will send “Creative Electron – Your Electronics Resource” over
serial port to PC .

If you want to send value of an integer;

printf(“Value: %d”,var)

This is what sending data over serial is about.

The following code will increment an integer every second and send it on serial communication.
#include <16F876.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232 (baud=57600,rcv=PIN_C7, xmit=PIN_C6)

int count;
float temp;
float volt;

void main()
{

while(1){

//1 Sec Delay


delay_ms(1000);

//Increment Count
count ++;
if (count>60)
count=0;

//Send data to PC
printf(“\n\r Count Value: %d”,count);

You can use Hyper Terminal or any other serial monitoring software with the baudrate 57600.
The serial communication helps a lot in debugging. Use it to monitor your variables, conditions
and logic in run-time.

Das könnte Ihnen auch gefallen