Sending Commands to LCD

To send commands we simply need to select the command register. Everything is same as we have done in the initialization routine. But we will summarize the common steps and put them in a single subroutine. Following are the steps:
  • Move data to LCD port
  • select command register
  • select write operation
  • send enable signal
  • wait for LCD to process the command

Keeping these steps in mind we can write LCD command routine as.


;Ports used are same as the previous example
;Routine to send command to LCD

LCD_command:
         mov   LCD_data,A     ;Move the command to LCD port
         clr   LCD_rs         ;Selected command register
         clr   LCD_rw         ;We are writing in instruction register
         setb  LCD_en         ;Enable H->
L
         clr   LCD_en
         acall LCD_busy       ;Wait for LCD to process the command
         ret                  ;Return from busy routine

; Usage of the above routine
; A will carry the command for LCD
; e.g. we want to send clear LCD command
;
; mov   a,#01H         ;01H is command for clearing LCD
; acall LCD_command    ;Send the command

The equivalent C code Keil C compiler. Similar code can be written for SDCC.

void LCD_command(unsigned char var)
{
     LCD_data = var;      //Function set: 2 Line, 8-bit, 5x7 dots
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in instruction register
     LCD_en   = 1;        //Enable H->
L
     LCD_en   = 0;
     LCD_busy();          //Wait for LCD to process the command
}
// Using the above function is really simple
// var will carry the command for LCD
// e.g.
//
// LCD_command(0x01);

Setting cursor position on LCD
To set the cursor position on LCD, we need to send the DDRAM address...
Bit7  6   5   4   3   2   1   0
 1   AD6 AD5 AD4 AD3 AD2 AD1 AD0

The seventh bit is always 1, and bit 0 to 7 are DDRAM address (See the introduction section of LCD). so if you want to put the cursor on first position the address will be '0b0000000' in binary and 7th bit is 1. so address will be 0x80, so for DDRAM all address starts from 0x80.

For 2 line and 16 character LCD. The address from 0x80 to 0x8F are visible on first line and 0xC0 to 0xCF is visible on second line, rest of the DDRAM area is still available but is not visible on the LCD, if you want to check this thing, then simply put a long sting greater than 16 character and shift the entire display, you will see all the missing character coming from the back.. this way you can make scrolling line on LCD (see more on shifting display in commands section).

Below is an example for setting cursor position on LCD.

;We are placing the cursor on the 4th position
;so the DDRAM address will be 0x03
;and the command will be 0x80+0x03 = 0x83
mov a,#83H           ;load the command
acall LCD_command    ;send command to LCD
// to do the same thing is C
// as we done before
LCD_command(0x83);

Sending Data to LCD

To send data we simply need to select the data register. Everything is same as the command routine. Following are the steps:
  • Move data to LCD port
  • select data register
  • select write operation
  • send enable signal
  • wait for LCD to process the data

Keeping these steps in mind we can write LCD command routine as.


;Ports used are same as the previous example
;Routine to send data (single character) to LCD

LCD_senddata:
         mov   LCD_data,A     ;Move the command to LCD port
         setb  LCD_rs         ;Selected data register
         clr   LCD_rw         ;We are writing
         setb  LCD_en         ;Enable H->
L
         clr   LCD_en
         acall LCD_busy       ;Wait for LCD to process the data
         ret                  ;Return from busy routine

; Usage of the above routine

; A will carry the character to display on LCD
; e.g. we want to print A on LCD
;
; mov   a,#'A'         ;Ascii value of 'A' will be loaded in accumulator

; acall LCD_senddata   ;Send data

The equivalent C code Keil C compiler. Similar code can be written for SDCC.

void LCD_senddata(unsigned char var)
{
     LCD_data = var;      //Function set: 2 Line, 8-bit, 5x7 dots
     LCD_rs   = 1;        //Selected data register
     LCD_rw   = 0;        //We are writing
     LCD_en   = 1;        //Enable H->
L
     LCD_en   = 0;
     LCD_busy();          //Wait for LCD to process the command
}
// Using the above function is really simple
// we will pass the character to display as argument to function
// e.g.
//
// LCD_senddata('A');

Now you have seen that its really easy to send command and data to LCD. Now what if we have a string to send to LCD? how we are going to do that?

Is simple, we will store the LCD string in the ROM of controller and call the string character by character. A simple exmple is shown below.


;Sending string to LCD Example

LCD_sendstring:
         clr   a                 ;clear Accumulator for any previous data
         movc  a,@a+dptr         ;load the first character in accumulator
         jz    exit              ;go to exit if zero
         acall lcd_senddata      ;send first char
         inc   dptr              ;increment data pointer
         sjmp  LCD_sendstring    ;jump back to send the next character
exit:
         ret                     ;End of routine

