Вы находитесь на странице: 1из 32

C3 - Serial Communication Interface

Upon completion of this chapter, you will be able to:


– Explain serial communication protocol
– Describe the serial communication features of
the PIC18
– Program the PIC18 serial port in C
Introduction
• Computers transfer data in two ways: Parallel
and Serial.
• Parallel: Eight or more data lines, few feet only,
short time
• Serial: Single data line, long distance
• The PIC18 has serial communication capability
built into it.
Basics of Serial Communication
• The byte of data must be converted to serial bits
using a parallel-in-serial-out shift register

Serial versus Parallel Data Transfer


• The receiving end must be a serial-in-
parallel-out shift register and pack them
into a byte.
• Two methods of serial data
communication: Synchronous and
Asynchronous

Transfers a block
of data at a time
Transfers a single
byte at a time
Half-and Full-Duplex Transmission
Start and Stop Bits
• In the asynchronous method, each
character is placed between start and stop
bits (framing)
MSB LSB

Framing ASCII ‘A’ (41H)


Data Transfer Rate
• Rate of data transfer: bps (bits per second)
• Another widely used terminology for bps is
baud rate
• For Asynchronous serial data communication,
the baud rate is generally limited to
100,000bps
RS232 Standard
• Standard for serial comm (COM port)
1: -3V to -25V;
0: +3V to +25V
– Reason: for long distance wired line
• Input-output voltage are not TTL compatible
• So, we need MAX232/233 for voltage converter.
Commonly known as line drivers
RS232 Pins
Minimally, 3 wires:
RxD, TxD, GND

IBM PC DB-9 Signals

Pin 1 – Data Carrier Detect (DCD)


Pin 2 – Received Data (RxD)
Pin 3 – Transmitted Data (TxD)
Pin 4 – Data Terminal Ready (DTR)
Pin 5 – Signal Ground (GND)
Pin 6 – Data Set Ready (/DSR)
Pin 7 – Request to Send (/RTS)
Pin 8 – Clear to Send (/CTS)
Pin 9 – Ring Indicator (RI)
PIC18 Connection to RS232

Line driver

(a) Inside MAX232 (b) its Connection to the PIC18


SPBRG (Serial Port Baud Rate Generator)
Register and Baud Rate in the PIC18
• The baud rate in the SPBRG
PIC18 is programmable Baud Rate
(Hex Value)
• The value loaded into
the SPBRG decides the 38400 3
baud rate 19200 7
• Depend on crystal 9600 F
frequency 4800 20
2400 40
1200 81
*For XTAL = 10MHz only!
Baud rate Formula
X (SPBRG) = (156250/Desired Baud Rate) - 1

Example:

Desired baud rate = 1200, Clock Frequency =


10MHz

X (SPBRG) = (156250/1200) – 1
X (SPBRG) = 129.21 = 129 = 81H
TXREG Register
• 8-bit register used for serial communication
in the PIC18
• For a byte of data to be transferred via the
Tx pin, it must be placed in the TXREG
register
RCREG Register
• 8-bit register used for serial communication
in the PIC18
• When the bits are received serially via the
Rx pin, the PIC18 deframes them by
eliminating the START and STOP bit,
making a byte out of data received and then
placing in the RCREG register
TXSTA (Transmit Status and Control
Register)
RCSTA (Receive Status and Control
Register)
_used to enable the serial port to receive
data
PIR1 (Peripheral Interrupt Request Register 1)
Programming the PIC18 to Transfer Data Serially
1. TXSTA register = 20H: Indicating asynchronous
mode with 8-bit data frame, low baud rate and
transmit enabled
2. Set Tx pin an output
3. Loaded SPBRG for baud rate
4. Enabled the serial port (SPEN = 1)
5. Character byte to transmit is write into TXREG
6. Keep Monitor TXIF bit
7. To transmit next character, go to step 5
EX1
Write a C program to transfer the letter ‘G’ serially at 9600,
continuously. Use 8-bit data and 1 stop bit. Assume XTAL
= 10MHz.
EX2
Write a C program to transfer the message “YES” serially
at 9600, continuously. Use 8-bit data and 1 stop bit.
Assume XTAL = 10MHz.
EX3
Write a C program to receive bytes of data
serially and put them on PORTB. Set the baud
rate at 9600, 8-bit data and 1 stop bit.
EX4:
• Write a program to send two different string to the
serial port. Assume the switch is connected to pin
PORTB.5.
• SW = 0, send first name
• SW = 1, send last name
• Assume XTAL = 10 MHz, Baud rate = 9600 with 8-
bit data.
#include <P18f458.h>
#define MySW PORTBbits.RB5 //input switch

void main(void)
{
unsigned char z;
unsigned char fname [] = “Abu”;
unsigned char lname [] = “Abdul”;
TRISBbits.TRISB5=1; //PB5 as input;
TXSTA = 0x20 //low baud ratem 8-bit
SPBRG = 15 //9600 baud rate
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
If(MySW == 0)
{
for (z=0;z<3;z++)
{
while(PIR1bits.TX1F==0; //wait for transmit
TXREG=fname[z]; //place char in buffer
}
}
else
{
for (z=0;z<5;z++)
{
while(PIR1bits.TX1F==0; //wait for transmit
TXREG=lname[z]; //place char in buffer
}
}
while(1);
}
EX5:
• Write a program to send two different messages
“Normal” and “High” to the serial port. Assume the
switch is connected to pin PORTB.0.
• SW = 0, 9600 baud rate
• SW = 1, 38400 baud rate
• Assume XTAL = 10 MHz, with 8-bit data.
#include <P18f458.h>
#define MySW PORTBbits.RB0 //Input switch

void main(void)
{
unsigned char z;
unsigned char Mess1 [] = “Normal Speed”;
unsigned char Mess2 [] = “High Speed”;
TRISBbits.TRISB0=1; //PB5 as input;
TXSTA = 0x20 //low baud ratem 8-bit
SPBRG = 15 //9600 baud rate
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
If(MySW == 0)
{
for (z=0;z<12;z++)
{
while(PIR1bits.TX1F==0; //wait for transmit
TXREG=Mess1[z]; //place value in buffer
}
}
else
{
TXSTA=TXSTA | 0x4; //for high spee
for (z=0;z<10;z++)
{
while(PIR1bits.TX1F==0; //wait for transmit
TXREG=Mess2[z]; //place value in buffer
}
}
while(1);
}

Вам также может понравиться