Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

LCD 8-bit interface using "LCD Busy Flag"

Status
Not open for further replies.

amihomo

Full Member level 5
Full Member level 5
Joined
Jan 9, 2007
Messages
287
Helped
66
Reputation
132
Reaction score
43
Trophy points
1,318
Location
PERSIAN Gulf
Activity points
3,116
ts1620a-20/y

Hi
I've written an LCD driver program in CodeVision, but I have not used any existing LCD library.

My LCD is a 16 by 2 character LCD module (TS1620A-20/Y from www.lcdstar.com ).

the code is full of illustrating comments.


Code:

Code:
/****************************************************************
Project : LCD 8-bit interface Test
Author  : kouchenan(amihomo) ([url]www.amihomo.persianblog.ir[/url])                             
Chip type           : ATmega8535
Clock frequency     : 4.000000 MHz
==========================================================
PORTC[0..7] ==>  DB0..DB7 LCD data bus (pin 14-pin7)
LCD EN (pin 6) ==> PORTD.7
LCD RW (pin 5) ==> PORTD.6
LCD RS (pin 4) ==> PORTD.5
               
*******************************************************************/

#include <mega8535.h>
#include <string.h>
#include <delay.h>
#define EN PORTD.7
#define RW PORTD.6
#define RS PORTD.5
#define BF PINC.7   //busy flag

/*****************************************************************************/
/* LCD Commands ( Refer to LCD Data Sheet )                                  */
/* Standard command should work with most common devices                     */
/*****************************************************************************/
/*                                                                           */
#define write_data        0x00 /* With RS = 1                                */
#define clr_lcd           0x01 /* Clear Display                              */
#define ret_home          0x02 /* Cursor to Home position                    */
#define entry_mode        0x06 /* Normal entry mode                          */
#define entry_mode_sh     0x07 /* - with shift                               */
#define sys_set_4_bit     0x28 /* 4 bit data mode 2 line ( 5x7 font )        */
#define sys_set_8_bit     0x38 /* 8 bit data mode 2 line ( 5x7 font )        */
#define disp_on           0x0C /* Display ON                                 */
#define disp_off          0x08 /* Cursor plus blink                          */
#define cursor_on         0x0E /* Switch Cursor ON                           */
#define cursor_on_blink   0x0F /* Switch Cursor ON ,Blink ON                 */
#define cursor_off        0x0C /* Switch Cursor OFF                          */
#define move_cursor_l     0x10 /* Move cursor one character left             */
#define move_cursor_r     0x14 /* Move cursor one character right            */
#define scrl_disp_l       0x18 /* Scroll display 1 character left (all lines)*/
#define scrl_disp_r       0x1E /* Scroll display 1 character left (all lines)*/ 
#define goto_line1        0x80 /* Line 1 position 1                          */
#define goto_line2        0xC0 /* Line 2 position 1                          */
/*****************************************************************************/
void waitLCD(void);
void writeLCD(unsigned char byte ,unsigned char rs); //rs=0 for commands, rs=1 for data
void putStr(unsigned char str[]);
void initLCD(void);
void gotoXY(unsigned char x ,unsigned char y);
//=====================================================
void main(void)
{
   char msg[]="Hello World!This is an LCD test!";
   DDRC=0xff;
   PORTC=0xff;   //Set all data pins to 1 initially
   DDRD=0xff;

   initLCD();
   
   putStr(msg);
   
   while (1)
   {
      //loop forever                  
   }
}

void initLCD(void)
{   
   //delay_ms(45);
   writeLCD(sys_set_8_bit,0);
   writeLCD(cursor_on_blink,0);
   writeLCD(clr_lcd,0);
   writeLCD(entry_mode,0);
}

void writeLCD(unsigned char byte ,unsigned char rs)
{
   DDRC=0xff;
   PORTC=byte;
   RS=rs;   //rs=0 for commands, rs=1 for data
   RW=0;
   EN=1;
   EN=0;
   waitLCD();
}
//monitor the busy flag of LCD on its DB7
void waitLCD(void)
{

   unsigned char busy=1;
   //unsigned char cnt=0;
   do
   {
      DDRC=0xff;
      PORTC=0xff;   //Set all data pins to 1 initially
       DDRC=0x00;   //configure all bits as inputs
      EN=0;      //Start LCD command
      RS=0;      //It's a command
      RW=1;      //It's a read command
      EN=1;
      busy=BF;   //read the busy flag
      //cnt++;
      
   } while(busy==1);// && cnt<255);
   EN=0;   //Finish the command
   RW=0;   //Turn off RW for future commands

   
}

void putStr(unsigned char str[])
{
   unsigned char i;
   for (i=0;i<strlen(str);i++)
   {
      if (i>15)
         gotoXY(1,i-16);
      writeLCD(str[i],1);
   }
}

 
 
void gotoXY(unsigned char x ,unsigned char y)
{
   //x is 0 or 1
   //y is between 0 and 0x0f
   unsigned char pos;
   pos=x ? (0x80+0x40+y):(0x80+y) ;
   
   DDRC=0xff;
   PORTC=pos;
   RS=0;
   RW=0;
   EN=1;
   EN=0;
   waitLCD();   
}

the code was tested and works fine. but it is a simple tutorial and may not be usable for real world applications.

The distinctive feature of this code is that I have used the LCD busy flag instead of using delay functions ,so it will work regardless of LCD response time and needs no further customization of delay when the LCD is replaced.


currently I have written the code for the 4-bit interface mode of LCD , but i'm testing and debugging it. the code work well but i have problems with cursor positioning and I'll soon fix it.
Cool

cheers
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top