Thursday 3 January 2013

Sending Commands to LCD

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.


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

CODE:
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...
CODE:
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 '0000000B' 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 adress 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.

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


CODE:
// to do the same thing is C
// as we done before
LCD_command(0x83);

No comments:

Post a Comment