# Conversion of 8-bit data into its equivalent 4-bit data

Status
Not open for further replies.

#### imranahmed Please let me know how to convert 8-bit parallel data into 4-bit parallel data only using microcontroller?

#### iukhan Dear Imran
1) If you mean that the 8 bit parallel data is converted in two 4-bit parallel data (as required LCD in 4 bit mode) then you can bitwise AND the 8 bit byte data with 0x0F to get low nibble (data & 0x0F) and shift right 4 times to get high nibble (data>>4).
2) if you mean 4 bit equivalent data of 8 bits then just divide the data by two and you will get the equavelent.

• imranahmed

### imranahmed

points: 2

#### imranahmed Yes, i want to convert for using LCD 1) point is right .Please let me know how it is done (data & 0x0F) then (data>>4)?

#### iukhan The & (bitwise AND) operator compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's, the corresponding bit of the result is set to 1. Otherwise, it sets the corresponding result bit to 0.
so 0x0F = 0000 1111 in binary
this means that all the high nibble of data when bitwise AND will result 0 and the low nibble of the data will left as it is.

The bitwise shift operator >> move the bit values of a binary object data.
data >> 4
The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted.
e.g. if data=1011 0100 then
the result after 4 time shifting to right will 0000 1011

#### imranahmed Dear Imran
1) If you mean that the 8 bit parallel data is converted in two 4-bit parallel data (as required LCD in 4 bit mode) then you can bitwise AND the 8 bit byte data with 0x0F to get low nibble (data & 0x0F) and shift right 4 times to get high nibble (data>>4).
2) if you mean 4 bit equivalent data of 8 bits then just divide the data by two and you will get the equavelent.
Please let me know that the operations (data & 0x0F) and (data>>4) will apply on a same time or use only one.

##### Banned First you do the operation (data & 0x0F) on the data and send it to LCD port and then you do the operation (data>>4) on the data and send it to LCD port.

• imranahmed

### imranahmed

points: 2

#### iukhan Please let me know that the operations (data & 0x0F) and (data>>4) will apply on a same time or use only one.
The rule is that the higher nibble is sent first and then the lower nibble. Remember that high nibble can be achived using bitwise shif i.e. >>4.

• imranahmed

### imranahmed

points: 2

#### imranahmed Code:
#include<htc.h>
#include<pic.h>
#define _XTAL_FREQ 8000000
#define rs RE0
#define en RE1
__CONFIG(FOSC_HS);
void lcd();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
unsigned char z,d1={"Volts:"};
//unsigned char d2={"Amps:"};
void main()
{
CMCON=7;
TRISB=0;
TRISE=0;
lcd();
while(1)
{
for(z=0;z<6;z++)
lcddata(d1[z]);
//lcdcmd(0xC0);
//for(z=0;z<5;z++)
//lcddata(d2[z]);
}
}
void lcd()
{
lcdcmd(0x38);
__delay_ms(50);
lcdcmd(0x01);
__delay_ms(50);
lcdcmd(0x06);
__delay_ms(50);
lcdcmd(0x0C);
__delay_ms(50);
lcdcmd(0x80);
__delay_ms(50);
}
void lcdcmd(unsigned char value)
{
rs=0;
PORTB=value>>4;
en=1;
__delay_ms(1);
en=0;
PORTB=value&0x0f;
en=1;
__delay_ms(1);
en=0;
}
void lcddata(unsigned char value)
{
rs=1;
PORTB=value>>4;
en=1;
__delay_ms(1);
en=0;
PORTB=value&0x0f;
en=1;
__delay_ms(1);
en=0;
return;
}
Please let me know that it is not working.Please find any mistake..

Last edited by a moderator:

##### Banned Please refer to the book "PIC Microcontroller and Embedded Systems Using Assembly and C" by mazidi

#### imranahmed I tried data>>4 and data&0x0f operations but it could not work please give any hint.

#### imranahmed I wrote LCD code in MPLAB not in MikroC,but I want to send data&command through 4-bit mode.

#### iukhan • imranahmed

### imranahmed

points: 2

#### bigdogguru Please let me know that it is not working.Please find any mistake..
I would suggest for starters disabling the watchdog timer, according to your Configuration Register directive the watchdog timer is still enabled:

Code:
__CONFIG(FOSC_HS);
If the watchdog is not disabled or cleared the device will continually reset preventing anything from being displayed, regardless of the LCD code implemented.

Also remove the following as it is no longer need for recent versions of the Hitech C Compiler:

Code:
#include<pic.h>
Only the inclusion of the htc.h header file is required.

What model PIC are you currently using for this project?

Consult the device specific header file for the correct predefined macros for use in the Configuration Register compiler directive.

BigDog

• imranahmed

points: 2