Saturday, January 23, 2010

Experiment No. 10: Use of UART Library to Communicate with PC

MikroC has two sets of built-in library functions for UART communications: Software UART and Hardware UART. Since PIC16F628A has a built-in hardware USART module, we are going to use the Hardware UART library. Some PICs don't have hardware USART, such as PIC16F84A. In such cases, any digital I/O pins of PIC can be used for Asynchronous Serial Data Transfer using mikroC Software UART libraries.

Experimental Setup

The UART Rx and Tx pins in PIC16F628A are multiplexed with RB1 and RB2 pins. In this experiment, we are just sending some character data from PIC to a PC as demonstration of the technique. On PC, the HyperTerminal program should be running to receive data from the PIC16F628A. Since our PIC board does not have a TTL to RS232 voltage level shifter, we are going to construct it on a breadboard. Here is my Level Shifter Circuit:

Connect Tx on PIC side to RB2 pin and leave Rx open, as we are not receiving any data from PC. And,  the same circuit on breadboard is shown below. I am using BC557 transistor. The diode in the circuit is any general purpose diode.

BC557 Transistor Pins Relevant Links

I have one COM Port on my PC to which I have connected a RS232 DB9 female connector. We need to make a common ground between RS232 and our experiment board. So connect pin 5 (which is ground) of  DB9 connector to our circuit ground. 

Male DB9 Connector Pin descriptions. (Source: )

The same signal pins for a female DB9 connector. See, how the pins are flipped around horizontal.

Complete Setup for this Experiment

We are going to set 9600 baud rate for data transfer, so next step is to setup the Hyperterminal on PC. I am running this on Windows XP.
  1. Go to Start-> All Programs -> Accessories -> Communications -> Hyper Terminal
  2. Setup a New Connection to a proper COM port number with
      • bps : 9600
      • Databits: 8
      • Parity : None
      • Stop Bits : 1
      • Flow Control : Hardware
Click OK and now the HyperTerminal is ready to receive data from PIC.

As mentioned earlier, mikroC has in-built library functions for hardware UART. Check out the manual here.

 * Project name:
     Asynchronous Serial Data Transfer from PIC16F628A to a PC HyperTerminal
 * Copyright:
     (c) Rajendra Bhatt, 2009.


void main() {
CMCON = 7;       // Disable Comparators
UART1_Init(9600);     // Baud Rate 9600
while(1) {
UART1_Write_Text(" UART Test Successful! ");   // Character Message to be Sent
UART1_Write(10); // Line Feed
UART1_Write(13); // Carriage Return
delay_ms(2000);     // Send the message every 2 seconds

Experimental Output


  1. If i want to used from pc to pic16F628A. How do I do that?

  2. To read data from PC, you can use UART1_Read or UART1_Read_Text functions in mikroC. Meanwhile, you have to write an application software that runs in the PC to send the character data from PC to PIC.

  3. do you know how i can do the same job by using h-tech c compiler, i mean what is the functions?

  4. Hello,
    what configuration registers are you using?

  5. Can someone please help me, i am new on c programming and i wanna program three running LED using pi12f683a.


Microcontroller-based Embedded Systems Design