You are on page 1of 29

Faculty of Engineering & Information Technology

Al-AZHAR UNIVERSITY

Microcontrollers:
Programming and Applications
(ITME 4336 )

Chapter 7
Serial Communication
Lecturer: Dr. Mohammad Aqel
First Semester 2022/2023

Alazhar University Dr. Mohammad Aqel 1


GAZA First Semester 2022/2023
• Communications, in general, refers to the exchange of
information following rules, sometimes called a protocol.

• Digital and computer communications come in two types:


serial and parallel.

• In serial communications the data is sent one bit at a


time over the communications channel.

• In parallel communications all the bits that compose a


single symbol or character are sent simultaneously.

Alazhar University Dr. Mohammad Aqel 2


GAZA First Semester 2022/2023
Serial Data Transmission

• In serial communications, data are transmitted and


received in a stream of consecutive electrical pulses that
represent data bits and control codes.

• The Electronic Industries Association (EIA) has


sponsored the development of several standards for
serial communications, such as RS-232-C, RS-422, RS-
423, RS 449, EIA232E, and EIA232F, among others.

• In this designation the characters RS stand for the words


Recommended Standard.

Alazhar University Dr. Mohammad Aqel 3


GAZA First Semester 2022/2023
Asynchronous Serial Transmission
• The information in a serial bit stream is contained in a time-
dependent waveform, that is, each bit code (data, control, or error) is
transmitted for a fixed time period, known as the baud period.

• One bit is transmitted during each baud period.

• A binary 1 bit is represented with a negative voltage level and a


binary 0 bit by a positive voltage.

• The line condition during the logic 1 transmission is called a marking


state, and the a logic 0 is called a spacing state.

• The baud rate is equal to the number of bits per second being
transmitted or received.

Alazhar University Dr. Mohammad Aqel 4


GAZA First Semester 2022/2023
• Transmitter and receiver clocks should be synchronized
at the same frequency to send information bit-by-bit.
That is, both receiver and transmitter operate at the
same baud rate.

• In typical asynchronous serial communications, bits are


transmitted as separate groups, usually 7 to 10 bits long.
Each group is called a character (numeric or alphabetic
symbol).

• Each character is sent in a frame consisting of a start bit,


followed by a set of character bits, followed (optionally)
by a parity bit, and finalized by one or more stop bits.

Alazhar University Dr. Mohammad Aqel 5


GAZA First Semester 2022/2023
• The serial line is normally held marking, that is, at a logic 1 state. The
change from logic high to logic low, signaled by the start bit, tells the
receiver that a frame follows.
• The receiver reads the number of character bits expected according to
the adopted protocol until a logic high, represented by one or more stop
bits, marks the end of the frame.

• Figure 14-1 shows the different elements in a serial communications bit


stream.

Alazhar University Dr. Mohammad Aqel 6


GAZA First Semester 2022/2023
• The transmitter holds the line to logic high (marking
state) until it is ready to send.

• The start bit (spacing state) is used to signal the start of


a new character.

• The start bit is also used by the receiver to synchronize


with the transmitter.

• This form
of transmitting serial data is called
asynchronous because the receiver resynchronizes itself
to the transmitter using the start bit of each frame.

Alazhar University Dr. Mohammad Aqel 7


GAZA First Semester 2022/2023
Synchronous Serial Transmission
• An alternative approach to asynchronous serial data transmission is
one in which the characters are sent in blocks with no framing bits
surrounding them.

• In asynchronous communications, each character is framed by a


start and a stop signal so that the receiver can know exactly where
the character bits are located.

• In synchronous communications, the sender and receiver are


synchronized with a clock.

• This scheme assumes that a separate line (or wire) is used for the
clock signal.

• Alternatively, a clock line can be used to synchronize the moment in


time at which the receiver reads the data line.

Alazhar University Dr. Mohammad Aqel 8


GAZA First Semester 2022/2023
• Most legacy PC communications systems are asynchronous,
although the EIA232F standard supports both synchronous and
asynchronous methods.

• The most common chip used in PC communications is the UART


(Universal Asynchronous Receiver and Transmitter).

• An alternative chip called the USRT is used for synchronous


communications and the USART (Universal
Synchronous/Asynchronous Receiver and Transmitter) supports
both.

• The 16F877 PIC has two serial communication modules. One of


them is the Master Synchronous Serial Port, or MSSP. The other
one is a USART.

