USART Async

Download as pptx
Download as pptx
You are on page 1of 16

USART

Universal Synchronous Asynchronous Receiver Transmitter

in

Asynchronous Mode
Main Functions
Can be synchronous or asynchronous
Can receive and transmit
Full duplex asynchronous operation

Most Common Use


RS-232 communications to a PC serial port
Registers
Registers used to control USART
SPBRG : Baud rate generator
TXSTA : Transmit status and control
RCSTA : Receive status and control
TXREG : Transmit data register
RCREG : Receive data register
PIR1 : Peripheral interrupt flag register
PIE1 : Peripheral interrupt enable register
USART – Interrupts

Interrupt flags and enable bits


INTCON : Interrrupt control register GIE, PEIE
PIE1 : Peripheral interrupt enable register TXIE,
RCIE
PIR1 : Peripheral interrupt flag register TXIF, RCIF
USART – Interrupts
Enabling interrupts

bsf PIE1, TXIE


bsf PIE1, RCIE
bsfINTCON, PEIE
bsf INTCON, GIE
The USART Asynchronous module consists
of the following important elements:

Baud Rate Generator


Asynchronous Transmitter
Asynchronous Receiver
Baud Rate

Baud rate generator register


– SPBRG register
– BRGH bit in TXSTA register
– SYNC bit in TXSTA register
Baud Rate
Baud rate generator register
Baud rate = Fosc/(16(SPBRG+1)), BRGH=1
Baud rate = Fosc/(64(SPBRG+1)), BRGH=0
Formulas for SPBRG
SPBRG = (Fosc/(16 x Baud rate)) - 1, BRGH=1
SPBRG = (Fosc/(64 x Baud rate)) - 1, BRGH=0
USART – Baud Rate
Example calculation
Given : 4MHz
9600 baud asynchronous
For BRGH = 1
SPBRG = 4000000/(16x9600) – 1 = 25.04
For BRGH = 0
SPBRG = 4000000/(64x9600) – 1 = 5.51
Best choice is BRGH = 1, SPBRG = 25
USART – Baud Rate
Setting up the baud rate

movlw D'25'
movwf SPBRG
bsf TXSTA, BRGH

SPBRG=6; /* Set baud rate to 9.6K*/


SYNC=0; /* Asynchronous mode*/
SPEN=1; /* Serial input enable*/
USART – TXSTA Register
TXSTA – Transmit status and control
CSRC : Clock source select
TX9 : 9 bit transmission
TXEN : Transmit enable
SYNC : Synchronous mode select
BRGH : High baud rate select
TRMT : Transmit shift register status
TX9d : 9th bit of data to transmit
USART – RCSTA Register
RCSTA – Receive status and control
SPEN : Serial port enable
RX9 : 9 bit receive enable
SREN : Single receive enable
CREN : Continuous receive enable
ADDEN : Address detect enable
FERR : Framing error
OERR : Overrun error
RX9D : 9th bit of data received
Steps to follow when setting up an Asynchronous
Transmission or Reception :
1. Initialize the SPBRG register for the appropriate baud rate.
2. Enable the asynchronous serial port (clear bit SYNC and set bit SPEN)
3. If interrupts are desired, then set enable bit TXIE or bit RCIE.
4. If 9-bit transmission is desired, then set transmit bit TX9.
5. Enable the transmission by setting bit TXEN or the reception by setting bit
RCEN
(FOR TRANSMISSION)
6. If 9-bit transmission is selected, the ninth bit should be loaded in bit
TX9D.
7. Load data to TXREG register
(RECEPTION)
8. Read RCSTA register to get the ninth bit, if enabled (reception)
9. Read the 8-bit received data by reading the RCREG register (reception)
USART – Setting up
• Setting up 8 bit asynchronous mode
– High baud rate, no interrupt
movlw
B'00100100'
movwf TXSTA
movlw B'10010000'
movwf RCSTA
USART – Transmitting
• Loading the byte and transmitting data
(8 data bits in DataTransmit)

movf DataTransmit, w
movf TXREG
USART – Receiving
• Loading the byte and transmitting data
(8 data bits in DataTransmit)

movf RCREG, w
movwf DataReceived

You might also like