Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Embedded Systems Lab # 1 MTE-409

 Objective:
Write a C code to serially transfer data (MSB first 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) 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.
2) Code:

/*********************************Header Files******************************/
#define _XTAL_FREQ 20000000
#include <xc.h>
__PROG_CONFIG(1,0x3F32);
/*********************************Function declaration**********************/
unsigned char MSB_First(unsigned char Data, unsigned char Number);
void LSB_First(unsigned char Data1, unsigned char Number1);
/********************************Main Program*******************************/
void main(void)
{
unsigned char Var = 0x85;

Attique Ur Rehman (17-003) Page # 1


Embedded Systems Lab # 1 MTE-409

TRISB = 0xFC; //Making RB0, RB1 an Output, remaining pins are Input.
while(1)
{
PORTB = 0x00; //Remove any garbage value.
//Sending MSB First
for(int i = 8; i>=1;)
{
RB0 = MSB_First(Var,i);
__delay_ms(1000);
//Iteration must be done after the function calling to avoid the change
//in number of bit for which the status is being checked.
i--;
}
//After sending MSB first, wait for few seconds
__delay_ms(2000);
//Remove all previous data
PORTB = 0x00;
//Turn ON Yellow LED as an indication of successful transfer of MSB
RB1 = 1;
//Now Sending LSB First
for(int a=0; a<=7;)
{
LSB_First(Var,a);
__delay_ms(1000);
//Iteration must be done after the function calling to avoid the change
//in number of bit for which the status is being checked.
a++;
}
//After sending LSB first, wait for few seconds
__delay_ms(2000);
//Turn OFF Yellow LED as an indication of successful transfer of MSB
RB1 = 0;
}
return;
}
/****************************Function Definition****************************/
unsigned char MSB_First(unsigned char Data, unsigned char Number)

Attique Ur Rehman (17-003) Page # 2


Embedded Systems Lab # 1 MTE-409

{
unsigned char Mask = 0, Check = 0, Status = 0;//Making all the bits zero i.e 0x00000000
Check = 1 << Number; //Move 1 to the specified bit number by the main program
Status = Data & Check;
if (Status == Mask) //If true then TURN OFF the LED
{
return (0);
}
else //IF false then TURN ON the LED
return (1);
}
void LSB_First(unsigned char Data1, unsigned char Number1)
{
unsigned char Check = 0;//Making all the bits zero i.e 0x00000000
Check = Data1 & 0x01;
Data1 = Data1 >> 1; //Move 1 to the specified bit number by the main program
RB0 = Check;
}

Attique Ur Rehman (17-003) Page # 3


Embedded Systems Lab # 1 MTE-409
3) Theoretical Work:

Attique Ur Rehman (17-003) Page # 4


Embedded Systems Lab # 1 MTE-409

4) Software Implementation:

Attique Ur Rehman (17-003) Page # 5


Embedded Systems Lab # 1 MTE-409

Attique Ur Rehman (17-003) Page # 6


Embedded Systems Lab # 1 MTE-409
5) Successful building and uploading:

6) Results:

Attique Ur Rehman (17-003) Page # 7


Embedded Systems Lab # 1 MTE-409

 Procedure:
i. Firstly, we set the port direction TRISB, input or output where 0 means output and 1 means
input.
ii. Declared and initialized the variables and set the values 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 Serially LSB.
iv. Then we applied the masked gate on temp variable with 0x01 and right shifted the variable value.
v. Send this data bit by bit in pin (RB0) of microcontroller serially.

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

Attique Ur Rehman (17-003) Page # 8

You might also like