Alazhar University Dr. Mohammad Aqel 9


GAZA First Semester 2022/2023
The RS-232-C Standard
• Today, RS-232-C is gradually being replaced by
USB for local communications.

• USB is faster, has lower voltage levels, and uses


smaller connectors that are easier to wire. USB
is a more complex standard, requiring more
complex software.

• Furthermore, serial ports are used to directly


control hardware devices, such as relays and
lamps, since the RS-232-C control lines can be
easily manipulated by software. This is not
feasible with USB.
Alazhar University Dr. Mohammad Aqel 10
GAZA First Semester 2022/2023
• Essential Concepts

• The RS-232-C convention specifies that, with respect to ground, a voltage


more negative than -3 V is interpreted as a 1 bit and a voltage more positive
than +3 V as a 0 bit.
• Serial communications, according to RS-232-C, require that transmitter and
receiver agree on a communications protocol. The following terminology refers
to the RS-232-C communications protocol:
• Baud period: The rate of transmission measured in bits per second, also called the
baud rate

• Marking state: The time period during which no data is transmitted. During the
marking period, the line condition is logic 1.

• Spacing state: The time period during which data is transmitted. During the spacing
period, the line condition is logic 0.

• Start bit: The transition that indicates that data transmission is about to start. The
change from logic high to logic low is the start bit, tells the receiver that a frame
follows.

• Character bits: The data stream composed of 5, 6, 7, or 8 bits that encode the
character transmitted. The least significant bit is the first one transmitted.

Alazhar University Dr. Mohammad Aqel 11


GAZA First Semester 2022/2023
• Parity bit: An optional bit, transmitted following the character bits, used in
checking for transmission errors.
If even parity is chosen, the transmitter sets or clears the parity bit so as to
make the sum of the character’s 1 bits and the parity bit an even number.
In odd parity, the sum of 1 bits is an odd number. If parity is not correct, the
receiver sets an error flag in a special register.
• Stop bits: One or more logic high bits inserted in the stream following the
character bits or the parity bit, if there is one. The stop bit or bits ensure that
the receiver has enough time to get ready for the next character.
• Half-duplex: A system that allows serial communications in both directions,
but only one direction at a time.

in Half-duplex radio communications one user says the word “Over” to


indicate the end of transmission.
• Full-duplex: A full-duplex system allows communication in both directions
simultaneously.

Alazhar University Dr. Mohammad Aqel 12


GAZA First Semester 2022/2023
• The Serial Bit Stream

• In the RS-232-C protocol, the transmission/reception


parameters are selected from a range of standard values.

• The following are the most common ones:


– Baud rate: 50, 110, 300, 600, 1200, 2400, 4800, 9600,
and 19200
– Data bits: 5, 6, 7, or 8.
– Parity bit: Odd, even, or no parity.
– Stop bits: 1, 1.5, or 2.

Alazhar University Dr. Mohammad Aqel 13


GAZA First Semester 2022/2023
Connectors and Wiring

• The RS-232-C standard requires specific hardware connectors with either


25 or 9 pins.
• The 25-pin connector is called a D-shell connector, or DB-25.
• The connector with 9 pins is called the 9-pin D-shell connector or DB-9.
• In addition, the RJ-45 connector (the name stands for Registered-Jack 45)
is used for twisted-pair cables. A common application of RJ-45 connectors
is in Ethernet cables.
• Figure 14-2 shows the male DB-25, DB-9, and the female RJ-45
connectors.

Alazhar University Dr. Mohammad Aqel 14


GAZA First Semester 2022/2023
• The function assigned to each pin varies in the
common connectors.

• Table 14.1 lists the assignation of the RS-232-C


lines in the different hardware.

Alazhar University Dr. Mohammad Aqel 15


GAZA First Semester 2022/2023
PIC “Free Style” Serial Programming
• The following example of free-style communications systems does not
conform to any particular protocol or standard. This style is adequate for
stand-alone applications and circuits. :

• A PIC to PIC communications circuit and program. Two programs are


required: one for the sender or master PIC and one for the receiver or
slave PIC.

• In this case, one PIC acts as a sender, or master, and the other one as a
receiver or slave, although it is also possible for sender and receiver to
exchange roles.

• Consider a circuit in which one PIC polls the state of switches and then
sends the result serially to a second PIC that controls the states of LEDs
to be lighted according to the switch settings.

