MSP430 Tutorial


4 Bit Interface MSP430 To an Alpha-Numeric LCD display

    Interfacing NHD-0216HZ-FSW-FBW-33V3C Alpha-Numeric LCD display to MSP430F5529IPNR microcontroller.

Hardware Interface between MSP430F5529IPNR and NHD-0216HZ-FSW-FBW-33V3C Display

In 4 bit mode we send the data one nibble (4bit) at a time, first the upper nibble and then the lower nibble. A nibble is a group of four bits. Each byte of data has two nibbles (MSN and LSN).

MSN - Most Significant Nibble
LSN - Least Significant Nibble


		
#include <msp430x552x.h>
#include <string.h>
#include "driverlib.h"
#include "hal.h"

#include "USB_config/descriptors.h"
#include "USB_API/USB_Common/device.h"
#include "USB_API/USB_Common/usb.h"                     //USB-specific functions
#include "USB_API/USB_CDC_API/UsbCdc.h"
#include "USB_app/usbConstructs.h"

// 4 - bit
void writecom4(char cmd);
void writedata4(char byte);
unsigned char LCDbusy4();

unsigned char i;


void DelayMs1(int Ms)
{

	while(Ms>0)
	{
		__delay_cycles(8000); // Delay 1ms - 8000 000 / 1000 = 8000 counts
		Ms--;
	}
}

void latch(){                           // command to latch E
	GPIO_setOutputHighOnPin(GPIO_PORT_P6, GPIO_PIN3); 	// E = 1; SETB E

	DelayMs1(1);

	GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN3); 	// E = 0; RESETB E
}

/*******************************************************************************
 *
 * GPIO_setAsOutputPin(GPIO_PORT_P5, GPIO_PIN7); // RS
 * GPIO_setAsOutputPin(GPIO_PORT_P5, GPIO_PIN6); // RW
 * GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN7); // En
 * GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN6); // XRESET
 * __delay_cycles(1000);
*/

/*
 * Write command
 */
void writecom4(char cmd)
{
	char temp;
	temp = cmd;

	// Send High Nybble
	temp = ((temp >> 4) & 0x0F);		//Shift H-Nybble to L-Nybble

	GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN3); 	// E = 0;		RESETB E
	GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN4); 	// RW = 0 - Write	CLR RW
	GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN5); 	// RS = 0 - Command	CLR RS
	DelayMs1(10);
	
	DelayMs1(1);

	P2OUT = (P2OUT & 0xF0) | temp; 	// Apply H-Nybble to 2.0 - 2.3 MOV P1,A

	DelayMs1(1);
	latch();

	//----------------------------------------------------------------------------------------

	// Send Low Nybble	
	DelayMs1(1);
	P2OUT = (cmd & 0x0F);				// Apply L-Nybble to 2.0 - 2.3				MOV P1,A
	
	DelayMs1(1);
	
	latch();
	
	DelayMs1 (2);

}

void writedata4(char byte)
{
	char temp;
	temp = byte;

	temp = ((temp >> 4) & 0x0F);			//Shift variable 'byte' MS nibble to LS nibble

	GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN3); 	// E = 0;	RESETB E
	GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN4); 	// RW = 0 - Write
	GPIO_setOutputHighOnPin(GPIO_PORT_P6, GPIO_PIN5); 	// RS = 1 - Data

	DelayMs1(2);

	P2OUT = (P2OUT & 0xF0) | temp;			//Output HN (P2OUT & 0xF0)|((cmd>>4) & 0x0F)
	latch();
	
	//------------------------------------------------------------------------------------------

	P2OUT = (byte & 0x0F);	//Output LN (P2OUT & 0xF0)|(byte & 0x0F)
	latch();
	
	DelayMs1(1);
}

unsigned char LCDbusy4(){

	unsigned char busy;
	
	GPIO_setAsInputPin(GPIO_PORT_P2, GPIO_PIN3);		//In 2.3

	DelayMs (2);

	GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN5); 	// RS = 0 - Command

	GPIO_setOutputHighOnPin(GPIO_PORT_P6, GPIO_PIN4); 	// RW = 1 - Read

	busy = (P2IN & 0x04);

	GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN3);		//Out 2.3

	DelayMs (2);

	return busy;
}


// Newhaven Initialization Program Code Example (4 Bit Interface) 


void init4() //initialize the LCD
{

	DelayMs1 (1000);
	P2OUT = 0x03;
	latch();
	DelayMs1 (100);
	latch();
	DelayMs1 (100);
	latch();
	DelayMs1 (100);
	P2OUT = 0x02;
	latch();
	DelayMs1 (100);
	
	writecom4_NHD_C0216CU_FSW_GBW_3V3(0x28); //wake up	28H 4 bit,N=1,5*7dot
	DelayMs1 (100);

	writecom4_NHD_C0216CU_FSW_GBW_3V3(0x10); //wake up	29H 4 bit,N=1,5*7dot
	DelayMs1 (100);
	
	writecom4_NHD_C0216CU_FSW_GBW_3V3(0x0F); //Internal OSC
	DelayMs1 (100);
	
	writecom4_NHD_C0216CU_FSW_GBW_3V3(0x06); //Contrast set 78H
	DelayMs1 (100);

}

const unsigned char Msg1[] = " 16x2 LCD Test... ";
const unsigned char Msg2[] = " F2812 EVB BOARD ";


void main(void)
{
	Calendar currentTime;
	uint16_t count = 0;

	volatile unsigned int i;

	WDT_A_hold(WDT_A_BASE); //Stop watchdog timer

	GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0); //DB4 is conneted to P2.0
	GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN1); //DB5 is conneted to P2.1
	GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN2); //DB6 is conneted to P2.2
	GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN3); //DB7 is conneted to P2.3

	GPIO_setAsOutputPin(GPIO_PORT_P6, GPIO_PIN5); // RS
	GPIO_setAsOutputPin(GPIO_PORT_P6, GPIO_PIN4); // RW
	GPIO_setAsOutputPin(GPIO_PORT_P6, GPIO_PIN3); // En	
	
	//USB initialize
	// Minimum Vcore setting required for the USB API is PMM_CORE_LEVEL_2
	PMM_setVCore(PMM_CORE_LEVEL_2);
	USBHAL_initPorts();           // Config GPIOS for low-power (output low)
	USBHAL_initClocks(8000000);   // Config clocks. MCLK = SMCLK = FLL = 8MHz; ACLK=REFO=32kHz
	USB_setup(TRUE,TRUE);  // Init USB & events; if a host is present, connect

	init4();		// init LCD
	DelayMs1(50);
	//while (LCDbusy4());	// Check for busy flag while it is high it is busy
	writecom4(0x80);
	DelayMs(5);
	for(i=0;i<16;i++){
		writedata4(Msg1[i]);
		//while (LCDbusy4());	// Check for busy flag while it is high it is busy
		DelayMs1(5);
	}
	
	writecom4(0xc0);
	DelayMs1(5);
	for(i=0;i<16;i++){    	
		writedata4(Msg2[i]);
		//while (LCDbusy4());	// Check for busy flag while it is high it is busy
		DelayMs1(5);
	}
}






Create your Google Account or just sign in and post.