Wednesday, January 10, 2007

Reading the busy Flag

►Reading the busy Flag

As discussed in the previous section, there must be some delay which is needed to be there for LCD to successfully process the command or data. So this delay can be made either with a delay loop of specified time more than that of LCD process time or we can read the busy flag, which is recomended. The reason to use busy flag is that delay produced is almost for the exact amount of time for which LCD need to process the time. So is best suited for every application.


Steps to read busy flag

when we send the command, the BF or D7th bit of the LCD becomes 1 and as soon as the command is processed the BF = 0. Following are the steps to be kept in mind while reading the Busy flag.
  • Select command register
  • Select read operation
  • Send enable signal
  • Read the flag

So following the above steps we can write the code in assembly as below...


CODE:
;Ports used are same as the previous example

LCD_busy:
         setb   LCD_D7        ;Make D7th bit of LCD data port as i/p
         setb   LCD_en        ;Make port pin as o/p
         clr    LCD_rs        ;Select command register
         setb   LCD_rw        ;we are reading
check:
         clr    LCD_en        ;Enable H->L
         setb   LCD_en
         jb     LCD_D7,check  ;read busy flag again and again till it becomes 0
         ret                  ;Return from busy routine


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

CODE:
void LCD_busy()
{
     LCD_D7   = 1;           //Make D7th bit of LCD as i/p
     LCD_en   = 1;           //Make port pin as o/p
     LCD_rs   = 0;           //Selected command register
     LCD_rw   = 1;           //We are reading
     while(LCD_D7){          //read busy flag again and again till it becomes 0
           LCD_en   = 0;     //Enable H->L
           LCD_en   = 1;
     }
}


The above routine will provide the necessary delay for the instructions to complete. If you dont want to read the busy flag you can simply use a delay routine to provide the a specific ammount of delay. A simple delay routine for the LCD is given below.

CODE:
LCD_busy:
         mov  r7,#50H
back:
         mov  r6,#FFH
         djnz r6,$
         djnz r7,back
         ret                  ;Return from busy routine



CODE:
void LCD_busy()
{
         unsigned char i,j;
         for(i=0;i<50;i++)        //A simple for loop for delay
            for(j=0;j<255;j++);
}

Now we are ready with the initialization routine and the busy routine for LCD. In the next section we will see how to send data and command to the LCD.