1 : Save the pins of microcontroller 
 2 : Overcome the issue of interfacing with same port pins  of microcontroller

The interface between MCU and LCD is in parallel mode.  LCD  can be used in two different,  8 bit and 4-bit parallel interface modes. We are going to use LCD  in 4-bit mode to save the pin of the microcontroller

In 4bit mode, 4 data lines (D4-D7)  are used to transfer data between LCD and microcontroller.  These 4 pins can be connected to any available free pins of the microcontroller. 

In 4 bit mode, we divide the 8bit data into four 4 bits nibbles and send it to LCD. For this purpose, we are going to use the structure and union function of the c language.

typedef union 
{
struct
{
unsigned char Bit0:1;
unsigned char Bit1:1;
unsigned char Bit2:1;
unsigned char Bit3:1;
unsigned char Bit4:1;
unsigned char Bit5:1;
unsigned char Bit6:1;
unsigned char Bit7:1;
};
unsigned char Byt;
}UINT8_REG;

function to send command data to LCD in 4bit mode. 


void Send_LCD_Cmnd(unsigned char Cmnd) // send command to lcd
{
UINT8_REG lcd_cmnd = {.Byt = Cmnd};
/*upper nible of command i.e. if cmd = 0x53, high byte = 0x05 and low byte = 0x03*/
LCD_DT4 = lcd_cmnd.Bit0;
LCD_DT5 = lcd_cmnd.Bit1;
LCD_DT6 = lcd_cmnd.Bit2;
LCD_DT7 = lcd_cmnd.Bit3;

LCD_RS = 0;
Nop();Nop();
LCD_EN = 1;
delay_us(1);
LCD_EN = 0;
delay_ms(3);

/*Lower nible command*/
LCD_DT4 = lcd_cmnd.Bit4;
LCD_DT5 = lcd_cmnd.Bit5;
LCD_DT6 = lcd_cmnd.Bit6;
LCD_DT7 = lcd_cmnd.Bit7;
LCD_RS = 0;
Nop();Nop();
LCD_EN = 1;
delay_us(1);
LCD_EN = 0;
delay_ms(3);
}

We can use any pin of the microcontroller for these pins LCD_DT4, LCD_DT5, LCD_DT6, and LCD_DT7. RW pin of LCD is connected to ground or 0v physically to put LCD in writing mode always.

Responses