You are on page 1of 22

SERIAL PORT USART

PIC18f452

COURSE: Microprocessors and Microcontrollers-II


CONTACT: atiq@mail.au.edu.pk
WHY SERIAL COMMUNICATION?
- Parallel I/O uses many signal pins
- Cost of cable
- Many applications do not need high data rate
TYPES OF SERIAL I/O
1. USART (universal synchronous asynchronous receiver and transceiver)
2. SPI (synchronous peripheral interface)
3. I2C (Inter-Integrated circuit)
4. CAN bus (Controller Area Network bus)
5. LIN (Local Interconnect Network)
6. USB (Universal Serial Bus)
USART
INTRO
 The USART can be configured in the following modes:
• Asynchronous (full-duplex)
We will only Discuss
• Synchronous - Master (half-duplex)
Asynchronous 8-bit mode
• Synchronous - Slave (half-duplex)
 Registers and Flags associated with USART
 TXSTA (8-bit)  Transmit Status and Control Reg
 RCSTA (8-bit)  Receive Status and Control Reg
 TXREG (8-bit)  Hold Data to be Transmitted
 RCREG (8-bit)  Hold Received Data
 SPBRG (8-bit)  To generate Baud Rate
 TXIF flag  Indicates that transmission of data is done!
 RCIF flag  Indicates that Reception of data is complete!
 When using the synchronous communication –  In this case, the information is divided into
the information is transmitted from the frames, in the size of byte. Each one of the
transmitter to the receiver: frame has:
 in sequence bit after bit  “Start” bit marks the beginning of a new frame.
 and the clock frequency is transmitted along with  “Stop” bit marks the end of the frame.
the bits
 That means that the transmitter and the receiver
are synchronized between them by the same
 Frames of information must not necessarily be
clock frequency. The clock frequency can be
transmitted at equal time space, since they are
transmitted along with the information, while it
independent of the clock.
is encoded in the information itself, or in many
cases there is an additional wire for the clock.

 Faster compare to the asynchronous


communication since it is "constantly
transmitting” the information, with no stops.
INSIDE TXSTA REGISTER

bit 7 CSRC: Clock Source Select bit


Asynchronous mode: Don’t care
Synchronous mode:
1 = Master mode (clock generated internally from BRG)
0 = Slave mode (clock from external source)

bit 6 TX9: 9-bit Transmit Enable bit


1 = Selects 9-bit transmission
0 = Selects 8-bit transmission

bit 5 TXEN: Transmit Enable bit


1 = Transmit enabled
0 = Transmit disabled
INSIDE TXSTA REGISTER
bit 4 SYNC: USART Mode Select bit
1 = Synchronous mode
0 = Asynchronous mode

bit 3 Unimplemented: Read as '0‘

bit 2 BRGH: High Baud Rate Select bit


Asynchronous mode:
1 = High speed
0 = Low speed
Synchronous mode: Unused in this mode

bit 1 TRMT: TSR (Transmit Shift Register) Status bit  This bit can also be used to check transmission of data
1 = TSR empty
0 = TSR full
NOTE: If TSR is Empty then flag TXIF will be high. It becomes automatically zero when TSR is loaded with new data!

bit 0 TX9D: 9th bit of Transmit Data


Can be Address/Data bit or a parity bit.
INSIDE RCSTA REGISTER
bit 7 SPEN: Serial Port Enable bit
1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins)
0 = Serial port disabled

bit 6 RX9: 9-bit Receive Enable bit


1 = Selects 9-bit reception
0 = Selects 8-bit reception

bit 5 SREN: Single Receive Enable bit


Asynchronous mode: Don’t care
Synchronous mode - Master:
1 = Enables single receive
0 = Disables single receive
This bit is cleared after reception is complete.
Synchronous mode - Slave: Don’t care

bit 4 CREN: Continuous Receive Enable bit


Asynchronous mode:
1 = Enables receiver
0 = Disables receiver
Synchronous mode:
1 = Enables continuous receive until enable bit CREN is cleared
0 = Disables continuous receive
INSIDE RCSTA REGISTER

bit 3 ADDEN: Address Detect Enable bit


Asynchronous mode 9-bit (RX9 = 1):
1 = Enables address detection, enable interrupt and load of the receive buffer
when RSR<8> is set
0 = Disables address detection, all bytes are received, and ninth bit can be
used as parity bit
1. Framing error - May
bit 2 FERR: Framing Error bit occur due to clock
1 = Framing error (can be updated by readingsynchronization
RCREG register and receive
next valid byte) problem - Can be
detected by the
0 = No framing error
missing stop bit.
2. Receiver overrun - May
bit 1 OERR: Overrun Error bit occur when the CPU
1 = Overrun error (can be cleared by clearingdid
bit CREN)
not read the
0 = No overrun error received data for a
while
bit 0 RX9D: 9th bit of Received Data
This can be Address/Data bit or a parity bit, and must be calculated by user firmware.
SPBRG VALUES FOR BAUD RATE
 Two formulas to calculate SPBRG value.
 Selection of formula depends on BRGH bit in TXSTA register

