(24 intermediate revisions by the same user not shown)
Line 17: Line 17:
 
|-
 
|-
 
|SPBRG|| USART Baud Rate Generator
 
|SPBRG|| USART Baud Rate Generator
 +
|-
 +
|TXREG||USART Transmit Register. Holds the data to to be transmitted on UART
 +
|-
 +
|RCREG|| USART Transmit Register. Holds the data received from UART
 
|}
 
|}
 
<br><br>
 
<br><br>
 
  
 
=UART Register Configuration=
 
=UART Register Configuration=
Line 31: Line 34:
 
|CSRC || TX9 || TXEN || SYNC || -  || BRGH || TRMT || TX9D
 
|CSRC || TX9 || TXEN || SYNC || -  || BRGH || TRMT || TX9D
 
|}
 
|}
 +
 +
<b>CSRC:</b> Clock Source Select bit<br>
 +
Asynchronous mode:Don’t care.
 +
 +
<b>TX9:</b> 9-bit Transmit Enable bit<br>
 +
1 = Selects 9-bit transmission<br>
 +
0 = Selects 8-bit transmission
 +
 +
<b>TXEN:</b> Transmit Enable bit<br>
 +
1 = Transmit enabled<br>
 +
0 = Transmit disabled
 +
 +
<b>SYNC:</b> USART Mode Select bit<br>
 +
1 = Synchronous mode<br>
 +
0 = Asynchronous mode
 +
 +
<b>BRGH:</b> High Baud Rate Select bit<br>
 +
1 = High speed<br>
 +
0 = Low speed
 +
 +
<b>TRMT:</b> Transmit Shift Register Status bit<br>
 +
1 = TSR empty<br>
 +
0 = TSR full
 +
 +
<b>TX9D:</b> 9th bit of Transmit Data, can be Parity bit
  
  
Line 36: Line 64:
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
|-class="info"
 
|-class="info"
|TXSTA
+
|RCSTA
 
|-
 
|-
 
|7 || 6 ||  5 || 4 || 3 || 2 || 1 || 0   
 
|7 || 6 ||  5 || 4 || 3 || 2 || 1 || 0   
Line 42: Line 70:
 
|SPEN || RX9 || SREN || CREN || ADDEN || FERR || OERR || RX9D
 
|SPEN || RX9 || SREN || CREN || ADDEN || FERR || OERR || RX9D
 
|}
 
|}
 +
 +
<b>SPEN:</b> Serial Port Enable bit<br>
 +
1 = Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port pins)<br>
 +
0 = Serial port disabled
 +
 +
<b>RX9:</b> 9-bit Receive Enable bit<br>
 +
1 = Selects 9-bit reception<br>
 +
0 = Selects 8-bit reception
 +
 +
<b>SREN:</b> Single Receive Enable bit<br>
 +
Asynchronous mode:Don’t care.
 +
 +
<b>CREN:</b> Continuous Receive Enable bit<br>
 +
Asynchronous mode:<br>
 +
1 = Enables continuous receive<br>
 +
0 = Disables continuous receive
 +
 +
<b>ADDEN:</b> Address Detect Enable bit<br>
 +
Asynchronous mode 9-bit (RX9 = 1):<br>
 +
1 = Enables address detection, enables interrupt and load of the receive buffer when RSR is set<br>
 +
0 = Disables address detection, all bytes are received and ninth bit can be used as parity bit
 +
 +
<b>FERR:</b> Framing Error bit<br>
 +
1 = Framing error (can be updated by reading RCREG register and receive next valid byte)<br>
 +
0 = No framing error
 +
 +
<b>OERR:</b> Overrun Error bit<br>
 +
1 = Overrun error (can be cleared by clearing bit CREN)<br>
 +
0 = No overrun error
 +
 +
<b>RX9D:</b> 9th bit of Received Data (can be parity bit but must be calculated by user firmware)
 +
<br><br>
 +
 +
 +
=Baud Rate Calculation=
 +
The main criteria for UART communication is its baud rate. Both the devices Rx/Tx should be set to same baud rate for successful communication.<br>
 +
This can be achieved by SPBRG register.  SPBRG is a 8-bit register which controls the baud rate generation.<br>
 +
Given the desired baud rate and FOSC, the nearest integer value for the SPBRG register can be calculated using the below formula.
 +
 +
*BRGH = 1 High Speed
 +
$$SPBRG = (Fosc / (16 * BaudRate)) - 1$$
 +
 +
*BRGH = 0 Low Speed
 +
$$SPBRG = (Fosc / (64 * Baud rate)) - 1$$
 +
 +
It may be advantageous to use the high baud rate (BRGH = 1) even for slower baud clocks.
 +
This is because the FOSC/(16 (X + 1)) equation can reduce the baud rate error in some cases. Below table shows the list of standard BaudRates and the SPBRG values.
 +
[[FILE:Pic16f877a_BaudRates.png]]
 +
 +
=Steps To Send Char=
 +
#Wait till the previous char is transmitted. TXIF will be set when the TXREG is empty.
 +
#Clear the TXIF for next cycle.
 +
#Load the new char to be transmitted into THR.<br>
 +
<html>
 +
<script src="https://gist.github.com/SaheblalBagwan/ef0f2b16bc6eb398a8e7a2d272bd74dc.js"></script>
 +
</html>
 +
<br><br>
 +
 +
=Steps To Receive Char=
 +
#Wait till the Data is received. RCIF will be set once the data is received in RCREG register.
 +
#Clear the receiver flag(RCIF) for next cycle.
 +
#Copy/Read the received data from RCREG register.<br>
 +
<html>
 +
<script src="https://gist.github.com/SaheblalBagwan/f47ebf0daaffe565a1d64aa6daea8bab.js"></script>
 +
