Nothing Special   »   [go: up one dir, main page]

Uart Mcu

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Using the UART in Microchip PIC18F

Microcontrollers

Corrado Santoro

ARSLAB - Autonomous and Robotic Systems Laboratory


Dipartimento di Matematica e Informatica - Università di Catania, Italy
santoro@dmi.unict.it

L.A.P. 1 Course

Corrado Santoro Using the UART in PIC18F Family


UART in MCUs

UART = Universal Asynchronous Receiver/Trasmitter


It is commonly referred as serial port
It is a peripheral for point-to-point communication between
two devices
Communication occurs in serial, i.e. one bit at time
Two communication PINs: RX and TX

Corrado Santoro Using the UART in PIC18F Family


UART trasmission basics
When no transmission, the line is set to Logical “1”
Then the software triggers the trasmission of a byte
(e.g. “C”, hexcode 43, binary 0100 0011
First a Logical “0” is transmitted, called start bit
Then the byte is transmitted LSB first
An additional parity bit may follow (not in the example); it
used for error checking
One or two stop bits (Logical “1”) ends the transmission

Corrado Santoro Using the UART in PIC18F Family


UART trasmission parameters

The following parameters must be set in the UART hardware:


transmission speed, in bps = Bit Per Second or baud
number of bits per character, usually 8
presence/absence of partity bit, usually absent
number of stop bits, usually 1

A setting 19200,8,N,1 means:


speed = 19200 bit-per-second;
bits = 8;
parity = None;
stop bits = 1.

Corrado Santoro Using the UART in PIC18F Family


UART in the PIC18F25K22

From the datasheet:


Two different UART
The TX line of the UART1 is shared with RC6
The RX line of the UART1 is shared with RC7
The TX line of the UART2 is shared with RB6
The RX line of the UART2 is shared with RB7

Basic registers per UARTx (“x” is the UART number):


TXSTAx, configuration of the transmitter
RCSTAx, configuration of the receiver
BAUDCONx, configuration of the baud rate (speed)
generator
SPBRGx, baud rate (speed) generator value

Corrado Santoro Using the UART in PIC18F Family


The Baud Rate Generator

It is divisor driven from system clock frequency (FOSC).


It is controlled by the following bits/registers:

SPBRGHx:SPBRGx: the divisor value to be applied to


FOSC to obtain the desired baud rate
BAUDCONxbits.BRG16, a bit which indicates whether the
divisor value uses 8 or 16 bits
TXSTAxbits.BRGH, a bit which indicates if we are using a
high-speed baud date

A formula exists to compute the divisor value, but... Microchip


provides very usefull tables!

Corrado Santoro Using the UART in PIC18F Family


Setting the Baudrate generator

Corrado Santoro Using the UART in PIC18F Family


Setting the UART

UART Setup
...
// setup UART
TRISCbits.TRISC6 = 0; // TX as output
TRISCbits.TRISC7 = 1; // RX as input

TXSTA1bits.SYNC = 0; // Async operation


TXSTA1bits.TX9 = 0; // No tx of 9th bit
TXSTA1bits.TXEN = 1 // Enable transmitter

RCSTA1bits.RX9 = 0; // No rx of 9th bit


RCSTA1bits.CREN = 1; // Enable receiver
RCSTA1bits.SPEN = 1; // Enable serial port

// Setting for 19200 BPS


BAUDCON1bits.BRG16 = 0; // Divisor at 8 bit
TXSTA1bits.BRGH = 0; // No high-speed baudrate
SPBRG1 = 51; // divisor value for 19200

Corrado Santoro Using the UART in PIC18F Family


The UART Transmitter

Transmission is triggered by loading the byte into TXREGx;


The byte is loaded into the shift-register and the TXxIF is set;
The byte is “shifted-out” (transmitted) and, when transmission is
completed TXSTAxbits.TRMT is set;
A new byte can be loaded into TXREG for a new transmission.
Corrado Santoro Using the UART in PIC18F Family
Transmitting a byte
Basic transmission
// wait the end of transmission
while (TXSTA1bits.TRMT == 0) {};
TXREG1 = new data; // start sending the new byte

... or, you can use directly the printf function, provided that you
implement a proper putch(char c) function, that is then called
by printf for each character to send.

Using printf
void putch(char c) {
// wait the end of transmission
while (TXSTA1bits.TRMT == 0) {};
TXREG1 = c; // send the new byte
}
...
printf("Hello world!!");
...

Corrado Santoro Using the UART in PIC18F Family


The UART Receiver

When a byte is received it is automatically loaded into RCREGx;


When reception is completed RCxIF is set, which can be tested (using
polling or interrupt);
Reading RCREGx causes RCxIF to be automatically reset;
But some errors may occur and must be handled:
Overrun error, RCSTAxbits.OERR==1, a new data is received
but RCREGx has not previously read;
Framing error, RCSTAxbits.FERR==1, the data is not properly
“framed”, i.e. the stop bit(s) are not valid.

Corrado Santoro Using the UART in PIC18F Family


Reading a character

UART Char Reading


char read char(void)
{
while (PIR1bits.RC1IF == 0) { // wait for char
if (RCSTA1bits.OERR == 1) {
RCSTA1bits.OERR = 0; // clear overrun if it occurs
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}
}
return RCREG1;
}

Corrado Santoro Using the UART in PIC18F Family


Using the UART in Microchip PIC18F
Microcontrollers

Corrado Santoro

ARSLAB - Autonomous and Robotic Systems Laboratory


Dipartimento di Matematica e Informatica - Università di Catania, Italy
santoro@dmi.unict.it

L.A.P. 1 Course

Corrado Santoro Using the UART in PIC18F Family

You might also like