4 years, 8 months ago.

LPC1768 ARM Cortex M3 4 bit LCD code is not woking

Hi guys, I'm trying to dumb the code in my new LPC1768 development board, I've tried at least 100 different code from various websites. The HEX file of the manufactures CD is working, but there is no sample codes or proper information related to the codes. The code which is with me right now is,

  1. include<lpc17xx.h>

/* Configure the data bus and Control bus as per the hardware connection */

  1. define LcdDataBusPort LPC_GPIO2->FIOPIN
  2. define LcdControlBusPort LPC_GPIO1->FIOPIN
  1. define LcdDataBusDirnReg LPC_GPIO2->FIODIR
  2. define LcdCtrlBusDirnReg LPC_GPIO1->FIODIR
  1. define LCD_D4 23
  2. define LCD_D5 24
  3. define LCD_D6 25
  4. define LCD_D7 26
  1. define LCD_RS 21
  2. define LCD_RW 27
  3. define LCD_EN 22

/* Masks for configuring the DataBus and Control Bus direction */

  1. define LCD_ctrlBusMask ((1<<LCD_RS)|(1<<LCD_RW)|(1<<LCD_EN))
  2. define LCD_dataBusMask ((1<<LCD_D4)|(1<<LCD_D5)|(1<<LCD_D6)|(1<<LCD_D7))

/* local function to generate some delay */ void delay(int cnt) { int i; for(i=0;i<cnt;i++); }

/* Function send the a nibble on the Data bus (LCD_D4 to LCD_D7) */ void sendNibble(char nibble) { LcdDataBusPort&=(LCD_dataBusMask); Clear previous data LcdDataBusPort|= (((nibble >>0x00) & 0x01) << LCD_D4); LcdDataBusPort|= (((nibble >>0x01) & 0x01) << LCD_D5); LcdDataBusPort|= (((nibble >>0x02) & 0x01) << LCD_D6); LcdDataBusPort|= (((nibble >>0x03) & 0x01) << LCD_D7); }

/* Function to send the command to LCD. As it is 4bit mode, a byte of data is sent in two 4-bit nibbles */ void Lcd_CmdWrite(char cmd) { sendNibble((cmd >> 0x04) & 0x0F); Send higher nibble LcdControlBusPort &= (1<<LCD_RS); Send LOW pulse on RS pin for selecting Command register LcdControlBusPort &= (1<<LCD_RW); Send LOW pulse on RW pin for Write operation LcdControlBusPort |= (1<<LCD_EN); Generate a High-to-low pulse on EN pin delay(1000); LcdControlBusPort &= (1<<LCD_EN);

delay(10000);

sendNibble(cmd & 0x0F); Send Lower nibble LcdControlBusPort &= (1<<LCD_RS); Send LOW pulse on RS pin for selecting Command register LcdControlBusPort &= (1<<LCD_RW); Send LOW pulse on RW pin for Write operation LcdControlBusPort |= (1<<LCD_EN); Generate a High-to-low pulse on EN pin delay(1000); LcdControlBusPort &= (1<<LCD_EN);

delay(10000); }

void Lcd_DataWrite(char dat) { sendNibble((dat >> 0x04) & 0x0F); Send higher nibble LcdControlBusPort |= (1<<LCD_RS); Send HIGH pulse on RS pin for selecting data register LcdControlBusPort &= (1<<LCD_RW); Send LOW pulse on RW pin for Write operation LcdControlBusPort |= (1<<LCD_EN); Generate a High-to-low pulse on EN pin delay(1000); LcdControlBusPort &= (1<<LCD_EN);

delay(10000);

sendNibble(dat & 0x0F); Send higher nibble LcdControlBusPort |= (1<<LCD_RS); Send HIGH pulse on RS pin for selecting data register LcdControlBusPort &= (1<<LCD_RW); Send LOW pulse on RW pin for Write operation LcdControlBusPort |= (1<<LCD_EN); Generate a High-to-low pulse on EN pin delay(1000); LcdControlBusPort &= (1<<LCD_EN);

delay(10000); }

int main() { char i,a[]={"Good morning!"}; SystemInit(); Clock and PLL configuration

LcdDataBusDirnReg = LCD_dataBusMask; Configure all the LCD pins as output LcdCtrlBusDirnReg = LCD_ctrlBusMask;

Lcd_CmdWrite(0x02); Initialize Lcd in 4-bit mode Lcd_CmdWrite(0x28); enable 5x7 mode for chars Lcd_CmdWrite(0x0E); Display OFF, Cursor ON Lcd_CmdWrite(0x01); Clear Display Lcd_CmdWrite(0x80); Move the cursor to beginning of first line

Lcd_DataWrite('H'); Lcd_DataWrite('e'); Lcd_DataWrite('l'); Lcd_DataWrite('l'); Lcd_DataWrite('o'); Lcd_DataWrite(' '); Lcd_DataWrite('w'); Lcd_DataWrite('o'); Lcd_DataWrite('r'); Lcd_DataWrite('l'); Lcd_DataWrite('d');

Lcd_CmdWrite(0xc0); for(i=0;a[i]!=0;i++) { Lcd_DataWrite(a[i]); }

while(1); } The schematic diagram of 4 bit LCD interfacing in the LPC1768 controller is showing below.

/media/uploads/midhun9090/lpc1768_-_16_x_2_lcd.pdf

Your help is much appreciated, looking forward to your reply. Thank you.

1 Answer

4 years, 8 months ago.

Please use the <<code>> and <</code>> tags to keep posted code readable.

I recommend that you use one of the available LCD component libraries instead of the code above. See here for example.

Always check wiring, powersupply and contrast first. Do you get a row of dark rectangles after powering up the LCD?