SPBRG = (Fosc / (16 x Baud rate)) - 1, if BRGH = 1 High Speed

SPBRG = (Fosc / (64 x Baud rate)) - 1, if BRGH = 0 Low Speed

e.g for Xtal=4Mhz, and Baud Rate= 1.2kbps @ Low speed


SPBRG = (4MHz / (64x1200)) -1 = 51.08 =51 approx.

NOTE: Table of SPBRG values for different Baud Rates with Different Foscs given in book
and Datasheet
Load TXSTA Load SPBRG register Enable USART Load TXREG
reg for async Set TX (RC6) according to Baud using SPEN bit in with data to be
mode pin as output Rate RCSTA reg transmitted

Goto next step Wait for TXIF


according your to become high
requirement (wait for
completion of data
transmission)

NOTE: TXIF flag automatically get cleared when data is loaded in TXREG
PROBLEM: Write a MIKROC program to transmit character ‘A’ continuously with Baud rate 9600 with
XTAL=10Mhz.
SOLUTION:
void main()
{
TRSIC.TRISC6 = 0; // TX configured as o/p
TXSTA = 0x20; // 8.bit async mode, low baud rate
SPBRG=15; //9600 Baud Rate, Xtal =10Mhz
TXSTA.TXEN=1; //enable transmission .. Optional
RCSTA.SPEN = 1; // Enable USART
while(1)
{
TXREG = 'A'; // Load the character to be transmitted
while(PIR1.TXIF==0); // Wait here till transmission is complete
}
}
Load RCSTA reg Load SPBRG register
Load TXSTA reg to
for continuous Set RX (RC7) according to Baud
select low or high
reception for 8-bit pin as input Rate
speed Baud rate
data

Goto next step Take data out Wait for RCIF


according your from RCREG if to become high
requirement needed! (wait for reception
of data)

NOTE: RCIF flag automatically get cleared when received data is copied from RCREG
PROBLEM: Write a MIKROC program to throw serially received data to PORTB continuously. Take
Baud rate 9600 with XTAL=10Mhz.
SOLUTION:
void main()
{
TRSIC.TRISC7 = 1; // RX configured as o/p
RCSTA = 0x90; // enable serial port and receiver
TXSTA.BRGH=0; //low baud rate
SPBRG=15; //9600 Baud Rate, Xtal =10Mhz
while(1)
{
while(PIR1.RCIF==0); //Wait here till transmission is complete
PORTB = RCREG; // throw received data to PORTB
}
}
ASSIGNMENT
 Try to understand examples 10.12 to 10.15
SOME USEFUL MIKROC LIBRARY
FUNCTIONS OF USART
MIKROC LIBRARY FUNCTIONS FOR USART
 void UARTx_Init(unsigned long baud_rate); Configures and
initializes the USART module. The internal USART module is setting
configured in this function can be seen in MIKROC manual
// CODE to Initialize hardware UART1 module and
establish communication at 2400 bps

UART1_Init(2400);

 unsigned UARTx_Data_Ready(); The function tests if data in receive


buffer is ready for reading. // code to read data if ready
Unsigned char receive;
It Returns: UART1_Init(2400); //baud rate=2400
 1 if data is ready for reading. if (UART1_Data_Ready())
receive = UART1_Read();
 0 if there is no data in the receive register.
MIKROC LIBRARY FUNCTIONS FOR USART
 char UARTx_Tx_Idle();
Use this function to test if the // CODE: If the previous data has been shifted
out, send next data:
transmit shift register is empty or not. UART1_Init(2400); //baud rate=2400
if (UART1_Tx_Idle() == 1)
It RETURNS: { UART1_Write(data); }
 1 if the data has been transmitted.
 0 otherwise.

 unsigned UARTx_Read();
The function receives a byte via USART. Use
the UARTx_Data_Ready function // read data if ready
to test if data is ready first. unsigned char receive;
UART1_Init(2400); //baud rate=2400
It Returns Received byte. if (UART1_Data_Ready())
receive = UART1_Read();
MIKROC LIBRARY FUNCTIONS FOR USART
 void UARTx_Write(unsigned _data);

The function transmits a byte via the USART module.

//code to transmit 0x1E to TX


UART1_Init(2400); //baud rate=2400
unsigned char data = 0x1E;
UART1_Write(data);

 void UARTx_Write_Text(char * UART_text);

Sends text (maximum 255 characters) via USART. Text should be zero terminated.

//Code to transmit “Air uni”


UART1_Init(4800); // initialize UART1 module Delay_ms(100);
UART1_Write_Text(“Air uni \0 “); // sends text
What if you need 2 USART’s but your
controller have only one????

Use SOFTWARE UART Library of Mikro-C …..


You can configure pins of your choice as TX
and RX!
ASSIGNMENT
Understand codes done in LAB

You might also like