⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 16x4 lcd.h

📁 16X4 LCD in 4bit mode working perfectly with 89C51
💻 H
字号:
/*****************************************************************************
 * LCD control functions.
 * 16X4 LCD DRIVER 4 bit mode
 * Created By:Ganesh Kumar.SW 
 * Created On:8-06-2007

		Display size					Visible

		Character 		positions				  DDRAM addresses
   		   4*16		 	 00..15		00h..0Fh + 40h..4Fh + 14h..23h + 54h..63h

 ****************************************************************************/


//HD44780 instruction set 
 typedef unsigned char BYTE;
 code BYTE	LCD_CLR				=	0x01,			//Clears display and returns cursor to the home position (address 0).
		       LCD_INIT_5X7		=	0x2C,			//Sets interface data length (DL), number of display line (N) and character font(F).(5X7 FONT)
		       LCD_CUR_HOME		=	0x02,			//Returns cursor to home position (address 0). Also returns display being shifted to the original position. DDRAM contents remains unchanged
		       LCD_ON		    		=	0x0C,				//Sets On/Off of all display (D), cursor On/Off (C) and blink of cursor position character (B).
		       LCD_SHIFT_LEFT		=	0x1C,			//Sets cursor-move or display-shift (S/C), shift direction (R/L). DDRAM contents remains unchanged
		       LCD_SHIFT_RIGHT	=	0x18,
		       LCD_CUR_INIT		=	0x06,			//Sets cursor move direction (I/D), specifies to shift the display (S). These operations are performed during data read/write
		       LCD_BLINK_ON		=	0x0D,			//blink on cursor	
			LCD_BLINK_OFF		=	0x0C;			//blink off
				
//Function Prototype for HD44780
						  
void wait_lcd();
void lcd_init();
void lcd_send_char(BYTE );
void lcd_clear();
void lcd_send_str(BYTE* );
void lcd_send_cmd(BYTE );
void lcd_setcursor(unsigned char line, unsigned char position);
void inttolcd(BYTE );
void bcdtolcd(BYTE );




#define DATA    P1

sbit EN = 0xB2;      //ICpin 12   - LCDpin6
sbit RW =0xB1;	//ICpin 11   - LCDpin5
sbit RS = 0xB0;	//ICpin 13   - LCDpin4

//Command to LCD 
void lcd_send_cmd(BYTE cmd)
{
	BYTE temp;

    
	               RS=0;			//To clear RS to send Instruction to LCD
	               temp=cmd;
                      cmd=cmd>>4;             
     		        DATA=cmd;
                      EN=1;			//Set EN To send Data to LCD
		        wait_lcd();		//To read the Busy status of LCD
		        EN=0;
                      DATA=temp;
		        EN=1;			//Set EN To send Data to LCD
		        wait_lcd();		//To read the Busy status of LCD
		        EN=0;			//Clear EN to Perform the operation
	    
	
}


//wait Routine
void wait_lcd()
{
  int i;
  	for(i=0;i<255;i++)
  	 {
  	 	;
	 }
}
		


//Initialization Routine
void lcd_init(void)
{
	 RW=0;
        RS=0;
        DATA=0x02;
        EN=1;
        wait_lcd();
        EN=0;
        
	lcd_send_cmd(LCD_INIT_5X7);
       lcd_send_cmd(LCD_ON);
	lcd_send_cmd(LCD_CUR_INIT);
			
		
}

//Send Character to LCD
void lcd_send_char(BYTE ch)
{
       BYTE temp;
       
		RS=1;		//select register to write data
		temp=ch;
		ch=ch>>4;
		DATA=ch;
              EN=1;			//Set EN To send Data to LCD
		wait_lcd();		//To read the Busy status of LCD
		EN=0;
       	DATA=temp;
		EN=1;			//Set EN To send Data to LCD
		wait_lcd();		//To read the Busy status of LCD
		EN=0;			//Clear EN to Perform the operation
		
}

//Routine to clear LCD
void lcd_clear()
{
	
		lcd_send_cmd(LCD_CLR);
		lcd_send_cmd(LCD_CUR_HOME);
	
}


//Routine to send String to LCD
void lcd_send_str(BYTE* str)
{
	
	
	while(*str != '\0')
	{

              lcd_send_char(*str);
		str++;
		
	}
}


//place the cursor at the LCD

  void lcd_setcursor(unsigned char line, unsigned char pos)
{
	signed char addr=0x00;
    pos--;
	 
	if(line==1)
	{
		
		addr=0x80+pos ;
              
	}

	else if(line==2)
	{
		addr=0x80 +pos+0x40;
       }

	else if(line==3)
	{
		addr=0x80+pos+0x14;
            
	}
	
	else if(line==4)
	{
		addr=0x80+pos+0x54;
            
	}

	lcd_send_cmd(addr);

}


//integer to LCD
void inttolcd(BYTE value)
{
    BYTE d1,d2;

	 d1=value/10;
     d2=value%10;
	 	
        d1=d1+0x30;
        d2=d2+0x30;
     		
                lcd_send_char(d1);
                lcd_send_char(d2);
	
}

//BCD to LCD

void bcdtolcd(BYTE value)
{
	BYTE temp;
	
	temp=value;
	temp>>=4;
	temp&=0x0F;
	value&=0x0F;

	lcd_send_char(temp+0x30);
                lcd_send_char(value+0x30);



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -