MSP430 Launchpad interface with 16×2 LCD Display

Here I summarize the project of interfacing the 16×2 LCD Display with MSP430 Launchpad.

Hardware Connection

Here we use 4-bit mode (data bus D7-D4 only) to communicate with LCD display since we don’t have sufficient Pins in MSP430 Launchpad for 8-bit communicaton.

 

 

 

TP1 – Vcc (+5v)
TP3 – Vss (Gnd)
P1.0 – D4
P1.1 – D5
P1.2 – D6
P1.3 – D7
P1.4 – EN
P1.5 – RS
Gnd – RW
Gnd – Vee/Vdd through 1K Resistor – this value determines contrast – i.e. direct connection to Gnd means all dots displayed
Gnd – K (LED-)
Vcc – A (LED+) +5V for Backlight

 

IMG_20150310_211920

 

Source code

/* Build: msp430-gcc -mmcu=msp430g2231 -g -o lcd.elf lcd.c */

/* uC and LCD Connections 
	TP1 - Vcc (+5v)
	TP3 - Vss (Gnd) 
	P1.0 - D4
	P1.1 - D5
	P1.2 - D6
	P1.3 - D7
	P1.4 - EN
	P1.5 - RS
	Gnd  - RW
	Gnd  - Vee through 1K Resistor	- this value determines contrast - i.e. direct connection to Gnd means all dots displayed
	Gnd  - K (LED-)
	Vcc  - A (LED+) +5V
	Clock: 1MHz             */

#include <msp430g2231.h>

// uC Port definitions
#define lcd_port    	P1OUT
#define lcd_port_dir 	P1DIR
 
// LCD Registers masks based on pin to which it is connected
#define LCD_EN     	BIT4 
#define LCD_RS      BIT5 
 
void lcd_reset()
{
	lcd_port_dir = 0xff;	// output mode
	lcd_port = 0xff;
	__delay_cycles(20000);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	__delay_cycles(10000);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	__delay_cycles(1000);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	__delay_cycles(1000);
	lcd_port = 0x02+LCD_EN;
	lcd_port = 0x02;
	__delay_cycles(1000);
}
 
void lcd_cmd (char cmd)
{ 
	// Send upper nibble
	lcd_port = ((cmd >> 4) & 0x0F)|LCD_EN;
	lcd_port = ((cmd >> 4) & 0x0F);
 
	// Send lower nibble
	lcd_port = (cmd & 0x0F)|LCD_EN;
	lcd_port = (cmd & 0x0F);
 
	__delay_cycles(4000);
}

void lcd_init ()
{
	lcd_reset();         // Call LCD reset
	lcd_cmd(0x28);       // 4-bit mode - 2 line - 5x7 font. 
	lcd_cmd(0x0C);       // Display no cursor - no blink.
	lcd_cmd(0x06);       // Automatic Increment - No Display shift.
	lcd_cmd(0x80);       // Address DDRAM with 0 offset 80h.
	lcd_cmd(0x01);		 // Clear screen
}

 
void lcd_data (unsigned char dat)
{
	// Send upper nibble 
	lcd_port = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
	lcd_port = (((dat >> 4) & 0x0F)|LCD_RS);
	
	// Send lower nibble
	lcd_port = ((dat & 0x0F)|LCD_EN|LCD_RS);
	lcd_port = ((dat & 0x0F)|LCD_RS);
 
	__delay_cycles(4000); // a small delay may result in missing char display
}

void display_line(char *line)
{
	while (*line)
		lcd_data(*line++);
}

int main()
{
	// Stop Watch Dog Timer
	WDTCTL = WDTPW + WDTHOLD;

	// Initialize LCD
	lcd_init();

	lcd_cmd(0x80); // select 1st line (0x80 + addr) - here addr = 0x00
	display_line("Black God");
	lcd_cmd(0xc0); // select 2nd line (0x80 + addr) - here addr = 0x40
	display_line("Bits & Bytes");	

	while (1);
}

You can download the source code here >> lcd.c

Courtesy: Huge number of web sites which explains this protocol. I used a lot of code from many web sites. Still my LCD 16×2 needs some tweak in timing. Please be aware of this when you give a try.

 Troubleshooting tips

1. Only one line of LCD display shows black boxes – I was struggling with this one. It is due to wrong wiring. In this case remove jumper cap for RXD, TXD and LED1 which are connected to Port pins. And ensure that Gnd is connected between MSP430 and LCD. And also ensure that you connect +5v from TP1 (you may need to solder a header pin here) to LCD if your LCD is +5v operated one as in most cases. Don’t connect Vcc of MSP430 to LCD in this case.

2. Characters not displayed properly – Control it through Contrast control Pin Vee/Vdd. Ground Vee/Vdd pin of LCD with a appropriate resistor. In my case it is 1Kohms. It differs model to model. Feel free to experiment.

3. Characters are missing and partial – Check with the delay between displaying two characters. Improving the delay will help.

Share this post

One comment

  1. I’m facing the exact same problem as number 1 that is only one line of LCD display is showing black boxes. I have removed the jumper pins but I’m stuck. I really need your help. Would be very grateful to you 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *