You are on page 1of 20

Arab Academy for Science and Technology and Maritime Transport Faculty of Engineering Mechatronics Department

Serial communication using PIC Microcontroller

Assigned by: Dr. Salem Haggag Prepared by: Ahmed Saieed Ibrahim Abd El Samad

Introduction:The purpose of this report is to explain how to establish a serial communication using a PIC Microcontroller chip. I will try as much as possible to walk through the entire operation from the very beginning so that this report could help anyone to start to build his own serial communication circuit, add his own modification on it and use it in any application.

Firstly what is a microcontroller? A microcontroller or embedded controller is a silicon chip that is used in pretty much every device in our houses for example the television, kitchen machines, remote controls, etc. The difference between the microcontrollers and the microprocessors is that the microcontrollers include program memory, data memory, I/O ports .and some of this chip also may contain timers, counters and analog to digital convertor. On the other hand the microprocessors cant operate alone you have to build some circuits such as program memory and external clock. Microcontrollers can be programmed multiple times according to the application in which it is embedded in, you write a code using assembly or high level languages like Basic or C, then you could burn the code you have written using burners that can be connected to your computer. In the mini project that I am going to explain we will use the chip (PIC16f877A) which is a very popular chip and it contains all the features that I listed above so it would be a good choice for anyone to buy as his first microcontroller chip its pretty much available in any electronics shop and it would support anyone through the hole beginners and the intermediate phase from blinking LEDs to building a precise control system that can control a motor or a robot.

Secondly serial communication: It is the transmission of data bit after bit such that at any given time only one bit is transferred.

There are two types of serial communication:

Synchronous communication The transmitter and receiver are synchronized by the same clock frequency, whenever a transmission is received a respond is required in order to indicate that the data is transferred successfully or it needs to be transferred again. A good example for this type of communication is copying files from one point to another in order to prevent any data loss or the file will be corrupted.

Asynchronous communication The data are sent discretely without full synchronization between the transmitter and the receiver, the data is divided into frames of bytes which have a start bit at the beginning of the frame and stop bit at the end of the frame.
The baud rate is the number of times the signal can switch states in one second. Therefore, if the line is operating at 9600 baud, the line can switch states 9,600 times per second. This means each bit has the duration of 1/9600 of a second or about 100 sec.

Microcontroller serial communication:As I have explained before the microcontroller chips contain I/O ports and it can be programmed according to the application such that we can take an input process it and provide an output or provide an output only or make a closed loop control system whatever we wants, sometimes in the application we need to establish a communication either between two chips or between a chip and a computer the communication between two chips is very easy and can be done by a simple code and a connection between the two chips, it can be used for example for communications between two robots for collecting data. The serial communication between a chip and a computer can be used also for collecting data, processing it, plotting graphs, etc. The connection to the pc is through the RS-232 (Recommended standard-232) which is a standard interface approved by the Electronic Industries Association (EIA) for connecting serial devices. RS232 on DB9 (9-pin D-type connector)

There are other types of standard interfaces such as RS423, RS422, and RS485 which can be used for advanced applications.

Mini project
Now after explaining in brief the serial communication this is an application on serial communication between a computer and microcontroller (PIC16f877a) in order to demonstrate it practically. Objective To Design a circuit that can take an analogue input from 0v to 10v and transfer it to a digital number from 0 to 180 and send it serially to a computer. Procedures This mini project can be simply divided into three major steps:1- Receive the analogue input properly. 2- Process the input by writing a code and burning it on the pic16f877a. 3- Send the processed number serially to a computer. Requirements Here are the components needed for this project: lM7805. Switch. 2 x connector two pins. Diode. 11 x Resistance 300 Ohm. PIC 16f877A. 4 x Capacitor 10uf. Max232. 3 x NPN2N222. LED. Crystal oscillator 4MHZ.

2 x Capacitor 22pf. 3 x Resistance 1k Ohm. Rotary Potentiometer 5k ohm. Capacitor 1uf. Push bottom. DB9 female connector. 3 x 7segments.

Firstly: Receiving the analogue input


The chip PIC 16f877A can receive an analogue input because it has an analogue to digital convertor which can receive an input of 0 to 5v there is VREF. Pin in the PIC but it wont support our need maximum input of 10v which can damage the input pin, there is two solutions for this problem: 1- We can use two inverting OP.AMPs one with a gain of (-1) and the other with a gain of (-1/2). 2- We can use a voltage divider with two resistances with the same value. In this project we will use the second solution for simplicity.

This schematic show how the 10v input entering on a potentiometer to control the volt from 0 to 10v then the output of the potentiometer enters on the voltage divider so that the v-out became from 0 to 5v.

Secondly: Processing the input

The figure above is a figure of the PIC 16f877A the left side of the PIC has 8 pins that can be used a analog to digital converters we are only going to use one pin in our application. The PIC 16f877A has a10bit the analogue to digital convertors, according to the number of bits the accuracy of the ADC is defined the higher the number of bits the higher the accuracy. The ADC divide the incoming voltage into a certain number of divisions and it can be calculated depending on the number of bits Number of divisions= (2^ (number of bits))-1. For example the PIC 16f877A the number of divisions= (2^ (10))-1 =1023.

5v

1023

0v

As you can see in this figure that the relation is linear and we can convert it easily into a number from 0 to 180 easily by a simple linear equation also: The number = (the number of division) x 180/1023.

The following figure is the schematic diagram of our circuit.

This circuit consists of mainly four parts The first part is the analogue input which was explained in the first section. The second part is the serial communication which I will explain later. The third part is the power part to power the circuit. The fourth part is the three 7 segments part which is used to display the number it is not a major part in the circuit and we can eliminate it without any effect on the circuit.

This is a method that can be used in order to power multiple seven segments to display a certain statement and actually this method depends on deceiving our eyes by powering the 7 segments on and off at high frequency such that our eyes can see it stably. There are three transistors that power the 7 segments which are triggered by pin D2, D3and C4,7 pins from pin B0to B6 that determine the number to be displayed. This method can be implemented easily in our design because by a simple code we could display the number we need.

Thirdly: Sending data serially

Serial RS-232 communication works with voltages -15V to +15V for high and low. On the other hand, TTL logic operates between 0V and +5V. Modern low power consumption logic operates in the range of 0V and +3.3V or even lower. Thus the RS-232 signal levels are far too high TTL electronics, and the negative RS-232 voltage for high cant be handled at all by computer logic. To receive serial data from an RS-232 interface the voltage has to be reduced. Also the low and high voltage level has to be inverted. This level converter uses a Max232 and five capacitors. The max232 is quite cheap (less than 5 dollars) or if youre lucky you can get a free sample from Maxim. The MAX232 from Maxim was the first IC which in one package contains the necessary drivers and receivers to adapt the RS-232 signal voltage levels to TTL logic. It became popular, because it just needs one voltage (+5V or +3.3V) and generates the necessary RS-232 voltage levels.

Here is the PCB design for this circuit designed on Proteus Ares:

I have replaced the capacitors and diode and the push bottom with LEDs because there was no package for them in the Proteus Ares software in which I make my PCB designs on.

Here is the code that I have written using CCS compiler as everybody knows in programming there is a million ways to do the exact same thing so maybe someone would like to modify it or improve it as he or she wants. The (.h) file: #include <16F877A.h> #device adc=10

#FUSES NOWDT #FUSES HS #FUSES NOPUT #FUSES NOPROTECT #FUSES NODEBUG #FUSES NOBROWNOUT #FUSES NOLVP used for I/O #FUSES NOCPD #FUSES NOWRT

//No Watch Dog Timer //High speed Osc (> 4mhz) //No Power Up Timer //Code not protected from reading //No Debug mode for ICD //No brownout reset //No low voltage prgming, B3(PIC16) or B5(PIC18) //No EE protection //Program memory not write protected

#use delay(clock=4000000)

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)

The (.c) file: #include "C:\Users\Sa3don\Desktop\ccs\uart4.h" #include<math.h> void main() { int asign,n,z,y; int fdigit; int sdigit; int thdigit; float x; float input; int m; float corr; setup_adc_ports(AN0); setup_adc(ADC_CLOCK_INTERNAL); setup_psp(PSP_DISABLED); setup_spi(SPI_SS_DISABLED); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); // TODO: USER CODE!!

set_adc_channel(0); while(1){ output_high(pin_d0); input=read_adc(); //take analogue input. x=input*(0.17595307917888563049853372434018); //converting number (0-180). corr=x-0.5; if(corr>x){ x = ceil (x); } m=x; //approximation of the number

//the following part concerning the number into 3 parts to display it on 7 segments. fdigit=m%10; sdigit=(m/10)%10; thdigit=(m/100)%10; switch(fdigit){ case(0): asign=63 ; break; case(1): asign=6 ; break; case(2): asign=91; break; case(3): asign=79; break; case(4): asign=102; break; case(5):asign=109; break; case(6): asign=125; break;

case(7): asign=7 ; break; case(8): asign=127; break; case(9): asign=111; break; } switch(sdigit){ case(0): z=63 ; break; case(1): z=6 ; break; case(2): z=91; break; case(3): z=79; break; case(4): z=102; break; case(5):z=109; break; case(6): z=125; break; case(7): z=7 ; break; case(8): z=127; break; case(9): z=111; break; } switch(thdigit){ case(0): n=63 ; break; case(1): n=6 ; break; case(2): n=91; break; case(3): n=79; break; case(4): n=102; break; case(5):n=109; break;

case(6): n=125; break; case(7): n=7 ; break; case(8): n=127; break; case(9): n=111; break; }

//the following part power on and off the transistors as we discussed. if(y==0){ output_b(asign); output_high(pin_c4); delay_ms(6); output_low(pin_c4); y=1; }

if(y==1){

output_b(z); output_high(pin_d3); delay_ms(6); output_low(pin_d3); y=2; }

if(y==2){ output_b(n); output_high(pin_d2); delay_ms(6); output_low(pin_d2); } y=0; printf("Number= %u \n\r",m); // sending the number serially. output_low(pin_d0); } } The following figures shows the circuit after implementation and connecting it to the hyper terminal in windows XP you can find it in start>accessories>communication>Hyper terminal.

References:
Dogan Ibrahim,2006. Advanced PIC Microcontroller Projects in C: From USB to RTOS with the PIC 18F Series.
Milan Verle, 2008, PIC Microcontrollers, MikroElektronika. Followed by Available at:

http://www.mikroe.com/eng/products/view/11/book-pic-microcontrollers Independent inquiry about serial communication and max232: http://sodoityourself.com/max232-serial-level-converter/

You might also like