You are on page 1of 6

Embedded Systems Lab # 2 MTE-409

 Objective:
Write a C code for serial reception of data (MSB and LSB first) using PIC 16F877A.

 Apparatus:
i. PIC 16F877A
ii. Resistor(1Kohm)
iii. LED.

 Softwares:
i. MPLABX IDE
ii. XC8 compiler
iii. Proteus.

 Theory:
1) Protocol:
A protocol is an agreement between two parties about how the two parties should behave. A communication
protocol is a protocol about how two parties should speak to each other. Serial communication protocols
assume that bits are transmitted in series down a single channel.
In general, serial links transmit data in distinct packets or frames. A frame is a set of bits that are transmitted
sequentially by the transmitter.
2) Serial Communication:
Serial communication is the process of sending data one bit at a time sequentially over a communication
channel or computer bus. In telecommunication and data transmission, it is viewed as a reliable data
transmission method because a data bit is only sent if the previous data bit has already been received.
When data is sent or received using serial data transmission, the data bits are organized in a specific order,
since they can only be sent one after another. The order of the data bits is important as it dictates how the
transmission is organized, when it is received.
a) Most Significant Bit/Least Significant Bit:
The MSB and LSB gives you more resolution when controlling the parameters. MSB stands for most
significant bit, while LSB is least significant bit. In binary terms, the MSB is the bit that has the greatest
effect on the number, and it is the left-most bit. For example, for a binary number 0011 0101, the Most
Significant bit would be 0 and the Least Significant bit would be 1.
3) Code:

/*********************************Header Files******************************/
#define _XTAL_FREQ 20000000
#include <xc.h>
__PROG_CONFIG (1, 0X3F32);
/***************************************************************************/

Attique Ur Rehman (17-003) Page # 1


Embedded Systems Lab # 2 MTE-409

/****************************Function Declaration***************************/
void Receive_LSB(void);
void Receive_MSB(void);
/***************************************************************************/

/*********************************Main Program******************************/
void main(void)
{
//Receive the LSBs first by calling the function
Receive_LSB();
//Wait for few seconds after receiving the LSB first
__delay_ms(2000);
//After a few delay, Receive the MSB first.
Receive_MSB();
while(1);
return;
}
/***************************************************************************/

/*********************************Function Definition************************/
void Receive_MSB(void)
{
unsigned char byte = 0x00;
unsigned char a, b, c = 0x31;
unsigned char temp = c;
TRISB = 0x01;
TRISC = 0x00;
//MSB serial interfacing
for (char i=1; i<=8; i++)
{
b = (temp >> 7) & 0x01;
temp = temp << 1;
//Send Data by MSB First
RC0 = b;
byte = byte << 1;
//Receive Data by MSB first

Attique Ur Rehman (17-003) Page # 2


Embedded Systems Lab # 2 MTE-409

byte = byte | (RB0 & 0x01);


}
}
/***************************************************************************/
void Receive_LSB(void)
{
unsigned char byte = 0x00;
unsigned char a, b, c = 0x35;
unsigned char temp = c;
TRISB = 0x01;
TRISC = 0x00;
//LSB serial interfacing
for (int i = 1; i <= 8; i++)
{
b = temp & 0x01;
temp = temp >> 1;
//Send Data by LSB First
RC0 = b;
byte = byte >> 1;
//Receive Data by LSB first
byte = byte | (RB0 & 0x01) << 7;
}
}
/***************************************************************************/

Attique Ur Rehman (17-003) Page # 3


Embedded Systems Lab # 2 MTE-409
4) Software Implementation:

Attique Ur Rehman (17-003) Page # 4


Embedded Systems Lab # 2 MTE-409

5) Successful building and uploading:

6) Results:

Attique Ur Rehman (17-003) Page # 5


Embedded Systems Lab # 2 MTE-409

 Procedure:
i. Firstly, we set the port direction TRISB and TRISC input or output, where 0 means output and 1
means input.
ii. Declared and initialized the variables and set the value of variables.
iii. For-loop are typically used when the number of iterations is known before entering the loop. Here
is 8 iteration means we transfer a byte (RC0).
iv. One time Right shift the byte variable.
v. In next line we applied masked gate on RB0 with 0x01 then 7 times left shift and OR with byte
variable and receive a byte Serially LSB(RB0) as we verify in dry run.
vi. Then we implemented this logic in proteus as shown in output.
vii. We also understand the sequence of MSB receive code as like the sequence of LSB receive as shown
in previous steps.
viii. Then implemented this logic and simulated on Proteus.

 Conclusion:
In this lab, I have explored, learned and implemented the serial data reception operations on PIC16F877A
microcontroller. I implemented serial communication in both ways, i.e. MSB first and LSB first.

Attique Ur Rehman (17-003) Page # 6

You might also like