|
The microcontroller USART peripheral
requires setting up so that data can be transmit and received. The
USART needs to be activated so that the associated pins are used for
communications (and not as general purpose Input/Output). The data
format and speed (baudrate) also need configuring to be the same
as the PC. All the setup information can be found in the PIC
datasheet.
UART Registers
The PIC 16F627 has 3 registers that
require setting up for communications
SPBRG (Baud rate generator) -
this register is used to set up the baudrate (speed) of the UART.
TXSTA (Transmit status and
control) - this register is used to set up the UART for transmission and
also to see when bytes have been transmit
RCSTA (Recive status and
control) - this register is used to set up the UART for receiving and
also for checking to see in an error occurred whilst recieving a byte.
In our example we are going to transmit
and receive at 9600 baud. Our data will have 8 data bits and each
byte will have a start and one stop bit.
Baudrate (SPBRG)
The baudrate is dependant on the
microcontroller clock speed, as we are using the internal oscillator in
our example this will be 4MHz. There are two formulas for
calculating the baudrate depending on whether the High Baud Rate bit is
selected (BRGH).
BRGH = 0 (low speed)
BaudRate = Fosc / (64(X+1))
X = (Fosc / 64xBaudRate) - 1
X = (4000000 / 64x9600) - 1
X = 5.51
choosing X = 6 yields:
BaudRate = 8929 (7.00 % error)
|
BRGH = 1 (high speed)
BaudRate = Fosc / (16(X+1))
X = (Fosc / 16xBaudrate) - 1
X = (4000000 / 16x9600) - 1
X = 25.04
choosing X = 25 yields:
BaudRate = 9615 (0.16 % error)
|
As the baudrate needs to be within 5%
(max), then high speed (BRGH = 1) must be used.
Transmit Register (TXSTA)
The Transmit Register requires setting
to 00100100 (binary) which is 0x24 (hex)
| Bit |
Name |
Setting |
Deception |
| 7 |
CSRC |
0 |
We are running
asynchronous so we don't have a clock |
| 6 |
TX9 |
0 |
8-bit transmission |
| 5 |
TXEN |
1 |
Enable the Device
for Transmission |
| 4 |
SYNC |
0 |
Asynchronous Mode |
| 3 |
- |
0 |
- |
| 2 |
BRGH |
1 |
High Speed Baud Rate
required |
| 1 |
TRMT |
0 |
This is a READ only
bit |
| 0 |
TX9D |
0 |
No 9 bit
transmission or parity. |
Receiving Register (RCSTA)
The Recieve Register requires setting
to 10010000 (binary) which is 0x90 (hex)
| Bit |
Name |
Setting |
Deception |
| 7 |
SPEN |
1 |
Enable the serial
port |
| 6 |
RX9 |
0 |
8-bit reception |
| 5 |
SREN |
0 |
not used for
asynchronous |
| 4 |
CREN |
1 |
Continuous Receive |
| 3 |
ADEN |
0 |
not used for
asynchronous |
| 2 |
FERR |
0 |
This is READ only bit |
| 1 |
OERR |
0 |
This is READ only bit |
| 0 |
RX9D |
0 |
This is READ only bit |
Initialisation Code
As well as the UART registers there is
one other set up that is required. The PORT Pins must be set up as
inputs for the pins to be associated with the UART peripheral. The
pins associated with the UART are on PORT B, RB1 (RX) and RB2 (TX).
The following code extract configures the UART.
void uart_initialisation(void)
{
TRISB |= 0x6; //Set RB1 and RB2 as inputs
SPBRG = 25; //Setup Baudrate to 9600
TXSTA = 0x24; //Enable Transmission
RCSTA = 0x90; //Enable Reception
return;
} |
|