Lab # 3
Lab # 3
Objective:
Inter-convert packed BCD to ASCII code using PIC 16F877A
Apparatus:
i. PIC 16F877A
ii. Resistor(1Kohm)
iii. LEDs
iv. Bar Graph.
Softwares:
i. MPLABX IDE
ii. XC8 compiler
iii. Proteus.
Theory:
1) Embedded System:
A system based on microcontroller/microprocessor or an FPGA which is not a computer itself but whose
sole purpose is computational. E.g. Modern Photo Copier Machine, Printer, Cell Phones etc. Embedded
electronics is all about interlinking circuits (processors or other integrated circuits) to create a symbiotic
system.
In order for those individual circuits to swap their information, they must share a common communication
protocol. Hundreds of communication protocols have been defined to achieve this data exchange, and, in
general, each can be separated into one of two categories: parallel or serial.
2) Packed BCD and Unpacked BCD:
In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal
numbers where each decimal digit is represented by a fixed number of bits. Packed BCD is the first and
second number are represented as the first 4 bits and last 4 bits in a byte. The number 75 in packed BCD
would be 01110101. Unpacked BCD is each number is represented by its own byte. The number 75 in
unpacked BCD would be 00000111 and 00000101.
3) ASCII:
ASCII abbreviated from American Standard Code for Information Interchange, is a character encoding
standard for electronic communication. ASCII codes represent text in computers, telecommunications
equipment, and other devices. Most modern character-encoding schemes are based on ASCII, although they
support many additional characters.
4) Packed BCD to ASCII:
BCD uses only the digits 0 to 9. An unpacked BCD digit uses the lower nibble of an entire byte and to
convert it to ASCII.
/*********************************Header Files******************************/
#define _XTAL_FREQ 20000000
#include <xc.h>
__PROG_CONFIG (1,0X3F32);
/***************************************************************************/
/****************************Function Declaration***************************/
void BCD_TO_ACSII(void);
void ASCII_TO_BCD(void);
/***************************************************************************/
/*********************************Main Program******************************/
void main(void)
{
//Converting Binary Coded Decimals into ASCII
BCD_TO_ASCII();
//Wait for two minutes
__delay_ms(2000);
//Converting ASCII to Binary Coded Decimals
ASCII_TO_BCD();
while(1);
return;
}
/***************************************************************************/
/*********************************Function Definition************************/
void BCD_TO_ASCII()
{
TRISB = 0x00;
TRISD = 0x00;
//Convert packed BCD into ASCII 0x32 and 0x39
unsigned char a, b;
unsigned char temp = 0x29;
// Converting into 0x39
a = (temp & 0x0F) | 0x30;
b = (temp >> 4) | 0x30;
PORTB = a;
PORTD = b;
}
/***************************************************************************/
void ASCII_TO_BCD()
{
TRISB = 0x00;
//Convert ASCII into packed BCD
unsigned char a = 0x39, b = 0x32;
//Converting into 0x09
a = a & 0x0F;
//Converting into 0x20
b = b << 4;
//Converting into 0x29
a = a | b;
PORTB = a;
}
/***************************************************************************/
6) Software Implementation:
Procedure:
i. First, I converted the packed BCD (0x29) into ASCII code (0x32,0x39).
ii. Then I declared the variables for ASCII and for packed BCD.
iii. Also I initialize the temp variable with 0x29.
iv. Then I applied the masked gate with 0x0F to convert the 0x29 into 0x09 and OR with 0x30 for
convert the 0x09 into 0x39 and stored this ASCII into the variable ‘i’.
v. Again, use the temp variable with four times right shift for convert the 0x29 into 0x02 and OR with
0x30 to convert 0x02 into 0x32 and stored this ASCII into the variable ‘j’.
vi. Then I sent the data of variables ‘i’ and ‘j’ on PORTB an PORTD of PIC16F877A.
vii. Then I Connected the LED BARGRAPH with these ports for indication of bits.
viii. Then I verified the logic in Proteus that I implemented in program.
Conclusion:
In this lab, I have explored, learned and implemented conversion of BCD to ASCII and vice versa by using
PIC16F877A microcontroller.