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

📄 44.c

📁 44键盘液晶显示程序
💻 C
字号:
#include <reg51.h>

#define uchar unsigned char
#define uint  unsigned int
                                                      
#define LCD_Data  P0
#define Busy    0x80  //用于检测LCD状态字中的Busy标识

unsigned char temp; 
unsigned char key; 
unsigned char i,j;
 
  
sbit RS = P3^5;	 //定义LCD引脚
sbit RW = P3^6; 
sbit E  = P3^7;

sbit P2_4 = P2^4;
sbit P2_5 = P2^5;
sbit P2_6 = P2^6;
sbit P2_7 = P2^7;

void System_Initial(void);
void Delay5Ms(void);
void Delay400Ms(void);

void LCDInit(void);
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);  //指定位置显示一个字符
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);   //指定位置显示一串字符
void WriteDataLCD(unsigned char WDLCD);   //LCD写数据子程序
void WriteCommandLCD(unsigned char WCLCD,BuysC);  //LCD写指令子程序
unsigned char ReadStatusLCD(void);//读液晶的状态

unsigned char code Test[] = {"  4*4 Keyboard  "};
unsigned char code clr[]={"                "};
uchar DataNo = 0;
  


void System_Initial(void)
{
    Delay400Ms(); //启动等待,等LCD讲入工作状态
	LCDInit(); //LCD初始化	

	DisplayListChar(0, 0, Test);
	DisplayListChar(0,1,clr);	 
}

//写数据
void WriteDataLCD(unsigned char WDLCD) 
{
	ReadStatusLCD(); //检测忙
	LCD_Data=WDLCD;
	RS=1;
	RW=0;
	E=0; //若晶振速度太高可以在这后加小的延时
	E=0; //延时
	E=1;
}

//写指令
void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC为0时忽略忙检测
{
	if (BuysC) ReadStatusLCD(); //根据需要检测忙
	LCD_Data = WCLCD;
	RS = 0;
	RW = 0;	
	E = 0;
	E = 0;
	E = 1;	
}
//读状态
unsigned char ReadStatusLCD(void)
{
	LCD_Data=0xFF; 
	RS=0;
	RW=1;
	E=0;
	E=0;
	E=1;
	while(LCD_Data & Busy); //检测忙信号
	return(LCD_Data);
}

void LCDInit(void) //LCD初始化
{
	LCD_Data=0;
	WriteCommandLCD(0x38,0); //三次显示模式设置,不检测忙信号
	Delay5Ms(); 
	WriteCommandLCD(0x38,0);
	Delay5Ms(); 
	WriteCommandLCD(0x38,0);
	Delay5Ms(); 

	WriteCommandLCD(0x38,1); //显示模式设置,开始要求每次检测忙信号
	WriteCommandLCD(0x08,1); //关闭显示
	WriteCommandLCD(0x01,1); //显示清屏
	WriteCommandLCD(0x06,1); // 显示光标移动设置
	WriteCommandLCD(0x0F,1); // 显示开及光标设置
}

//按指定位置显示一个字符
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
	Y &= 0x1;
	X &= 0xF; //限制X不能大于15,Y不能大于1
	if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
	X |= 0x80; //算出指令码
	WriteCommandLCD(X, 1); //写命令字
	WriteDataLCD(DData); //写数据
}

//按指定位置显示一串字符
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
	unsigned char ListLength;
    ListLength = 0;
	Y &= 0x1;
	X &= 0xF; //限制X不能大于15,Y不能大于1,即控制在16列*2行的范围内
	while (DData[ListLength]>0x00) //若到达字串尾则退出
		{
			if (X <= 0xF) //X坐标应小于0xF
				{
					DisplayOneChar(X, Y, DData[ListLength]); //显示单个字符
					ListLength++;
					X++;
				}
		}
}

//5ms延时
void Delay5Ms(void)
{
	unsigned int i;
	for(i=0;i<5552;i++)
	{};
}

//400ms延时
void Delay400Ms(void)
{
	unsigned char i;
	unsigned int j;
	for(i=0;i<5;i++)
	{
	 for(j=0;j<7269;j++)
	 {};
	}
}