• The reason for this circuit is that some PICs may not have a sufficient
number of ports to monitor eight switches and control eight LEDs.

Alazhar University Dr. Mohammad Aqel 16


GAZA First Semester 2022/2023
PIC-to-PIC Serial Communications
Circuits
• Actually, the system requires two separate PIC-based circuits, one
PIC reading data (the state of the eight DIP switches) and serially
sending the data to another PIC that outputs and displays the
results.

• Following Figure shows the two PIC-based circuits.

• The top circuit contains eight DIP switches wired to ports RB0 to
RB7.

• A pushbutton switch is wired to port RA2. The serial output is


through port RA0.

• The second circuit at the bottom of the figure has eight LEDs wired
to ports RB0 to RB7. Input into the circuit is through port RA0.

• We refer to the top circuit as the sender circuit and PIC and the one
on the bottom as the receiver circuit and PIC.
Alazhar University Dr. Mohammad Aqel 17
GAZA First Semester 2022/2023
Alazhar University Dr. Mohammad Aqel 18
GAZA First Semester 2022/2023
PIC-to-PIC Serial Communications
Programs
• The Sender presses the switch labeled “send ready” in the sender
circuit. At that time, the program in the sender reads the state of the
DIP switches and sends the data out, one bit at a time, through the
serial line connected between two PIC circuit.

• The receiver reads the eight bits on the serial line and lights the
LEDs accordingly.

• The software consists of two different programs, one to run in the


sender PIC and one in the receiver PIC.

• Asynchronous communications require that sender and


receiver operate at the same data speed. Both devices need not
run at the same clock speed, but both must synchronize data
transmission and reception at the same clock rate.

Alazhar University Dr. Mohammad Aqel 19


GAZA First Semester 2022/2023
• Since the easiest way to synchronize data transmission and reception at the
same clock rate, is to have both PICs use the same oscillator at the same
speed, we make this assumption in the programs that follow.

– Associate a high voltage with a logic 1 and a low voltage with a logic 0.
– we will follow the character structure from the RS-232-C convention, that is,
information will contain a start bit, a series of eight data bits, and a stop bit.
– No parity is implemented.

• Figure 14-8 shows the bit structure for one character in this application.

Alazhar University Dr. Mohammad Aqel 20


GAZA First Semester 2022/2023
• The sender program, performs the following initialization
operations:

1. Line RA2 is initialized for input since the pushbutton switch


is located on this line. Lines RB0 to RB7 are also input,
since they are connected to the DIP switch array.

• Once initialized, the program performs the following


functions:

1. Code monitors the SEND pushbutton switch.


2. The state of the DIP switches is obtained by reading
RB0 to RB7.
3. The byte from Port-B is sent through the serial line.

• Each bit (start, data, and stop bits) is sent at a rate of 1ms.
That is, each bit is held high or low for one millisecond.
Alazhar University Dr. Mohammad Aqel 21
GAZA First Semester 2022/2023
int i;
int Data_bit;
Sender Program
void main() {
TRISA=0b00000100;
TRISB=0xFF;

PORTA=0;
PORTB=0;

while(1){
START:
PORTA.F0=1; //MARKING BIT on Serial line

if(PORTA.F2==1) goto START; // WAIT TILL PRESSING PUSHBUTTON


delay_ms(30); // DEBOUNCING TIME DELAY for pushbutton

Data_bit=PORTB; //READ DIP SWITCH VALUES

SENDDATA:
PORTA.F0=0 ; //Send start bit on serial line
Delay_ms(1); //BAUD PERIOD 1ms

SEND8BITS:
for(i=0; i<=7; i++) {
PORTA.F0=Data_bit>>i;
Delay_ms(1); //BAUD PERIOD 1ms
}

PORTA.F0=1 ; //Marking state as stop bit on serial line


Delay_ms(1); //BAUD PERIOD 1ms
//END OF TRANSMISSION

}
}
Alazhar University Dr. Mohammad Aqel 22
GAZA First Semester 2022/2023
• The receiver program, runs in the receiver PIC. In this case, the
serial line is RA0. Input from the sender program is received
through this line.

• Lines RA0 is initialized for input since the RA0 is the serial input
line. Lines RB0 to RB7 are output since they are wired to the eight
LEDs.

• Once initialized, code performs the following functions:

1. Code monitors the serial line for the first low that indicates the
leading edge of the start bit.

