16×2 LCD interfacing in 4 bit mode

Hello there!! After trying many times I finally managed to interface LCD in 4 bit mode. I’m writing this post as soon as I got the output and understood what was going wrong. First things first, if you are reading this article directly without knowing about 8 bit mode interfacing then you should read my blog post on 8 bit LCD interfacing. Assuming that you know what 8 bit mode is, let’s begin with the 4 bit mode interfacing.

Need for 4 bit mode

Well for interfacing anything with any processor we need system-bus (data-bus, address-bus and control-bus). In our case for 8 bit mode the 8 data pins (D0-D7) are the data and address bus while the 3 control pins(RS, R/W and E) are the control bus. Thus using these we can control the peripheral that we are interfacing. We are greedy so we want to interface as many peripherals as possible with the same microcontroller. This requires either large number of ports or we need to be smart and utilize what we have to the fullest. Thus first thing is we try to do is reduce the number of pins required for controlling the peripheral. Here comes the need for 4 bit mode. Thus we reduce the port pins required from 11 to 7. It might not seem much, but for a small microcontroller like msp430g2553 with limited port pins this is really a big amount. Now coming to the other method. Maybe we can use demultiplexing , in this way we can use ‘n’ lines to share the system bus with ‘2^n’ devices. I got one tip that we can use SIPO shift register for sending data. Now this will require only 5 port pins. Three control pins and two for serial data and clock.

4 bit mode working

In 4 bit mode we send the data nibble by nibble, first upper nibble and then lower nibble. For those of you who don’t know what a nibble is: a nibble is a group of four bits, so the lower four bits (D0-D3) of a byte form the lower nibble while the upper four bits (D4-D7) of a byte form the higher nibble. This enables us to send 8 bit data be it the ASCII code or the command code by using 4 pins instead of 8. The connections remain identical. The only change is that the lower nibble pins of LCD are unused.

Initializing the LCD in 4 bit mode

This is perhaps the most tricky part of this interfacing. When LCD is powered ON it is by default in 8 bit mode.


This is the command code format for function set operation. Using this we form the command code for 4 bit mode operation.

upper part


The function set bits are explained in the following section.


We need 4 bit mode so make DL ‘0’. Thus the upper nibble of the command code is 0010b which is 0x02. If we need 2 lines we set N and the normal font so F is 0 thus lower nibble comes out to be 1000b i.e 0x08. Thus total command code is 0x28.

Before sending this 0x28 we need to perform a specific initialization.


So we send 0x33 as the command code. This will do the initial 8 bit mode starting of LCD. Now after this we need to send 0x32.(Note : We’re using the nibble method so what will go for 0x33 is 3 followed by 3 and for 0x32 is 3 followed by 2.) I was not able to do the initialization. But then I suddenly remembered about a workshop that I had attended on msp430. The professor had taught LCD interfacing in 4 bit mode but due to shortage of time could not go into the details. He had just told about the nibble sending part and that we need to send command codes to initialize. So I opened his file and saw this 0x33 and 0x32 and tried sending it. And to my surprise I got my name on the LCD. So this credit goes to him. His name is Gurjeet Singh Gill.

Now coming back to the topic at hand. So now we have initialized the LCD in 4 bit mode. All we need to learn is how to send the value nibble by nibble in c and without affecting other port pins except the ones that we use for sending data. (In assembly its just the rotate instruction.)

Nibble sending logic

P1OUT = (P1OUT & 0xF0)|((data>>4) & 0x0F); // send higher nibble

In this we make use of masking concept and logical shift operation in c. Now in my program I’m using P1.0 – P1.3 pins as the data pins. Thus I’ll explain the logic accordingly. Here data is the parameter that is passed in the function. I shift the data right by four bits, thus bringing the higher nibble in the lower nibble location. Mask the upper part. Then I make the P1OUT lower nibble 0 so that ‘or’ing with nibble data will give nibble data on P1OUT pin. (x and 0 is 0 ; x and 1 is x; x or 1 is 1 and x or 0 is x—-> (x&0)|(data_bit)= 0|data_bit= data_bit. Thus we get data bit on the port pin without affecting P1.4-P1.7) This takes care of upper nibble.

P1OUT = (P1OUT & 0xF0)|(data & 0x0F); // send lower nibble

Now lower nibble involves the same operations except the shifting operation.


Header file :

// Author : Manpreet Singh Minhas
// This file is for 4 bit mode LCD interfacing with msp430g2553 chip
// 16x2 LCD is used
#include <msp430g2553.h>
#define DR P1OUT = P1OUT | BIT4 // define RS high
#define CWR P1OUT = P1OUT & (~BIT4) // define RS low
#define READ P1OUT = P1OUT | BIT5
// define Read signal R/W = 1 for reading
#define WRITE P1OUT = P1OUT & (~BIT5)
// define Write signal R/W = 0 for writing
// define Enable high signal
#define ENABLE_LOW P1OUT = P1OUT & (~BIT6)
// define Enable Low signal
unsigned int i;
unsigned int j;
void delay(unsigned int k)
for(i=0;i<100;i++); } } void data_write(void) { ENABLE_HIGH; delay(2); ENABLE_LOW; } void data_read(void) { ENABLE_LOW; delay(2); ENABLE_HIGH; } void check_busy(void) { P1DIR &= ~(BIT3); // make P1.3 as input while((P1IN&BIT3)==1) { data_read(); } P1DIR |= BIT3; // make P1.3 as output } void send_command(unsigned char cmd) { check_busy(); WRITE; CWR; P1OUT = (P1OUT & 0xF0)|((cmd>>4) & 0x0F); // send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT & 0xF0)|(cmd & 0x0F); // send lower nibble
data_write(); // give enable trigger


void send_data(unsigned char data)
P1OUT = (P1OUT & 0xF0)|((data>>4) & 0x0F); // send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT & 0xF0)|(data & 0x0F); // send lower nibble
data_write(); // give enable trigger

void send_string(char *s)

void lcd_init(void)
P1DIR |= 0xFF;
P1OUT &= 0x00;
send_command(0x28); // 4 bit mode
send_command(0x0E); // clear the screen
send_command(0x01); // display on cursor on
send_command(0x06); // increment cursor
send_command(0x80); // row 1 column 1

Main Code:

/* LCD_own.c
* Created on: 12-Nov-2013
* Author: Manpreet
* In this program we interface the lcd in 4 bit mode. We send strings and display it on the screen.
#include "lcd.h"

void main(void)
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
send_string("Manpreet Singh");

P1.0 – D4            Pin11

P1.1 – D5             Pin12

P1.2 – D6             Pin13

P1.3 – D7             Pin14

P1.4 – RS             Pin4

P1.5 – R/W         Pin5

P1.6 – E                 Pin6