</html>
 +
<br><br>
 +
 +
=Code=
 +
Below is the sample code to Transmit and receive the chars at 9600 baudrate with 20Mhz clock.
 +
<html>
 +
<script src="https://gist.github.com/SaheblalBagwan/28efb0ab8aa0523bd1fcaf40abca3d3b.js"></script>
 +
</html>
 +
 +
=Using ExploreEmbedded Libraries=
 +
In the above tutorial we discussed how to configure and use the inbuilt Pic16f877a UART.<br>
 +
Now we will see how to use the ExploreEmbededd UART library.<br>
 +
For this you have to include the uart.c/uart.h files and associated gpio/stdutils files.<br>
 +
*Note:Refer the uart.h file for more info.
 +
 +
<html>
 +
<script src="https://gist.github.com/sharanago/12a15188d6143edfa7ba6408d6c1da02.js"></script>
 +
</html>
 +
[[File:Pic16f877a uart.PNG]]<br><br>
 +
 +
 +
=Downloads=
 +
Download the complete project folder from the below link:<br>
 +
[https://github.com/ExploreEmbedded/Pic16f877a_ExploreUltraPicDevKit/archive/master.zip Hardware design Files and Code Library]
 +
 +
 +
{{DISQUS}}

Latest revision as of 17:39, 5 April 2017

In this tutorial we are going to discuss the serial/UART communication using PIC16F877A.
PIC16F877A comes with inbuilt USART which can be used for Synchronous/Asynchronous communication. We will be discussing only the UART. After understating the basics of PIC16F877A UART module, We will see how to use the ExploreEmbedded libraries to communicate with any of the UART devices.

0 UART main.gif


UART Registers

The below table shows the registers associated with PIC16F877A UART.

Register Description
TXSTA Transmit Status And Control Register
RCSTA Receive Status And Control Register
SPBRG USART Baud Rate Generator
TXREG USART Transmit Register. Holds the data to to be transmitted on UART
RCREG USART Transmit Register. Holds the data received from UART



UART Register Configuration

Now lets see how to configure the individual registers for UART communication.

TXSTA
7 6 5 4 3 2 1 0
CSRC TX9 TXEN SYNC - BRGH TRMT TX9D

CSRC: Clock Source Select bit
Asynchronous mode:Don’t care.

TX9: 9-bit Transmit Enable bit
1 = Selects 9-bit transmission
0 = Selects 8-bit transmission

TXEN: Transmit Enable bit
1 = Transmit enabled
0 = Transmit disabled

SYNC: USART Mode Select bit
1 = Synchronous mode
0 = Asynchronous mode

BRGH: High Baud Rate Select bit
1 = High speed
0 = Low speed

TRMT: Transmit Shift Register Status bit
1 = TSR empty
0 = TSR full

TX9D: 9th bit of Transmit Data, can be Parity bit


RCSTA
7 6 5 4 3 2 1 0
SPEN RX9 SREN CREN ADDEN FERR OERR RX9D

SPEN: Serial Port Enable bit
1 = Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port pins)
0 = Serial port disabled

RX9: 9-bit Receive Enable bit
1 = Selects 9-bit reception
0 = Selects 8-bit reception

SREN: Single Receive Enable bit
Asynchronous mode:Don’t care.

CREN: Continuous Receive Enable bit
Asynchronous mode:
1 = Enables continuous receive
0 = Disables continuous receive

ADDEN: Address Detect Enable bit
Asynchronous mode 9-bit (RX9 = 1):
1 = Enables address detection, enables interrupt and load of the receive buffer when RSR is set
0 = Disables address detection, all bytes are received and ninth bit can be used as parity bit

FERR: Framing Error bit
1 = Framing error (can be updated by reading RCREG register and receive next valid byte)
0 = No framing error

OERR: Overrun Error bit
1 = Overrun error (can be cleared by clearing bit CREN)
0 = No overrun error

RX9D: 9th bit of Received Data (can be parity bit but must be calculated by user firmware)


Baud Rate Calculation

The main criteria for UART communication is its baud rate. Both the devices Rx/Tx should be set to same baud rate for successful communication.
This can be achieved by SPBRG register. SPBRG is a 8-bit register which controls the baud rate generation.
Given the desired baud rate and FOSC, the nearest integer value for the SPBRG register can be calculated using the below formula.

  • BRGH = 1 High Speed

$$SPBRG = (Fosc / (16 * BaudRate)) - 1$$

  • BRGH = 0 Low Speed

$$SPBRG = (Fosc / (64 * Baud rate)) - 1$$

It may be advantageous to use the high baud rate (BRGH = 1) even for slower baud clocks. This is because the FOSC/(16 (X + 1)) equation can reduce the baud rate error in some cases. Below table shows the list of standard BaudRates and the SPBRG values. Pic16f877a BaudRates.png

Steps To Send Char

  1. Wait till the previous char is transmitted. TXIF will be set when the TXREG is empty.
  2. Clear the TXIF for next cycle.
  3. Load the new char to be transmitted into THR.



Steps To Receive Char

  1. Wait till the Data is received. RCIF will be set once the data is received in RCREG register.
  2. Clear the receiver flag(RCIF) for next cycle.
  3. Copy/Read the received data from RCREG register.



Code

Below is the sample code to Transmit and receive the chars at 9600 baudrate with 20Mhz clock.

Using ExploreEmbedded Libraries

In the above tutorial we discussed how to configure and use the inbuilt Pic16f877a UART.
Now we will see how to use the ExploreEmbededd UART library.
For this you have to include the uart.c/uart.h files and associated gpio/stdutils files.

  • Note:Refer the uart.h file for more info.

Pic16f877a uart.PNG


Downloads

Download the complete project folder from the below link:
Hardware design Files and Code Library