Академический Документы
Профессиональный Документы
Культура Документы
It is a serial communication interface which uses two lines for sending (TX) and receiving (RX) data. As its name indicates it is an asynchronous communication interface, which means it doesnt need to send clock along with it as in synchronous communications. UART is the communication standard of our old computers RS-232 serial port. Most of the Microchips PIC Microcontrollers have built in USART Module. USART stands for Universal Synchronous Asynchronous Receiver Transmitter. It can be configured in the following Modes :
UART Asynchronous (Full Duplex) USRT Master Synchronous (Half Duplex) USRT Slave Synchronous (Half Duplex) In this tutorial we are concentrating on sending and receiving data in the UART Mode using Hi-Tech C compiler. You may already know that Hi-Tech C has no built in functions for these, so we require some hardware knowledge for writing the code. I am going to explain it in deeply. If you dont need deep knowledge in this, please skip to the coding section.
Bit 7 CSRC : Clock Source Select Bit, this bit has not application in the Asynchronous mode operation of USART module. It is used to select master or slave mode in Synchronous mode operation. Bit 6 TX9 : When this bit is set it enables the 9 bit transmission otherwise 8 bit transmission is used. 9th bit in the 9 bit transmission mode is commonly used as parity bit. Bit 5 TXEN : Setting this bit enables the transmission. In the synchronous mode operation CREN and SREN bits of RCSTA register overrides this bit.
Bit 4 SYNC : This is the USART Mode select bit. Setting this bit selects Synchronous mode while clearing this bit selects Asynchronous mode. Bit 3 Unimplemented : This bit is unimplemented and will read as 0. Bit 2 BRGH : This is the High Baud Rate Select bit for Asynchronous mode operation and is unused in Synchronous mode. Setting this bit selects High Speed and clearing this bit selects Low Speed baud rates. You will can see the baud rate calculation later in this article. Bit 1 TRMT : This is the Transmit Shift Register (TSR) status bit. This can be used to check whether the data written to transmit register is transmitted or not. When the TRS is empty this bit is set and when the TSR is full this bit will be 0. Bit 0 TX9D : This is the 9th bit of data in the 9 bit transmission mode. This is commonly used as parity bit.
Bit 7 SPEN : Serial Port Enable bit. Setting this bit enables serial port and configures RC7, RC6 as serial port pins. Bit 6 RX9 : Setting this bit enables 9 bit reception otherwise it will be in 8 bit reception mode. Bit 5 SREN : Single Receive Enable bit. This bit has no effect on Asynchronous mode and Synchronous Slave mode. Setting this bit will enables Single Receive. This bit will cleared after the reception is complete. Bit 4 CREN : Continuous Receive Enable bit. Setting this bit will enable Continuous Receive. In the Synchronous Mode CREN overrides SREN. Bit 3 ADDEN : Address Detect Enable bit. This bit is applicable only in Asynchronous 9 bit mode. Setting this bit enables Address Detect. Bit 2 FERR : Framing Error bit. 1 at this bit stands for Framing Error while 0 stands for No Framing Error. Bit 1 OERR : Overrun Error bit. A high at this bit indicates that Overrun error has occured. Bit 0 RX9D : This is the 9th bit of Received Data and is commonly used as Parity Bit.
Baud Rate Generator provides the required clock for the data transmission and reception. USART module has a dedicated 8 bit baud rate generator which supports both Synchronous and Asynchronous modes. The 8-bit SPBRG register controls the time period of this free running timer. In Asynchronous mode BRGH, 2nd bit of TXSTA register also controls the generated baud rate but in Synchronous mode it is ignored. Baud Rate can be calculated from the following equations, where FOSC is the clock frequency of the microcontroller.
Hi-Tech C Programming
Initializing UART
char UART_Init(const long int baudrate) { unsigned int x; x = (_XTAL_FREQ - baudrate*64)/(baudrate*64); //SPBRG for Low Baud Rate if(x>255) //If High Baud Rate required { x = (_XTAL_FREQ - baudrate*16)/(baudrate*16); //SPBRG for High Baud Rate BRGH = 1; //Setting High Baud Rate } if(x<256) { SPBRG = x; //Writing SPBRG register SYNC = 0; //Selecting Asynchronous Mode SPEN = 1; //Enables Serial Port TRISC7 = 1; TRISC6 = 1; CREN = 1; //Enables Continuous Reception TXEN = 1; //Enables Transmissio return 1; } return 0; }
Note : 6th and 7th bit of TRISC registers are set as prescribed in the datasheet.
Writing Text
The following function can be used to write a string or array of characters to UART. It is accomplished by continuous use of character writing function UART_Write(). void UART_Write_Text(char *text) { int i; for(i=0;text[i]!='';i++) UART_Write(text[i]); }
Reading a Character
The following function wait till the reception is complete and reads 8 bit data from the Receive Register. char UART_Read() { while(!RCIF); //Waits for Reception to complete return RCREG; //Returns the 8 bit data }
Reading Text
The following function can be used to read a desired length of text or sequence of characters continuously. void UART_Read_Text(char *Output, unsigned int length) { int i; for(int i=0;i<length;i++) Output[i] = UART_Read(); } For simplifying the program readability we put all the above function to a header file uart.h. Thus you just need to include this header file and use required functions. For demonstrating the working of these functions we are using the following example.
Circuit Diagram
Note : TX of Master Microcontroller is connected to RX of Slave Microcontroller and RX of Master Microcontroller is connected to the TX of Slave Microcontroller.
Hi-Tech C Codes
Master Code
#include<htc.h> #include<pic.h> #define _XTAL_FREQ 8000000 #include "uart.h" void main() { TRISB = 0xFF; //PORTB as Input
Slave Code
#include<htc.h> #include<pic.h> #define _XTAL_FREQ 8000000 //Clock Frequency #include "uart.h" void main() { TRISB = 0x00; //PORTB as Output UART_Init(9600); do { if(UART_Data_Ready()) PORTB = UART_Read(); __delay_ms(100); }while(1); } Note : Dont forget to add Header file to the Project Folder and File list before compiling.