Skip to main content

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:http://tk5ep.free.fr/tech/ts850/if232/img/db9_pinouts.gif )

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.

Software
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
Delay_ms(100);
while(1) {
//UART1_Write(_data);
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

Comments

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

    ReplyDelete
  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.

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

    ReplyDelete
  4. Hello,
    what configuration registers are you using?

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

    ReplyDelete

Post a Comment

Popular posts from this blog

Contact less tachometer using PIC16F628A

Introduction
Tachometer is a device that gives you the information about the rotational speed of any shaft or disc. It usually measures the speed in revolutions per minute (RPM). Today we are going to make a simple tachometer that could measure the rotation speed of a disk without making any physical contact (that's why it is contact less) with the rotating object. The range of this tachometer is 0 - 9999 RPM and displays the RPM on a multiplexed 4-digit seven-segment display. Of course, we are going to do this project on our usual PIC16F628A development board.

Infrared sensor
Contact-less measurement of RPM will be achieved through an IR sensor. An IR diode will send a beam of infrared towards the rotating disc, and any reflected pulse will be received by a photo diode. The resistance of a photo diode drops drastically when exposed to infrared. An infrared is reflected by a white surface and absorbed by the dark ones. The test disc for this project is shown below. You can see the …

Experiment No. 2 : Push Button and Seven Segment Display Interface

In this experiment, we will program the PIC16F628A as an UP/DOWN Decade Counter. The count value will be displayed on a Seven-Segment Display and will be incremented/decremented by two push buttons on the board.

Experimental Setup:
The board has built in interface for a multiplexed 4-digit seven segment display (HS-5461AS2 from www.futurlec.com).We will select only one digit by connecting a Digit Select pin to Vcc, as shown in figure below. A black jumper wire is used for this purpose. The seven segments will be driven through PORTB (already wired on the board). Connect Push Buttons (PB3 and PB4) to RA1 and RA0 female headers using jumper wires.


PIC16F628A Development Board

The development board we are going to make for our experimental microcontroller PIC16F628A will look like this. Here are the features it is going to have:
Access to all I/O pins through female header pins4 Push Buttons for Input4 LEDs for OutputAn LCD Interface PortA 4-digit Seven-Segment Display InterfaceLCD Backlight Switch and Contrast AdjustmentICSP Programming (Very Important)