Uploaded by Noor Shazliza

LECTURE 10 - Serial Communication

advertisement
EET203 MICROCONTROLLER
SYSTEMS DESIGN
Serial Port Interfacing
Objectives
¨
¨
¨
¨
¨
Explain serial communication protocol
Describe data transfer rate and bps rate
Describe the main registers used by serial
communication of the PIC16
Program the PIC16 serial port in C language
Interface the PIC16 with an RS232 connector
Communication
There are two options to differentiate when speaking about transmission of
information on the transmission lines:
Serial Communication
Parallel Communication
Types of Communication
In addition to the serial and parallel communications, there are 2 types of
communication we will explore:
Synchronous communication
When using the synchronous communication – the information is transmitted from
the transmitter to the receiver:
• in sequence
• bit after bit
• with fixed baud rate
• and the clock frequency is transmitted along with the bits
That means that the transmitter and the receiver are synchronized between them
by the same clock frequency. The clock frequency can be transmitted along with
the information, while it is encoded in the information itself, or in many cases there
is an additional wire for the clock.
This type of communication is faster compare to the asynchronous communication
since it is "constantly transmitting” the information, with no stops.
Types of Communication
Asynchronous communication
When using the asynchronous communication - the transmitter and the receiver
refraining to transmit long sequences of bits because there isn't a full
synchronization between the transmitter, that sends the data, and the receiver,
that receives the data.
In this case, the information is divided into frames, in the size of byte. Each one of
the frame has:
• “Start” bit marks the beginning of a new frame.
• “Stop” bit marks the end of the frame.
Frames of information must not necessarily be transmitted at equal time space,
since they are independent of the clock.
Serial Communication - USART
To communicate with external components such as computers or microcontrollers,
the PIC micro uses a component called USART - Universal Synchronous
Asynchronous Receiver Transmitter. This component can be configured as:
• a Full-Duplex asynchronous system that can communicate with peripheral
devices, such as CRT terminals and personal computers
• a Half-Duplex synchronous system that can communicate with peripheral
devices, such as A/D or D/A integrated circuits, serial EEPROMs, etc.
Serial Communication - USART
¨
¨
Only asynchronous mode and full-duplex serial data
communication will be covered.
In PIC uC, six major registers are associated with the USART
(universal synchronous asynchronous receiver):
¤
¤
¤
¤
¤
¤
SPBRG: Serial Port Baud Rate Generator
TXREG: Transfer Register
RCREG: Receive Register
TXSTA: Transmit Status and Control Register
RCSTA: Receive Status and Control Register
PIR1: Peripheral Interrupt Request Register1
USART -Transmit Block Diagram
USART - Receive Block Diagram
Baud Rate Calculation
¨
¨
¨
¨
¨
¨
BAUD -baud rate
bps -units in which we are
measuring pace of transmission
PIC transfers and receives data
serially at many different baud
rates.
The baud rate in the PIC16 is
programmable
The value loaded into the 8-bit
register SPBRG decides the
baud rate
Depend on crystal frequency
SPBRG SPBRG
(Hex
(Hex
Baud Rate Value) Value)
XTAL =
10MHz
38400
19200
3
7
9600
4800
2400
1200
F
20
40
81
XTAL =
20MHz
Baud Rate Calculation
Desired Baud Rate = Fosc/64 (X+1)
X: value loaded in SPBRG reg.
XTAL
oscillator
Example:
10 MHz
20 MHz
÷4
Instruction cycle freq
2.5 MHz
5.0 MHz
÷ 16
by UART
To UART to set
the baud rate
156,250 Hz
312500 Hz
Desired Baud Rate = Fosc/64 (X+1)
X = (Fosc/(64*Desired Baud Rate)) – 1
Note:
Dividing the instruction cycle frequency by 16 is the setting upon Reset.
Can get a higher baud rate by making bit BRGH = 1 in TXSTA register
Baud Rate Calculation
With Fosc = 10 MHz, find the SPBRG value needed to have
the following baud rates:
(a) 9600 (b) 4800
(c) 2400
(d) 1200
Solution:
Desired Baud Rate = Fosc/64 (X+1)
X = (10MHz/(64*Desired Baud Rate)) – 1
(a) X = (10M/(64*9600)) – 1 = 15.28 = 15 = F (hex)
(b) X = (10M/(64*4800)) – 1 = 31.55 = 32 = 20 (hex)
(c) X = (10M/(64*2400)) – 1 = 64.10 = 64 = 40 (hex)
(d) X = (10M/(64*1200)) – 1 = 129.21 = 129 = 81 (hex)
Baud Rate Calculation
With Fosc = 20 MHz, find the SPBRG value needed to have
the following baud rates:
(a) 9600 (b) 4800
(c) 2400
(d) 1200
Solution:
Desired Baud Rate = Fosc/64 (X+1)
X = (Fosc/(64*Desired Baud Rate)) – 1
(a) X =
(b) X =
(c) X =
(d) X =
Quadrupling the baud rate in PIC16
¨
There are two ways to increase the baud rate of
data transfer in PIC16:
1.
2.
¨
Use a higher frequency crystal à not feasible!
Change a bit in TXSTA register
Option 2 can be used while the crystal frequency
stays the same.
Change bit of TXSTA<2>: BRGH bit
¤ Upon Reset, BRGH = 0 à Low Speed
¤ When BRGH = 1
àHigh Speed
àBaud rate is quadrupled with this method!
¤
Quadrupling the baud rate in PIC16
(cont’d)
¨
Baud rates for BRGH = 0
Default when PIC is powered up.
¤ PIC divides Fosc/4 by 16 once more.
¤ Use that frequency for UART to set the baud rate.
¤ Example: XTAL = 10 MHz
Instruction cycle freq = 10 MHz/4 = 2.5 MHz
And
This is the frequency used by
UART to set the baud rate
2.5 MHz/16 = 156,250 Hz
for BRGH = 0
¤
¨
The baud rate for BRGH = 0 was discussed in
previous slides.
Quadrupling the baud rate in PIC16
(cont’d)
¨
Baud rates for BRGH = 1
With fixed crystal frequency, the baud rate can be
quadrupled
¤ PIC divides Fosc/4 by 4 once more.
¤ Use that frequency for UART to set the baud rate.
¤ Example: XTAL = 10 MHz
Instruction cycle freq = 10 MHz/4 = 2.5 MHz
And
This is the frequency used by
UART to set the baud rate
2.5 MHz/4 = 625,000 Hz
¤
for BRGH = 1
Quadrupling the baud rate in PIC16
(cont’d)
X = (Fosc/(16*Desired Baud Rate)) – 1
X: value loaded in SPBRG reg.
SPBRG Values for Various Baud Rates
(BRGH = 1)
Baud Rate
SPBRG (Hex Value)
XTAL = 10MHz
57600
38400
19200
9600
4800
0A
0F
20
40
81
SPBRG (Hex Value)
XTAL = 20MHz
Quadrupling the baud rate in PIC16
(cont’d)
For XTAL = 10 MHz, find the SPBRG value (in both decimal and hex) to set
the baud rate to each of the following:
(a) 9600 if BRGH = 1
(b) 4800 if BRGH = 1
Solution:
XTAL = 10 MHz, Fosc/4 = 2.5 MHz,
X = (fosc/(16*Desired Baud Rate)) – 1
BRGH = 1 à UART frequency = 2.5 MHz/4 = 625,000 Hz
(a) X = (625,500/9600) – 1 = 64 D = 40 H
(b) X = (625,500/4800) – 1 = 129 D = 81 H
BRGH = 1
÷4
XTAL
oscillator
Example:
10 MHz
÷4
Instruction cycle freq
2.5 MHz
BRGH = 0
÷ 16
625,500 Hz
To
UART
to set
the
baud
rate
156,250 Hz
Quadrupling the baud rate in PIC16
(cont’d)
For XTAL = 10 MHz, find the SPBRG value (in both decimal and hex) to set
the baud rate to each of the following:
(a) 9600 if BRGH = 1
(b) 4800 if BRGH = 1
Solution:
XTAL = 20 MHz, Fosc/4 = 5.0 MHz,
X = (fosc/(16*Desired Baud Rate)) – 1
BRGH = 1 à UART frequency = 5.0 MHz/4 = 1,250,000 Hz
(a) X =
(b) X =
BRGH = 1
÷4
XTAL
oscillator
Example:
20 MHz
÷4
Instruction cycle freq
5 MHz
BRGH = 0
÷ 16
1,250,000 Hz
To
UART
to set
the
baud
rate
312,500 Hz
TXREG Register
¨
¨
¨
¨
8-bit register used for serial communication in the PIC16
For a byte of data to be transferred via the TX pin, it must be
placed in the TXREG register
The moment a byte is written into TXREG, the 8-bit data is
framed with the START and STOP bits.
The 10-bit data is transferred serially via TX pin.
TXSTA Register
¨
¨
¨
¨
¨
Transmit Status and Control Register
8-bit register
Used to select the synchronous/asynchronous modes
and data framing size.
BRGH bit: select a higher speed for transmission.
Default is lower baud rate transmission
D6 of TXSTA: determines the framing of data by
specifying the number of bits per character
RCREG Register
¨
¨
¨
8-bit register used for serial communication in the PIC16
When the bits are received serially via the RX pin, the PIC16
de-frames them by eliminating the START and STOP bit.
Making a byte out of data received and then placing in the
RCREG register
RCSTA Register
¨
¨
¨
Receive Status and Control Register
8-bit register
Used to enable the serial port to receive data.
PIR1 Register
¨
¨
¨
¨
¨
Peripheral Interrupt Request Register 1
8-bit register
TWO (2) of PIR1 register bits are used by UART:
¤ TXIF (transmit interrupt flag)
¤ RCIF (receive interrupt flag)
We monitor (poll) the TXIF flag bit to make sure that all the
bits of the last byte are transmitted before we write another
byte into TXREG.
By the same logic, we monitor the RCIF flag bit to see if a byte
of data has come in yet.
PIR1 Register
PIR1 Register (cont’d)
Programming the PIC16 to Transfer
Data Serially
1. Load TXSTA register = 20H
à Indicating asynchronous mode with 8-bit data frame,
low baud rate and transmit enabled
2. Make TX pin (PORTC6) an output
3. Load SPBRG to set the baud rate
4. Enabled the serial port: RCSTA<7> (SPEN = 1)
5. Character byte to be transmit is written into TXREG
6. Keep monitor TXIF flag bit of PIR1 register to make
sure UART is ready for next byte
7. To transmit next character, go to Step 5
Example 1
Write a C program for the PIC16 to transfer the letter 'G'
serially at 9600 baud continuously.
Assume XTAL = 20 MHz
void main (void)
{
TRISC = 0x00;
// TX pin (PORTC6) as output
TXSTA = 0x20;
// 0b00100000; choose low baud rate, 8-bit, asynchronous mode
SPBRG = 32;
// or 0x20; 9600 baud rate, XTAL = 20 MHz
TXEN = 1;
// enable transmit
SPEN = 1;
// enable serial port
while(1)
{
}
}
TXREG = ‘G’;
// place value in buffer
while(TXIF==0);
// wait until transmitted
Example 2
Write a C program to transfer the message “YES” serially
at 9600 baud, 8-bit data, and 1 STOP bit. Do this
continuously. Assume XTAL = 20 MHz
// PIC configuration
void SerTx(unsigned char);
// function for sending character
void main(void)
{
TRISC = 0x00;
// TX pin (PORTC6) as output
TXSTA = 0x20;
// 0b00100000; choose low baud rate, 8-bit, asynchronous mode
SPBRG = 32;
// or 0x20; 9600 baud rate, XTAL = 20 MHz
TXEN = 1;
// enable transmit
SPEN = 1;
// enable serial port
while(1)
{
SerTx(‘Y’);
SerTx(‘E’);
SerTx(‘S’);
}
}
void SerTx(unsigned char c)
{
while(TXIF==0);
TXREG = c;
}
// wait for transmitted
// place character in buffer
Example 2 (cont’d)
// PIC configuration
void main(void)
{
TRISC = 0x00;
// TX pin (PORTC6) as output
TXSTA = 0x20;
// 0b00100000; choose low baud rate, 8-bit, asynchronous mode
SPBRG = 32;
// or 0x20; 9600 baud rate, XTAL = 20 MHz
TXEN = 1;
// enable transmit
SPEN = 1;
// enable serial port
unsigned char z;
unsigned char Mess[] = “YES”;
while(1)
{
for (z=0; z<3; z++)
{
while(TXIF==0);
TXREG = Mess[z];
}
}
}
// write message
// wait for transmit
// place char in buffer
Example 3
Write a C program to send the two messages “Normal Speed” and “High
Speed” to the serial port. Assuming that SW is connected to pin PORTB0,
monitor its status and set the baud rate as follows:
SW = 0 : 9600 baud rate
SW = 1 : 38400 baud rate
Assume that XTAL = 20 MHz for both cases.
Solution:
BRGH = 0
BRGH = 1
UART freq = 312500 Hz
UART freq = 1,250,000 Hz
X = (312500/9600) - 1 = 32 D
X = (625,000/38400) - 1 = 32 D
Example 3 (cont’d)
// PIC configuration
#define SW RB0;
// PORTB0 as input switch SW
void main (void)
{
TRISB = 0x01;
// PORTB0 as input switch
TRISC = 0x00;
// TX pin (PORTC6) as output
TXSTA = 0x20;
// 0b00100000; choose low baud rate, 8-bit, asynchronous mode
SPBRG = 32;
// 9600 baud rate, XTAL = 20 MHz
TXEN = 1;
// enable transmit
SPEN = 1;
// enable serial port
unsigned char z;
unsigned char Mess1[] = “Normal Speed”;
unsigned char Mess2[] = “High Speed”;
Example 3 (cont’d)
if (SW==0)
{
for (z=0; z<12; z++)
{
while(TXIF==0);
TXREG = Mess1[z];
// wait for transmit
// place value in buffer
}
}
else
{
TXSTA = TXSTA | 0x04;
// for high speed (BRGH=1)
for (z=0; z<10; z++)
{
while(TXIF==0);
TXREG = Mess2[z];
}
}
while(1);
}
// stay in loop
// wait for transmit
// place value in buffer
Programming the PIC16 to Receive
Data Serially
1. Load RCSTA register = 90H:
à To enable the continuous receive in addition to the 8-bit
data size option
2. Load TXSTA register = 00H:
à To choose the low baud rate option
3. Load SPBRG to set the baud rate
4. Make RX pin (PORTC7) an input
5. Keep monitor RCIF flag bit of PIR1 register for a HIGH to see
if an entire character has been received yet
6. When RCIF = 1, RCREG has a byte, its contents are moved
into a safe place
7. To receive next character, go to step 5
Example 4
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.
// PIC configuration
void main(void)
{
TRICS = 0x00;
// set all pin at port B as output
TRISC = 0x80;
// RX pin (PORTC7) as input, TX pin (PORTC6) as output
RCSTA = 0x90;
// 0b1001000; Usart Enable, Continuous receive enable
TXSTA = 0x00;
// 0b00000000; choose low baud rate, 8-bit, asynchronous mode
SPBRG = 32;
// or 0x20; 9600 baud rate, XTAL = 20 MHz
TXEN = 0;
// disable transmit
SPEN = 1;
// enable serial port
unsigned char z;
while(1)
{
while(RXIF==0);
}
}
// wait for receive
z = RCREG;
// read data
PORTB = z;
// send data to port B
Hardware Interfacing
To communicate over UART or USART, we just need three basic signals
which are namely,
i) RXD (receive)
ii) TXD (transmit)
iii) GND (common ground)
Hardware Interfacing
Summary
¨
Serial Communication- data is sent one bit a time, is used in situations where
data is sent over significant distances because in parallel communications,
where data is sent a byte or more a time, great distances can cause
distortion of the data.
¨
Additional advantage- allowing transmission over phone lines
¨
Asynchronous communication - data is sent one byte at a time and
Synchronous communication - data is sent in blocks of bytes
¨
Simplex - can sent but cannot receive,
Half duplex - can send and receive, but not at the same time and
Full duplex - can send and receive at the same time
¨
RS232 - standard foe serial communication connectors
End of Chapter
“Life is not so short but that there is always time enough for courtesy” – Quote by
Ralph Waldo Emerson
“A river cuts the rock not because of its power but because of its consistency.
Never lose your hope and keep walking towards your vision!”
Download