2. Once the start bit is detected, code waits for 0.5ms to locate the
center of the start bit. This synchronizes the receiver with the
sender and accommodates small timing errors.

3. The eight data bits are then received and stored.

4. After waiting for the stop bit, code sets the eight LEDs according to
the data received through the serial line.
Alazhar University Dr. Mohammad Aqel 23
GAZA First Semester 2022/2023
int i;
Receiver Program
int Received_Data[8];
void main() {
TRISA=0b00000001;
TRISB=0;

PORTB=0;

while(1){
START:

if (PORTA.F0==0) { //RECEIVE start bit


Delay_us(500); // Delay 0.5 ms

for (i=0; i<=7; i++){


Delay_ms(1);
Received_Data[i]=PORTA.F0;

portb=0;

for (i=0; i<=7; i++){ //DISPLAY THE RECEIVED DATA ON PORTB TO


//PORTB.0[i]= Received_Data[i];

portb=portb + (Received_Data[i]<<i) ;

Delay_ms(1);

} //END OF RECEIVING THE 8 BIT CHARACTER BIT


}
}

Alazhar University Dr. Mohammad Aqel 24


GAZA First Semester 2022/2023
PIC Protocol-based Serial Programming
• PIC-based circuits sometimes communicate with systems that
conform to a specific communications standard, for example, with a
PC through its RS-232-C serial port.

• In this case, the PIC software and hardware must conform with the
RS232 protocol.

• For instance, the 16F87X PIC family includes an MSSP (Master


Synchronous Serial Port) module and a USART (Universal
Synchronous/asynchronous Receiver and Transmitter) module.

• The 16F877 circuits and applications in the this session use the
processor’s USART module is also known as a Serial
Communications Interface, or SCI.

• The USART module is useful in communicating with devices and


systems that support RS-232-C communications, including
computers and terminals.
Alazhar University Dr. Mohammad Aqel 25
GAZA First Semester 2022/2023
RS-232-C Communications on the 16F87x
• The easiest way to transfer data between the microcontroller and
some other device (for example, PC or another microcontroller) is
via RS-232 communication port.

• This type of communication provides serial asynchronous data


transfer over 2 lines (Tx for transmitting and Rx for receiving) within
10m range.

• MAX232 is used in RS232 serial interface to adjust signal voltage


levels on the microcontroller side, i.e. to convert RS-232 voltage
levels +/- 10V to TTL levels 0-5V and vice versa).

• Transfer format is 8 data bits, no parity bit and one stop bit, while
transfer rate is 2400 bauds.

Alazhar University Dr. Mohammad Aqel 26


GAZA First Semester 2022/2023
Alazhar University Dr. Mohammad Aqel 27
GAZA First Semester 2022/2023
miKroC Serial Program
Hardware_UART Module
char uart_rd;

void main() {
UART1_Init(2400); // Initialize UART module at 2400 bps
Delay_ms(100); // Wait for UART module to stabilize

UART1_Write_Text("Start");
UART1_Write(10); // a line feed moves the cursor on a display screen down one line.
UART1_Write(13); // a carriage return moves the cursor to the beginning of the current line

while (1) { // Endless loop


if (UART1_Data_Ready()) { // If data is received,
uart_rd = UART1_Read(); // read the received data,
UART1_Write(uart_rd); // and send data via UART
}
}
}

Alazhar University Dr. Mohammad Aqel 28


GAZA First Semester 2022/2023
miKroC Serial Program
char i, error, byte_read; // Auxiliary variables

void main(){
TRISD=0; // Set PORTB as output (error signalization)
TRISB = 0x00;
PORTB = 0;
PORTD=0; // No error

error=Soft_UART_Init(&PORTC, 7, 6, 2400, 0); // Initialize Soft UART at 2400 bps

if (error > 0) {
PORTD = error; // Signalize Init error
while(1) ; // Stop program
}

Delay_ms(100);

for (i = 'z'; i >= 'A'; i--) { // Send bytes from 'z' downto 'A'
Soft_UART_Write(i);
Delay_ms(100);
}

while(1) { // Endless loop


byte_read = Soft_UART_Read(&error); // Read byte, then test error flag
if (error) // If error was detected
PORTD = error; // signal it on PORTB
else
Soft_UART_Write(byte_read); // If error was not detected, return byte read
PORTB= byte_read;
}
}

Alazhar University Dr. Mohammad Aqel 29


GAZA First Semester 2022/2023

You might also like