Professional Documents
Culture Documents
Al-AZHAR UNIVERSITY
Microcontrollers:
Programming and Applications
(ITME 4336 )
Chapter 7
Serial Communication
Lecturer: Dr. Mohammad Aqel
First Semester 2022/2023
• The baud rate is equal to the number of bits per second being
transmitted or received.
• This form
of transmitting serial data is called
asynchronous because the receiver resynchronizes itself
to the transmitter using the start bit of each frame.
• This scheme assumes that a separate line (or wire) is used for the
clock signal.
• 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.
• 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.
• The top circuit contains eight DIP switches wired to ports RB0 to
RB7.
• 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.
– 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.
• 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
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
}
}
}
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.
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.
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:
portb=0;
portb=portb + (Received_Data[i]<<i) ;
Delay_ms(1);
• In this case, the PIC software and hardware must conform with the
RS232 protocol.
• 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.
• Transfer format is 8 data bits, no parity bit and one stop bit, while
transfer rate is 2400 bauds.
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
void main(){
TRISD=0; // Set PORTB as output (error signalization)
TRISB = 0x00;
PORTB = 0;
PORTD=0; // No error
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);
}