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:
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.
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.
- Go to Start-> All Programs -> Accessories -> Communications -> Hyper Terminal
- Setup a New Connection to a proper COM port number with
- bps : 9600
- Databits: 8
- Parity : None
- Stop Bits : 1
- Flow Control : Hardware
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.
*/
* 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
}
}
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
No comments:
Post a Comment