void main(void) 
{ 
  System_Initial();

  while(1) 
    { 
      P2=0xff; 
      P2_4=0; 
      temp=P2; 
      temp=temp & 0x0f; 
      if (temp!=0x0f) 
        { 
          for(i=50;i>0;i--) 
          for(j=200;j>0;j--); 
          temp=P2; 
          temp=temp & 0x0f; 
          if (temp!=0x0f) 
            { 
              temp=P2; 
              temp=temp & 0x0f;              
              switch(temp) 
                { 
                  case 0x0e: 
                    key='0'; 
                    break; 
                  case 0x0d: 
                    key='4'; 
                    break; 
                  case 0x0b: 
                    key='8'; 
                    break; 
                  case 0x07: 
                    key='C'; 
                    break; 
                } 
              temp=P2; 
              DisplayOneChar(DataNo,1,key);
			  DataNo++;  
              temp=temp & 0x0f;              
              while(temp!=0x0f) 
                { 
                  temp=P2; 
                  temp=temp & 0x0f;              
                } 
            } 
        } 


      P2=0xff; 
      P2_5=0; 
      temp=P2; 
      temp=temp & 0x0f;
      if (temp!=0x0f) 
        { 
          for(i=50;i>0;i--) 
          for(j=200;j>0;j--); 
          temp=P2; 
          temp=temp & 0x0f; 
          if (temp!=0x0f) 
            { 
              temp=P2; 
              temp=temp & 0x0f;              
              switch(temp) 
                { 
                  case 0x0e: 
                   key='1'; 
                    break; 
                  case 0x0d: 
                    key='5'; 
                    break; 
                  case 0x0b: 
                    key='9'; 
                    break; 
                  case 0x07: 
                    key='D'; 
                    break; 
                } 
              temp=P2; 
              DisplayOneChar(DataNo,1,key);
			  DataNo++; 
              temp=temp & 0x0f;              
              while(temp!=0x0f) 
                { 
                  temp=P2; 
                  temp=temp & 0x0f;              
                } 
            } 
        } 


      P2=0xff; 
      P2_6=0; 
      temp=P2; 
      temp=temp & 0x0f; 
      if (temp!=0x0f) 
        { 
          for(i=50;i>0;i--) 
          for(j=200;j>0;j--); 
          temp=P2; 
          temp=temp & 0x0f; 
          if (temp!=0x0f) 
            { 
              temp=P2; 
              temp=temp & 0x0f;              
              switch(temp) 
                { 
                  case 0x0e: 
                    key='2'; 
                   break; 
                  case 0x0d: 
                    key='6'; 
                    break; 
                  case 0x0b: 
                   key='A'; 
                    break; 
                  case 0x07: 
                    key='E'; 
                    break; 
                } 
              temp=P2; 
              DisplayOneChar(DataNo,1,key); 
			  DataNo++;
              temp=temp & 0x0f;              
              while(temp!=0x0f) 
                { 
                  temp=P2; 
                  temp=temp & 0x0f;              
                } 
            } 
        } 


      P2=0xff; 
      P2_7=0; 
      temp=P2; 
      temp=temp & 0x0f; 
      if (temp!=0x0f) 
        { 
          for(i=50;i>0;i--) 
          for(j=200;j>0;j--); 
          temp=P2; 
          temp=temp & 0x0f; 
          if (temp!=0x0f) 
           { 
              temp=P2; 
              temp=temp & 0x0f;              
              switch(temp) 
                { 
                  case 0x0e: 
                    key='3'; 
                   break; 
                  case 0x0d: 
                    key='7'; 
                    break; 
                  case 0x0b: 
                    key='B'; 
                    break; 
                  case 0x07: 
                    key='F'; 
                    break; 
                } 
             temp=P2; 
			 DisplayOneChar(DataNo,1,key); 
			 DataNo++;
             temp=temp & 0x0f;              
              while(temp!=0x0f) 
                { 
                  temp=P2; 
                  temp=temp & 0x0f;              
                } 
            } 
        }
		if(DataNo > 16) 
		{
			DataNo=0;  
			for(i=0;i<16;i++)
		    {
			 	DisplayOneChar(i, 1,' ');			 
			}
			DisplayOneChar(DataNo,1,key);
			DataNo++;  
		} 
    } 
}

⌨️ 快捷键说明

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