Thursday 3 January 2013

Sending Data To LCD

►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.


CODE:
;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.

CODE:
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.


CODE:
;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.

CODE:
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. Please proceed to the next section to read more.

No comments:

Post a Comment