100% found this document useful (2 votes)
3K views

Interfacing LCD in 4bit Mode With lpc2138

This document describes code for interfacing an LCD display in 4-bit mode with an LPC2138 microcontroller. It includes macro definitions for controlling the LCD data and control pins, functions for initializing the LCD, writing commands and characters to the LCD, clearing the LCD screen, and positioning the cursor. It also provides a main function that initializes the LCD, clears the screen, positions the cursor, and prints a test message to the LCD.

Uploaded by

hypernuclide
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
3K views

Interfacing LCD in 4bit Mode With lpc2138

This document describes code for interfacing an LCD display in 4-bit mode with an LPC2138 microcontroller. It includes macro definitions for controlling the LCD data and control pins, functions for initializing the LCD, writing commands and characters to the LCD, clearing the LCD screen, and positioning the cursor. It also provides a main function that initializes the LCD, clears the screen, positions the cursor, and prints a test message to the LCD.

Uploaded by

hypernuclide
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Interfacing Lcd in 4bit mode

with LPC2138
Code:
#include<lpc21xx.h>

#define pin_rs 1<<24 /*lcd rs pin*/

#define pin_rw 1<<23 /*lcd rw pin*/

#define pin_en 1<<22 /*lcd en pin*/

#define pin_ctrl pin_rs|pin_rw|pin_en /*lcd control pins*/

#define lcd_d4 1<<10 /*lcd data pins D4,D5,D6,D7*/

#define lcd_d5 1<<11

#define lcd_d6 1<<12

#define lcd_d7 1<<13

#define lcd_data lcd_d4|lcd_d5|lcd_d6|lcd_d7

#define lcd_rs(x) ((x)?(IOSET1=pin_rs):(IOCLR1=pin_rs)); /*lcd_rs(x) if x=1 will set the lcd_rs pin will clear if x=0*/

#define lcd_rw(x) ((x)?(IOSET1=pin_rw):(IOCLR1=pin_rw));

#define lcd_en(x) ((x)?(IOSET1=pin_en):(IOCLR1=pin_en));

#define data_in ((IOPIN0>>10)&(0x0F)) /*direction of the data pins as input*/

#define data_out(x) IOCLR0=lcd_data; IOSET0=(x & 0x0F)<<10; /*direction of the data pins and control pins
as output*/

#define lcd_all_dir_out IODIR0 |= lcd_data ; IODIR1 |= pin_ctrl; /*setting as output for both data and control pins*/

#define lcd_dir_data_out IODIR0 |= lcd_data

#define lcd_dir_data_in IODIR0 &= ~(lcd_data)

#define delay_2n 4

static void delay(int);

static unsigned char busy_wait();

static void delay(int cnt)

{
cnt <<= delay_2n;

while(cnt--);

static unsigned char busy_wait()

unsigned char status;

do

lcd_dir_data_in;

delay(10);

lcd_rs(0);

delay(10);

lcd_rw(1);

delay(10);

lcd_en(1);

delay(10);

status= (data_in<<4)&(0xF0);

lcd_en(0);

delay(10);

lcd_en(1);

delay(10);

status |=data_in;

lcd_en(0);

delay(10);

lcd_dir_data_out;

delay(10);

}while(status & 0x80);

return (status);

}
void lcd_write_4bit(unsigned char c)

lcd_rw(0);

delay(10);

lcd_en(1);

delay(10);

data_out(c & 0x0F);

delay(10);

lcd_en(0);

delay(10);

void lcd_cmd_write(unsigned char c)

busy_wait();

lcd_rs(0);

delay(10);

lcd_write_4bit(c>>4);

lcd_write_4bit(c);

static void lcd_data_write(unsigned char c)

busy_wait();

lcd_rs(1);

delay(10);

lcd_write_4bit(c>>4);

lcd_write_4bit(c);
}

void lcd_putchar(char c)

lcd_data_write(c);

void lcd_init()

lcd_all_dir_out;

delay(10);

lcd_rs(0);

lcd_write_4bit(0x3);

delay(10);

lcd_write_4bit(0x3);

delay(10);

lcd_write_4bit(0x3);

lcd_write_4bit(0x2);

lcd_cmd_write(0x28);

lcd_cmd_write(0x0C);

lcd_cmd_write(0x06);

int lcd_gotoxy( unsigned int x, unsigned int y)

int retval = 0;

if( (x > 1) && (y > 15) )

retval = -1;

} else {
if( x == 0 )

lcd_cmd_write( 0x80 + y ); /* command - position cursor at 0x00 (0x80 + 0x00 ) */

} else if( x==1 ){

lcd_cmd_write( 0xC0 + y ); /* command - position cursor at 0x40 (0x80 + 0x00 ) */

return retval;

void lcd_clear()

lcd_cmd_write(0x01);

lcd_gotoxy(0,0);

void lcd_print(unsigned char const *str)

while(*str)

lcd_putchar(*str++);

main()

lcd_init();

lcd_clear();

lcd_gotoxy(0,0);

lcd_print("hypernuclide.com");
}

Download the proteus simulation here :-


http://hypernuclide.com/viewtopic.php?f=30&p=236#p236

You might also like