dolphinlabs
...
Doplhin labs
News

LPC2148-LCD Interfacing


1.      LCD

Objective:

Write a program to display message on LCD.

Hardware Connections for LPC2148 Board:-

1.      Plug in the USB cable to your computer and to the Board. Once the USB cable is plugged in, Board is powered up.

2.      Connect +5V pin of LCD interface to +5V supply pin and GND pin of LCD interface to GND pin of supply.

3.      Connect RS pin of LCD to P0.15 pin of microcontroller.

4.      Connect RW pin of LCD to P0.16 pin of microcontroller.

5.      Connect EN pin of LCD to P0.17 pin of microcontroller.

6.      Connect data pins of LCD D4-D7 to P0.18-P0.21 pins of microcontroller.

Diagram:

+5V

 

Source Code:

//lcd.h file

#ifndef _LCD_H

#define _LCD_H

 

#define MAX_CHAR_IN_ONE_LINE 16

//lcd.h

enum ROW_NUMBERS

{

  LINE1,

  LINE2          

};

 

void init_lcd(void);

void test_cmd(void);

void lcd_putstring(unsigned char line, char *string);

void lcd_clear(void);

void lcd_backlight_on(void);

int lcd_gotoxy(unsigned int x, unsigned int y);

void lcd_putchar(int c);

 

#endif

 

//lcd.c file

 

#include <LPC21xx.H>                       /* LPC214x definitions */

#include "lcd.h"

 

#define LCD_BACK_LIGHT_TIMEOUT 1000

#define LCD_DATA_DIR       IO0DIR

#define LCD_DATA_SET       IO0SET

#define LCD_DATA_CLR      IO0CLR

 

#define LCD_CTRL_DIR        IO0DIR

#define LCD_CTRL_SET       IO0SET

#define LCD_CTRL_CLR       IO0CLR

 

#define LCDRS                     (1 << 15)

#define LCDRW                   (1 << 16)

#define LCDEN                    (1 << 17)

 

#define LCD_D4 (1 << 18)

#define LCD_D5 (1 << 19)

#define LCD_D6 (1 << 20)

#define LCD_D7 (1 << 21)

 

#define LCD_DATA_MASK           (LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7)

#define LCD_BUSY_FLAG           LCD_D7

 

#define LCD_IO0PIN                                               IO0PIN

void delay(int count)

{

  int j=0,i=0;

 

  for(j=0;j<count;j++)

  {

    /* At 60Mhz, the below loop introduces

    delay of 10 us */

    for(i=0;i<35;i++);

  }

}

 

void wait_lcd( void )

{

  LCD_DATA_DIR &= ~LCD_BUSY_FLAG;

  LCD_CTRL_CLR |=  LCDRS;      

  LCD_CTRL_SET |=  LCDRW |LCDEN;

  while(LCD_IO0PIN & LCD_BUSY_FLAG);       /* wait for busy flag to become low */

    LCD_CTRL_CLR |= LCDEN | LCDRW;

  LCD_DATA_DIR |= LCD_DATA_MASK;

    delay(100); 

}

void lcd_command_write( unsigned char command )

{

  unsigned char temp=0;

  unsigned int temp1=0;

 

  temp=command;

  temp=(temp>>4)&0x0F;

  temp1=(temp<<18)&LCD_DATA_MASK;

 

  LCD_CTRL_CLR = LCDRS;

  LCD_CTRL_SET = LCDEN;

  LCD_DATA_CLR = LCD_DATA_MASK;

  LCD_DATA_SET = temp1;

  delay(10000);

  LCD_CTRL_CLR = LCDEN;

 

  temp=command;

  temp&=0x0F;

  temp1=(temp<<18)&LCD_DATA_MASK;

  delay(100*2);

 

  LCD_CTRL_CLR |= LCDRS;

  LCD_CTRL_SET |= LCDEN;

  LCD_DATA_CLR = LCD_DATA_MASK;

  LCD_DATA_SET = temp1;

  delay(10000);           

  LCD_CTRL_CLR |= LCDEN;

  wait_lcd();

}

void set_lcd_port_output( void )

{

  LCD_CTRL_DIR |= ( LCDEN | LCDRS | LCDRW );

  LCD_CTRL_CLR |= ( LCDEN | LCDRS | LCDRW );     

  LCD_DATA_DIR |= LCD_DATA_MASK;

}

void lcd_clear( void)

{

  lcd_command_write( 0x01 );

}

int lcd_gotoxy( unsigned int x, unsigned int y)

{

  int retval = 0;

 

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

  {

    retval = -1;

  } else {

  if( x == 0 )

  {

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

  }

 else if( x==1 )

{

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

    }

   }

   return retval;

}

 

void lcd_data_write( unsigned char data )

{

  unsigned char temp=0;

  unsigned int temp1=0;

 

  temp=data;

  temp=(temp>>4)&0x0F;

  temp1=(temp<<18)&LCD_DATA_MASK;

 

  LCD_CTRL_SET |= LCDEN|LCDRS;

  LCD_DATA_CLR = LCD_DATA_MASK;

  LCD_DATA_SET = temp1;

  LCD_CTRL_CLR |= LCDEN;

 

  temp=data;

  temp&=0x0F;

  temp1=(temp<<18)&LCD_DATA_MASK;

 

  LCD_CTRL_SET |= LCDEN|LCDRS;

  LCD_DATA_CLR = LCD_DATA_MASK;

  LCD_DATA_SET = temp1;

  LCD_CTRL_CLR |= LCDEN;

  wait_lcd();

}

void lcd_putchar( int c )

{

  lcd_data_write( c );

}

 

void lcd_putstring( unsigned char line, char *string )

{

  unsigned char len = MAX_CHAR_IN_ONE_LINE;

 

  lcd_gotoxy( line, 0 );

  while(*string != '\0' && len--)

  {

    lcd_putchar( *string );

    string++;

  }

}

 

void init_lcd( void )

{

  set_lcd_port_output();

  delay(100*2);

  lcd_command_write(0x28);     /*   4-bit interface, two line, 5X7 dots.        */

  lcd_command_write(0x02);     /*   cursor home                                 */

  lcd_command_write(0x06);     /*   Entry mode                       */

  lcd_command_write(0x0F) ;    /*   display on cursor blinking    */ 

}

 

void test_cmd(void)

{

//          lcd_command_write(0x38);   // Interface length 8-bit 2 line

            lcd_command_write(0x08);   //Display off,cursor off

            lcd_command_write(0x0C);   //Display on cursor off

            lcd_command_write(0x10);   //Move cursor left by 1 char

            lcd_command_write(0x14);   //Move cursor right by 1 char

            lcd_command_write(0x01);   //Clear display

}

//main.c                       

#include <LPC21xx.H>                       /* LPC214x definitions */

#include "lcd.h"

 

int main (void)

  init_lcd();                                                                    //Initialize the LCD

  test_cmd();                                                                  //Test all the LCD commands

  lcd_putstring(0,"  Dolphin labs   ");

  lcd_putstring(1,"   Pune  ");

    while(1);

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Get SOCIAL







SAFE & SECURE SHOPPING

visa card master card maestro card
Maverick IT Industries Pvt. Ltd.