Anda di halaman 1dari 14

Microprocessor & Interfacing

(CEN-321)
Semester: Spring 2018

Instructor: Muhammad Saim


PIC16F877 – USART
Serial Transmission Using UART ?
• UART: Universal Asynchronous
Receiver Transmitter embedded
device
• Takes parallel data and 1
0 1 0 1
1
1
transmits serially 0

• Receives serial data and


converts to parallel
10011011 10011011
• Parity: extra bit for simple
error checking Sending UART Receiving UART
start bit end bit
• Start bit, stop bit dat
a
• Baud rate
• signal changes per second
• bit rate usually higher
• Synchronous? - USART
• Requires Clock for 1 0 0 1 1 0 1 1
synchronization

Reference: Embedded System Design: A Unified Hardware/Software Introduction Frank Vahid and Tony Givargis, John Wiley & Sons; ISBN: 0471386782.
PIC16F877 – USART
• The Universal Synchronous Asynchronous Receiver Transmitter
(USART) module is one of the two serial I/O modules (other is the
SSP module).

• The USART is also known as a Serial communications Interface or SCI.

• The USART can be configured as a full duplex asynchronous system


that can communicate with peripheral devices such as CRT terminals
and personal computers, or
• It can be configured as a half duplex synchronous system that can
communicate with peripheral devices such as A/D or D/A integrated
circuits, Serial EEPROMs etc.

• The USART can be configured in the following modes:


• Asynchronous (full duplex)
• Synchronous - Master (half duplex)
• Synchronous - Slave (half duplex)
PIC16F877 – USART - TXSTA
PIC16F877 – USART - RCSTA
PIC16F877 – USART – Baud Rate
• The Baud Rate Generator BRG supports both the
Asynchronous and Synchronous modes of the USART.
• It is a dedicated 8-bit baud rate generator.
• The SPBRG register controls the period of a free running 8-
bit timer. In Asynchronous mode, bit BRGH (TXSTA<2>) also
controls the baud rate. In Synchronous mode, bit BRGH is
ignored.
• Writing a new value to the SPBRG register causes the
BRG timer to be reset (or cleared).
• SAMPLING
• The data on the RC7/RX/DT pin is sampled three times by a
majority detect circuit to determine if a high or a low level is
present at the RX pin.
PIC16F877 – USART – Baud Rate
PIC16F877 – USART – Asynchronous Mode
• In this mode, the USART uses standard non-return-to-zero (NRZ) format
• (one START bit, eight or nine data bits, and one STOP bit).
• The most common data format is 8-bits.

• An on-chip, dedicated, 8-bit baud rate generator can be used to derive standard baud rate frequencies
from the oscillator.
• The USART transmits and receives the LSb first.
• The transmitter and receiver are functionally independent, but use the same data format and baud
rate.
• Parity is not supported by the hardware, but can be implemented in software (and stored as the
ninth data bit).
• Asynchronous mode is stopped during SLEEP.
• Asynchronous mode is selected by clearing bit SYNC (TXSTA<4>).
• The USART Asynchronous module consists of the following important elements:
• Baud Rate Generator
• Sampling Circuit
• Asynchronous Transmitter
• Asynchronous Receiver
PIC16F877 – Asynchronous Mode - TX
• TSREG
• Load data to be transmitted in this!
• Once the TXREG register transfers the data to the TSR register
the TXREG register is empty and flag bit TXIF (PIR1<4>) is set.
• This interrupt can be enabled/disabled by setting/clearing
enable bit TXIE ( PIE1<4>).
• Transmission is enabled by setting enable bit TXEN (TXSTA<5>).
PIC16F877 – Asynchronous – TX - How
• 1. Initialize the SPBRG register for the appropriate baud rate. If a
high speed baud rate is desired, set bit BRGH.
• 2. Enable the asynchronous serial port by clearing bit SYNC and
setting bit SPEN.
• 3. If interrupts are desired, then set enable bit TXIE.
• 4. If 9-bit transmission is desired, then set transmit bit TX9.
• 5. Enable the transmission by setting bit TXEN, which will also
set bit TXIF.
• 6. If 9-bit transmission is selected, the ninth bit should be loaded
in bit TX9D.
• 7. Load data to the TXREG register (starts transmission).
• 8. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of
the INTCON register are set.
PIC16F877 – Asynchronous – RX - How
• 1. Initialize the SPBRG register for the appropriate baud rate. If a high speed baud rate
is desired,
• set bit BRGH.
• 2. Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.
• 3. If interrupts are desired, then set enable bit RCIE.
• 4. If 9-bit reception is desired, then set bit RX9.
• 5. Enable the reception by setting bit CREN.
• 6. Flag bit RCIF will be set when reception is complete and an interrupt will be
generated if enable bit RCIE is set.
• 7. Read the RCSTA register to get the ninth bit (if enabled) and determine if any error
occurred during reception.
• 8. Read the 8-bit received data by reading the RCREG register.
• 9. If any error occurred, clear the error by clearing enable bit CREN.
• 10. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register
are set.
PIC16F877 – Synchronous Mode
• In Synchronous Master mode, the data is transmitted in a half-
duplex manner (i.e., transmission and reception do not occur at
the same time).
• When transmitting data, the reception is inhibited and vice
versa.
• Synchronous mode is entered by setting bit SYNC (TXSTA<4>).
• In addition, enable bit SPEN (RCSTA<7>) is set in order to
configure the RC6/TX/CK and RC7/RX/DT I/O pins to CK (clock)
and DT (data) lines, respectively.
• The Master mode indicates that the processor transmits the
master clock on the CK line.
• The Slave mode indicates that the other device transmits the
master clock on the CK line.
PIC16F877 – Synchronous Mode - How
• From a programmer’s perspective, the overall step for
transmission and receptions are pretty much similar to
that of asynchronous mode.

• In case of Master Mode, programmer need to configure


clock source.

Anda mungkin juga menyukai