; Usage of the above routine
; DPTR(data pointer) will carry the address
; of string to send to LCD.
; e.g. we want to print "LCD Tutorial" on LCD then
;

; mov   dptr,#my_string   ;my_string is the label where the string is stored

; acall LCD_sendstring    ;Send string

;

; To store a string..

; my_string:

; DB   "LCD Tutorial", 00H

; 00H indicate that string is finished.

The equivalent C code Keil C compiler. Similar code can be written for SDCC.

void LCD_sendstring(unsigned char *var)
{
     while(*var)              //till string ends
       LCD_senddata(*var++);  //send characters one by one
}
// Using the above function is really simple
// we will pass the string directly to the function
// e.g.
//
// LCD_sendstring("LCD Tutorial");

Now we are ready with sending data and sending command to LCD. Now the last and final section which is creating custom characters or patterns to display on LCD.

Share it! Like it!

Conversation

Sat Jan 31 2015, 11:17 AM
Pradeep Singh
I'm using 8051 microcontroller and want to configure HC-06 with it but I'm not able to find out what problem is occuring as my blueethooth is getting connected but no communication between blwutooth and pc/andriod phone
sachinmm
Sat Jan 31 2015, 05:23 AM
hello sir,i ask the question related to 7 segment display plz help me.
Thu Jan 29 2015, 11:24 AM
durai
I found discussion relevant to my problem (link:http://www.8051projects.net/t59199/gsm-cdma-discussion/flashing-new-firmware-in-sim900-modem.htm) but i could not find the project mentioned in that discusion
Thu Jan 29 2015, 11:21 AM
durai
Hi i am using a SIM/900A GSM modem, I want to send email with an attachement using a microcontroller but when i try to use AT_Email commands it gives me error,I think modem doesnot support those commands,is there any other way to send mail using that modem??
ajay_bhargav
Thu Jan 29 2015, 06:54 AM
@gaurav, please mail me message you're trying to post.
ExperimenterUK
Thu Jan 29 2015, 01:17 AM
bhagyesh.bhavasar/gaurav.k I can post with Firefox. Can you try other browsers ?
naqirizvi
Wed Jan 28 2015, 08:08 AM
the web theme is too much buggy and heavy , admin please change it to simple theme, so that user can browse quickly
naqirizvi
Wed Jan 28 2015, 08:07 AM
some one please share software name or ebooks related to avr
bhagyesh.bhavasar
Tue Jan 27 2015, 11:06 AM
i want to share my project but there is problem occurred during postng
bhagyesh.bhavasar
Tue Jan 27 2015, 05:58 AM
@ExperimentalUK i have used that purple button but same as previous it takes me to blank page

Downloads

Comments

sachinmm
Thu Jan 29 2015, 07:09 AM
nawfel
Sat Jan 17 2015, 07:27 AM
Rohi123
Sat Jan 10 2015, 11:00 AM
safakarici
Fri Jan 09 2015, 08:55 PM
Nikita_Pinjare
Thu Jan 08 2015, 03:10 PM
mayank058
Wed Jan 07 2015, 05:43 AM
Overeating
Tue Jan 06 2015, 05:56 AM
trancebreaker11
Sun Jan 04 2015, 07:42 AM

Online

Guests: 34, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37574
Newest member: sachinmm
Forum Activity
sankalp_s Avatar
Posted By sankalp_s
Fri Jan 30 2015, 05:40 PM
you need to remove jump statements from main progr...
mayank058 Avatar
Posted By mayank058
Fri Jan 30 2015, 07:02 AM
dear sir, please tell me how to do t...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Jan 29 2015, 10:41 PM
T he P89V51RD2 P1.5, P1.6, P1.7 has extra curren...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Jan 29 2015, 06:45 PM
@naqirizviAs you already have a thread running I t...
naqirizvi Avatar
Posted By naqirizvi
Thu Jan 29 2015, 02:11 PM
Circuit diagram i am following i am not sure is it...
naqirizvi Avatar
Posted By naqirizvi
Thu Jan 29 2015, 02:11 PM
Circuit diagram i am following i am not sure is it...
naqirizvi Avatar
Posted By naqirizvi
Thu Jan 29 2015, 02:11 PM
Circuit diagram i am following i am not sure is it...
naqirizvi Avatar
Posted By naqirizvi
Thu Jan 29 2015, 01:50 PM
Hi,I am new to programming got some issues in RFID...
sachinmm Avatar
Posted By sachinmm
Thu Jan 29 2015, 07:21 AM
hello sir, plz tell me the circuit diagram interfa...
ajay_bhargav Avatar
Posted By ajay_bhargav
Thu Jan 29 2015, 06:28 AM
Moreover I see the segment Anode is connected via ...