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

📄 lcd1602.h

📁 8051单片机驱动1602字符液晶的C驱动 已经调试成功
💻 H
字号:
/********************************************************************************************/
//* 		DB0--P1   P3.0--RS   P3.1--R/W   P3.2--E                                            */                                          */
/*                                                                                          */
/*                                                                                          */
/********************************************************************************************/

#include <reg52.h>
#include <intrins.h>
#define  uchar  unsigned char

sbit RS = P3^2 ;
sbit RW = P3^3 ;
sbit E = P3^4 ; 

extern void LCD_init( void ) ;
extern void LCD_clear( void ) ;
extern void putcommands( uchar  comd ) ;
extern void displaymyNAME( void ) ;
extern void disp_one_char( uchar x , uchar y , uchar disp_string ) ;
extern void disp_list_char( uchar x , uchar y , uchar *disp_string ) ;

extern void putchar(uchar ch ) ;

delay(unsigned int t);
Delay_10t_us(int t) ;

uchar  count ;


/*********************************************************/
/* LCD initialize */
/* 设置LCD的工作模式:* 16×2显示,5×7点阵,8位数据接口   */
/*                    * 显示OFF,光标OFF,闪烁NO         */
/*                    * 光标输入方式增量移位             */
void LCD_init( void )
{
	delay(5);
	delay(5);
	delay(5);

	putcommands( 0x38 ) ;       //set LCD 16words x 2lines and 5 x 7 
	                            //写指令38H,设定LCD为16×2显示,5×7点阵,8位数据接口 
	                            // RS  R/W  D7 D6 D5 D4 D3 D2 D1 D0
	                            // 0    0   0  0  1  1  1  0  0  0
	                            // D7,D6 总是为0
	                            // D5总是为1 ,因为指令的设置就要求它是1
	                            // D4(DL)  =1  4位总线
	                            //         =0  8位总线
	                            // D3(N) =1 两行显示
	                            //       =0 单行显示
	                            // D2(F) =1 显示5×10的点阵
	                            //       =0 显示5×7的点阵
	                            // D1,D0 为任意值 
	delay(5);
	putcommands( 0x38 ) ;
	Delay_10t_us(10) ;
	putcommands( 0x38 ) ;
	putcommands( 0x38 ) ;
	putcommands( 0x0c ) ;     	//显示开/关 控制
							                // RS R/W D7 D6 D5 D4 D3 D2 D1 D0
							                // 0  0   0  0  0  0  1  1  0  0
							                // D7,D6,D5,D4  always is 0
							                // D3 always is 1
							                // D2(D) 控制整体显示的开与关
							                //       =0 关显示
							                //       =1 开显示
							                // D1(C) 控制光标的开与关
							                //       =0 没光标
							                //       =1 有光标
							                // D0(B) 控制光标是否闪烁
							                //       =0 不闪烁
							                //       =1 闪烁

		
	putcommands( 0x01 ) ;   //清显示
	                        // RS R/W D7 D6 D5 D4 D3 D2 D1 D0
							            // 0  0   0  0  0  0  0  0  0  1
							            //光标复位到地址00H位置
	
	putcommands( 0x06 ) ;   //光标和显示模式设置
	                        //光标输入方式增量移位
	                        // RS R/W D7 D6 D5 D4 D3 D2 D1 D0
							            // 0  0   0  0  0  0  0  1  1  0
							            // D7,D6,D5,D4,D3 ALWAYS IS 0
							            // D2 ALWAYS IS 1
							            // D1(I/D) 光标移动方向
							            //         =1 右移
							            //         =0 左移
							            // D0(S) 屏幕上所有文字是否左移或者右移
							            //       =1 表示有效
							            //       =0 表示无效
	
	putcommands( 0x0f ) ;  //显示开/关 控制
							           // RS R/W D7 D6 D5 D4 D3 D2 D1 D0
							           // 0  0   0  0  0  0  1  1  1  0
							           // D7,D6,D5,D4  always is 0
							           // D3 always is 1
							           // D2(D) 控制整体显示的开与关
							           //       =0 关显示
							           //       =1 开显示
							           // D1(C) 控制光标的开与关
							           //       =0 没光标
							           //       =1 有光标
							           // D0(B) 控制光标是否闪烁
							           //       =0 不闪烁
							           //       =1 闪烁
	putcommands( 0x02 ) ;  // 光标复位,光标返回到地址00H
	                       // RS R/W D7 D6 D5 D4 D3 D2 D1 D0
							           // 0  0   0  0  0  0  0  0  1  0
							           // D7,D6,D5,D4,D3,D2 ALWAYS IS 1
							           // D0 任意 
}



