USART Communication use PIC Microcontroller
Welcome to back . In this tutorial we will learn how to UART communication using PIC Microcontroller. Here we are use PIC16F877A Microcontroller and PICkit 2 for control the logic lavel. The PICkit 2 also provide a Universal Asynchronous Receiver Transmitter ( UART ) Tool and Serial Monitor for showing the data.
In Synchronous Transmission, data is sent in form of blocks or frames. This transmission is the full duplex type. Between sender and receiver the synchronization is compulsory. In Synchronous transmission, There is no gap present between data. It is more efficient and more reliable than asynchronous transmission to transfer the large amount of data.
In Asynchronous Transmission, data is sent in form of byte or character. This transmission is the half duplex type transmission. In this transmission start bits and stop bits are added with data. It does not require synchronization.
You can watch the following video or read the written tutorial below.
We control any microcontroller and electronics equipment by sending command form personal computer. In this tutorial we will control the three leds by send command form our personal computer. Here, personal computer send a command and the micrcontroller read the commend that control the leds.
We can easily control the green, red and yellow leds by sending command 1, 2 and 3.
Send command 1 the green led is active and it emit the light source.
Again we send command 1 the green led is deactivate. Send command 2 the red led is active. Again send command 2 the red led is deactivate. As well as in 3rd case we send command 3 the yellow led is active and resend command 3 which deactivate the yellow led.
Now, Lets see the device data sheet.
The Universal Synchronous Asynchronous Receiver Transmitter ( USART ) module is one of the two serial I/O modules.
USART is also known as a Serial Communication Interface or SCI. The USART can be configured as a full duplex asynchronous system that can configured as a half duplex synchronous system that can communicate with peripheral devices such as CRT terminals and personal computer, or it can be configured as a half duplex synchronous system that can communicate with peripheral device such as A/D or D/A integrated circuits, serial EEPROMs etc.
The USART can be configured in the following modes:
- Asynchronous ( Full duplex )
- Synchronous - Master ( Half duplex )
- Synchronous - Slave ( Half duplex )
In Synchronous Transmission, data is sent in form of blocks or frames. This transmission is the full duplex type. Between sender and receiver the synchronization is compulsory. In Synchronous transmission, There is no gap present between data. It is more efficient and more reliable than asynchronous transmission to transfer the large amount of data.
In Asynchronous Transmission, data is sent in form of byte or character. This transmission is the half duplex type transmission. In this transmission start bits and stop bits are added with data. It does not require synchronization.
Data can be Transmission Three ways:
- Simplex
- Half duplex
- Full duplex
Simplex:
Half duplex:
Full duplex:
The PICkit 2 Programmer application versions 2.40 and later include the UART Tool feature. This feature allows PICkit 2 to be used as a serial Universal Asynchronous Receiver and Transmitter ( UART ) terminal interface for communicating with a PIC microcontroller.
Potential uses include:
- Displaying debug text output from the microcontroller
- Logging microcontroller data to a text file
- Developing and debugging a microcontroller UART interface
- Interfacing with and sending commands to the microcontroller during development
The tool supports full duplex asynchronous serial communications from 150 to 38400 baud, including custom non-standard baud rates. The PICkit 2 connects via the ICSP connector directly to microcontroller TX and RX pins at logic levels. No transceiver is needed.
CONNECTING THE PICkit 2 UART TOOL
The PICkit 2 connects to the target serial port target as shown in Figure . The PICkit 2 TX signal (out) should connect to the target RX signal (in), and the PICkit 2 RX signal (in) should connect to the target TX signal (out). When being used with the UART Tool, the PICkit 2 may not be able to supply VDD volt- age to the target depending on the application version. See the version Release Notes in the PICkit 2 Readme file. However, even when PICkit 2 is not supplying the target VDD, the PICkit 2 VDD pin must be connected to the target VDD voltage or it will not be able to communicate. The PICkit 2 UART Tool will work with target VDD voltages from 2.5 Volts to 5.0 Volts.
PICkit 2 User Guide
The format of the serial signals on pins 4 and 5 is inverted such that a Start Bit = logic low (VSS), and a Stop Bit = logic high (VDD). The serial data format is always 8 data bits, no parity, with one stop bit. The BUSY LED on the PICkit 2 unit acts as an activity light for the serial connection.
THE PICKIT 2 UART TOOL WINDOW
To open the PICkit 2 UART Tool window, select Tools>UART Tool. The PICkit 2 Programmer window will open and the PICkit 2 Programmer application window will close, as the programmer cannot be used while the UART Tool is active, and vice versa.
The UART Tool has two modes: ASCII and HEX. The current mode is selected and displayed by the buttons on the upper right hand of the display. The button corresponding to the active mode will be displayed depressed. For example, in Figure the ASCII mode is active and so the ASCII button appears pressed. The mode selection affects how serial data is displayed and entered in the window, and the modes are explained in the next two sections.
Here's the circuit schematics. Simply we connect Vdd and ground pin with 2 and 3 pin of pickit2. pin 4 and 5 of pickit 2 is connect tx and rx pin of microcontroller.
Source code:
char uart_rd;
void main(){
UART1_Init(9600); // UART1 Initialize with 9600kbp TRISB= 0X00; // PORTB in Output
PORTB= 0X00; // Clear the PORTB
UART1_Write_Text("Start"); // Start text on visiul monitor
while(1){
if(UART1_Data_Ready()){
uart_rd = UART1_Read();
if(uart_rd=='1'){
RB0_bit = ~RB0_bit; // Toggle the green led
}
if(uart_rd=='2'){
RB1_bit = ~RB1_bit; // Toggle the red led
}
if(uart_rd=='3'){
RB2_bit = ~RB2_bit; // Toggle the yellow led
}
delay_ms(500);
}
}
}
Source Code Explain:
We write the code in mikroC PRO for PIC compiler. The mikroC PRO for PIC UART Library
provides comfortable work with the Asynchronous (full duplex) mode. For more details UART Library
So first we need to include UART library by check the box. In declaration section of the program we declare a character variable uart_rd for store the UART receive byte .
In main section we need to initialize UART module at required baud rate. Refer to the device data sheet for baud rates allowed for specific Fosc. As well as we Clearing a TRISB bit (= 0) will make the corresponding PORTB pin an output. Then clear the PORTB register value.
In the main loop section we have create a if statement to Use a function to test if data in receive buffer is ready for reading. When we call the UART1_Data_Ready() function the function return a value. "1" if data is ready for reading and "0" if there is no data in the receive register. And then we read a byte simply call the UART1_Read() functions. We create a three if statement to compare the receive byte and toggle the Leds.
Click the download button for source code and proteus file.
Sir i m ur great fan....and i love ur project.
ReplyDelete