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

📄 1602.txt

📁 1602显示程序
💻 TXT
字号:
#include <reg52.h>
#include <INTRINS.H>			//_nop_指令所需的头文件
#include <string.h>

#define uchar unsigned char
#define uint unsigned int		//定义方便使用
#define Busy 0x80               //用于检测LCD状态字中的Busy标识
/*************定义引脚****************/
#define Data P1
sbit RS=P0^5;
sbit RW=P0^4;
sbit E=P0^3;

/*************定义全局变量*************/
uchar Char1[]={"LCD TEST"};
uchar Char2[]={"Hohai University"};
uchar Char3='?';
/*************需要的延时***************/
void delay1s(void)
{
  unsigned char i,j,k;
  for(k=100;k>0;k--)
  for(i=20;i>0;i--)
  for(j=248;j>0;j--);
}
void delay15ms(void)
{
  unsigned char i,j;
  for(i=15;i>0;i--)
  for(j=248;j>0;j--);
}
void delay10ms(void)
{
  unsigned char i,j;
  for(i=10;i>0;i--)
  for(j=248;j>0;j--);
}
void delay5ms(void)
{
  unsigned char i,j;
  for(i=10;i>0;i--)
  for(j=248;j>0;j--);
}
/**************************************/



/********************LCD操作函数**********************/

/***************LCD忙信号检测函数*************/
void  ReadStatusLCD(void)
{
	Data=0xFF;
	E=1;
 	RS=0;
 	RW=1;
 	_nop_();
 	_nop_();
 	_nop_();	 
	while(Data&Busy); //检测忙信号
 	E=0;
}
/***************LCD数据读取函数**************/
void ReadDataLCD(uchar RDLCD)
{
	ReadStatusLCD(); //检测忙
	RS=1;			 //读数据: E=高脉冲 RS=1 RW=1
 	RW=1;
 	Data=RDLCD;
	E=1; 
 	_nop_();		 //空指令做一个机器周期的延时
 	_nop_();
 	_nop_();
 	E=0;             //重设E=0
}


/***************LCD数据写入函数**************/
void WriteDataLCD(uchar WDLCD)
{
 	ReadStatusLCD(); //检测忙
 	RS=1;			 //写数据: E=高脉冲 RS=1 RW=0
 	RW=0;
 	Data=WDLCD;
 	E=1; 
 	_nop_();
 	_nop_();
 	_nop_();
 	E=0;            
}
/***************LCD指令写入函数**************/
void WriteCommandLCD(uchar WCLCD,bit BusyC) //BusyC为0时忽略忙检测
{
 	if(BusyC) ReadStatusLCD(); //根据需要检测忙
 	RS=0;					   //写指令: E=高脉冲 RS=0 RW=0
	RW=0;
	Data=WCLCD;
	E=1; 
 	_nop_();		 
 	E=0;
}
//正常读写操作之前必须检测LCD控制器状态:E=1 RS=0 RW=1;DB7: 0 LCD控制器空闲,1 LCD控制器忙。

/****************LCD初始化函数****************/
void LCDInit(void) 
{
	delay15ms();
 	WriteCommandLCD(0x38,0); //初始化操作,pdf中已说明
 	delay5ms();
	WriteCommandLCD(0x38,0);
 	delay5ms();
	WriteCommandLCD(0x38,0);
 	delay5ms();
 	WriteCommandLCD(0x38,1); //显示模式设置,开始要求每次检测忙信号
 	delay5ms();
 	WriteCommandLCD(0x08,1); //关闭显示
 	delay5ms();
 	WriteCommandLCD(0x01,1); //显示清屏
 	delay5ms();
 	WriteCommandLCD(0x06,1); // 显示光标移动设置
 	delay5ms();
 	WriteCommandLCD(0x0C,1); // 显示开及光标设置
 	delay5ms();
}
/*************按指定位置显示一个字符****************/
void DisplayOneChar(uchar X, uchar Y, uchar DData)
{
 	Y&=0x01;
 	X&=0x0F;                              //限制X不能大于15,Y不能大于1
 	if(Y)X|=0x40;                         //当要显示第二行时地址码+0x40;
 	X|=0x80;                              //算出指令码
 	WriteCommandLCD(X,1);                 //这里检测忙信号,发送地址码
 	WriteDataLCD(DData);
}

/**************按指定位置显示一串字符******************/	  

void DisplayListChar(uchar x,uchar y, uchar *listdata)
{
 	uchar i=0;
 	for(i=0;i<strlen(listdata);i++ )
  	{
   		if (x<=0x0F)                      //x坐标应小于0x0F
    		{
    	 		DisplayOneChar(x,y,listdata[i]); //显示单个字符
         		x++;
         	}
        else if(y==0)
		{
        	y++;
         	x=x-0x0F;
         	DisplayOneChar(x,y,listdata[i]);
  	  	}
	}
}
/*****************4*4键盘扫描程序***********************/
uchar keyscan()
{
	uchar h,l,p;
	P2=0xf0;
	if((P2&0xf0)!=0xf0)
	{
	    delay10ms();
		if((P2&0xf0)!=0xf0)
		{	
			h=0xfe;
			while((h&0x10)!=0)
			{
				P2=h;
				if((P2&0xf0)!=0xf0)
				{
					l=(P2&0xf0)|0x0f;
					p=(~h)+(~l);
					if(p==0x81)	return ('0');
					if(p==0x41)	return ('1');
					if(p==0x21)	return ('2');
					if(p==0x11) return ('3');
					if(p==0x82) return ('4');
					if(p==0x42)	return ('5');
					if(p==0x22)	return ('6');
					if(p==0x12)	return ('7');
					if(p==0x84)	return ('8');
					if(p==0x44)	return ('9');
					if(p==0x24)	return ('A');
					if(p==0x14)	return ('B');
					if(p==0x88)	return ('C');
					if(p==0x48)	return ('D');
					if(p==0x28)	return ('E');
					if(p==0x18) return ('F');
				}
				else	h=(h<<1)|0x01;
			}
		}
   }
return (0xff);
} 
/***********************主函数**************************/
void main(void)
{
	LCDInit();	//LCD初始化
	delay5ms();
	
	while(1)
	{
		uchar n;
		DisplayListChar(3,0,Char1);
		DisplayListChar(0,1,Char2);
		DisplayOneChar(12,0,Char3);
		n=keyscan();
		if(n!=0xff)
			Char3=keyscan();
			DisplayOneChar(12,0,Char3);	
	}
 	
}
	











⌨️ 快捷键说明

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