[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1499
  • Last Modified:

Writing to a 16x2 LCD in 4-bit Mode C code

Hi I need to use an LCD (16x2) in 4-bit mode. I have found this assembly code and I need to translated to C. This is only to write to the LCD. Below this code I have posted the C code I have translated but it is not working...can anyone help me re-writing this?
DATA is the port use to send data and commands.
machine code

 PUSH  ACC           ;Save A for low nibble
 ORL   DATA,#0F0h    ;Bits 4..7 <- 1
 ORL   A,#0Fh        ;Don't affect bits 0-3
 ANL   DATA,A        ;High nibble to display
 POP   ACC           ;Prepare to send
 SWAP  A             ;...second nibble
 ORL   DATA,#0F0h    ; Bits 4...7 <- 1
 ORL   A,#0Fh       ; Don't affect bits 0...3
 ANL   DATA,A        ;Low nibble to display
C Code
static void WriteInstrReg (unsigned char Instr)
Ena = 0;                      
RS = 0;              // select instruction register
R_W= 0;            // write operation
Ena = 1;                // give operation start signal
_nop_ (); _nop_ ();        // wait
nibble = 0;
nibble |= Instr;
nibble &= 0xF0;
P2 = nibble;
Ena = 0;
delay1(64000);      // wait for LCD...is busy
nibble = 0;
Ena = 1;
Instr <<= (0x04);
nibble |= Instr;
nibble &= 0xF0;
P2 = nibble;
Ena = 0;
delay1(64000);    // wait for LCD...is busy
nibble is an unsigned char.
1 Solution
Jaime OlivaresSoftware ArchitectCommented:
You have not specified what microcontroller you are working on, so I will assume it is 8051 based.
You can search for a ready-made library written in C, take a look to this:
It is designed for an Atmel processor with a 4 bit display interface (brand doesn't matters).
Good luck,
calenoAuthor Commented:
Jaime, your assumption is right. The micro is from 8051 family. The Link you posted is very good. It helped a lot


Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now