/**********************************************************************/
/*the putcommands function sends the datas(commands) to the data port */
void putcommands( unsigned char  comd ) 
{
	E = 0 ;     // set E=0 disable LCD
	Delay_10t_us(10) ;
	RS = 0 ;     // RS = 0 for COMMAND
	Delay_10t_us(10) ;
	RW = 0 ;     // set R/W=0 as write modle
	Delay_10t_us(10) ;
	P1 = comd;              // assign command to LCD
	Delay_10t_us(10) ;
	E = 1 ;     // E=1  enable LCCD
	Delay_10t_us(10) ;
	E = 0 ;     // E=0  disable LCD
	delay(5);              // wait
}


/**********************************************************************/
/*the putchar function sends the data to the data port                */
void putchar( uchar  ch )
{
	E = 0 ;     // set E=0 disable LCD 
	Delay_10t_us(10) ;
	P1 = ch ;               // assign char to LCD
	Delay_10t_us(10) ;
	RS = 1 ;     // RS = 1 for DATA
	Delay_10t_us(10) ;
	E = 1 ;     // E=1  enable LCCD
	Delay_10t_us(10) ;
	E = 0 ;     // E=0  disable LCD
	delay(5);              // wait
}

//实验证明上述不用延迟是可以的,因为E只是对高冲击有响应


/********************************************************************/
/*the LCD_clear fuction clear the display on the LCD                */
void LCD_clear( void ) 
{
  putcommands( 0x01 ) ;
}


/********************************************************************/
// 对于11.0592M时钟,约延时tms
delay(unsigned int t)
{
	unsigned int i;
	while(t--)
	{
		for (i=0;i<125;i++)
		{}
	}
}	


/**********************************************************************/
//11.0592M时钟,延时t*10us
Delay_10t_us(int t)
{
	while(t--)
	{
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	}
}

void displaymyNAME( void )
{
	putchar( 0x4c ) ;
	putchar( 0x69 )	;
	putchar( 0x20 )	;

	putchar( 0x7a )	;
	putchar( 0x6f )	;
	putchar( 0x6e )	;
	putchar( 0x67 )	;
	putchar( 0x20 )	;

	putchar( 0x6b )	;
	putchar( 0x61 )	;
	putchar( 0x69 ) ;
}

/***********************************
//函数名称:    void disp_one_char(unsigned char x, unsigned char y,unsigned char disp_data)
//传递参数:    unsigned char x, unsigned char y,unsigned char disp_data
//返 回 值:    无
//函数功能:    指定位置显示一个字符
//函数说明:    
***********************************/
void disp_one_char(unsigned char x,unsigned char y,unsigned char disp_data)
{
    y = y & 0x01 ;
    x = x & 0x0f ;                  //限制2行,没行15个字
    if ( y )
        x = x + 0x40 ;          //算RAM地址
    x = x + 0x80 ;
    putcommands( x ) ;
    putchar( disp_data ) ;
}

/***********************************
//函数名称:    void disp_one_char(unsigned char x, unsigned char y,unsigned char *disp_data)
//传递参数:    unsigned char x, unsigned char y,unsigned char *disp_data
//返 回 值:    无
//函数功能:    指定位置显示一串字符
//函数说明:    
***********************************/    
void disp_list_char(unsigned char x,unsigned char y, unsigned char *disp_string )    
{
   	uchar  position ;
	position = 0 ;   
   	y = y & 0x01 ;
    x = x & 0x0f ;
	
	while( disp_string[position] != '\0' )
	{
		disp_one_char( x , y , disp_string[position] ) ;
		x ++ ;
		position ++ ;
    }   
}

⌨️ 快捷键说明

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