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

📄 键盘输入液晶输出(键盘测试)13.c

📁 液晶模块显示实例 实现字符的输入 以及液晶模块使用的注意事项
💻 C
字号:
#include<at89x51.h>
sbit rs=P3^4;
sbit rw=P3^3;
sbit e=P3^2;
sbit d0=P1^0;
sbit d1=P1^1;
sbit d2=P1^2;
sbit d3=P1^3;
sbit d4=P1^4;
sbit d5=P1^5;
sbit d6=P1^6;
sbit d7=P1^7;//说明管脚连接
//P2口由于键盘的连接 
unsigned char store1=0,store2=0,store=0;//用于全局扫瞄键值决定用    	   
unsigned char k=0x80;  //显示地址变量 
void delay(unsigned char a)//延时a*1ms
           { 
		    unsigned char b;
			while(--a!=0)
			      {	 
			      for(b=0;b<125;b++);
			      }
		    }
void if_busy(void)//液晶忙否判断   
                {  P1=0xff;//很必要 
                   rs=0;rw=1;
                   e=0;e=1;
		           while(d7==1); //先执行读忙指令再能判断忙              
	        }
void rst_lcd(void)
               {
			    if_busy();
			    P1=0x01;
			    rs=0;rw=0; 
			    e=0;e=1;//执行清显示 	
			    if_busy();P1=0x38;  
			    rs=0;rw=0;	   
			    e=0;e=1;//显示模式设定:双行、5*7格式、8位数据转送	
			    if_busy();P1=0x0e;  
			    rs=0;rw=0;     
			    e=0;e=1;//显示设定:光标有且不闪动,开显示 
			    if_busy();P1=0x06;   
			    rs=0;rw=0;    
			    e=0;e=1;//光标和显示模式:光标右移,字不移动 
			   }
void show(unsigned char c)
           { 
		   if_busy();
			rs=0;rw=0;
			P1=k;if(k==0xc0)delay(20);//由于液晶是慢显示器件,因此需要换行地址的读取时先延时!很重要否则会出错     
		    e=0;e=1;//写入显示的地址   
			k=k+0x01;//地址移至下一位   
			if(k==0x90)
			   {
			    k=0xc0;//当到达第一行末尾时换行	  
			   }
            if_busy(); 
		    rs=1;rw=0;
			P1=c;      
			e=0;e=1;//执行 
			if(k==0xd0)
              {while(1);}//规定只能输入32个字符  
		    }		    
void  which(void)//进行按键的二次选择
            {
		    while(P2!=0x0f); //等待第一次按键的释放 
			delay(20);//按键释放去抖动 
			P2=0xf0;
			while(P2==0xf0);//等待第二次按键的输入    
			delay(20);//去抖动处理  
			if(P2!=0xf0)
			   {
			    store1=P2;				
			    P2=0x0f;
		        store2=P2;
			    store=store1|store2;//注意|是进行或运算 ;||是逻辑或 
			   }
			 }		   
void key11(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('a');//显示a,液晶的ascll码的字母和数字和计算机相同 ,一次可以这样赋值 
			    }
			 if(store==0xb7)
			   {
			    show('b');
			   }
			 if(store==0x7e)
			   {
			    show('c');
			   }
			  if(store==0x7d)
			  {
			   show('0');
			  }
		   }
void key21(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('d');//显示d
			    }
			 if(store==0xb7)
			   {
			    show('e');
			   }
			 if(store==0x7e)
			   {
			    show('f');
			   }
			  if(store==0x7d)
			  {
			   show('1');
			  }
		   }
void key31(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('g');//显示g
			    }
			 if(store==0xb7)
			   {
			    show('h');
			   }
			 if(store==0x7e)
			   {
			    show('i');
			   }
			  if(store==0x7d)
			  {
			   show('2');
			  }
		   }
void key41(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('j');//显示j 
			    }
			 if(store==0xb7)
			   {
			    show('k');
			   }
			 if(store==0x7e)
			   {
			    show('l');
			   }
			  if(store==0x7d)
			  {
			   show('3');
			  }
		   }
void key12(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('m');//显示m 
			    }
			 if(store==0xb7)
			   {
			    show('n');
			   }
			 if(store==0x7e)
			   {
			    show('o');
			   }
			  if(store==0x7d)
			  {
			   show('4');
			  }
		   }
void key22(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('p');//显示p 
			    }
			 if(store==0xb7)
			   {
			    show('q');
			   }
			 if(store==0x7e)
			   {
			    show('r');
			   }
			  if(store==0x7d)
			  {
			   show('5');
			  }
		   }
void key32(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('s');//显示s 
			    }
			 if(store==0xb7)
			   {
			    show('t');
			   }
			 if(store==0x7e)
			   {
			    show('u');
			   }
			  if(store==0x7d)
			  {
			   show('6');
			  }
		   }
void key42(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('v');//显示v 
			    }
			 if(store==0xb7)
			   {
			    show('w');
			   }
			 if(store==0x7e)
			   {
			    show('x');
			   }
			  if(store==0x7d)
			  {
			   show('7');
			  }
		   }
void key13(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show('y');//显示y 
			    }
			 if(store==0xb7)
			   {
			    show('z');
			   }
			 if(store==0x7e)
			   {
			    show('8');
			   }
			  if(store==0x7d)
			  {
			   show('9');
			  }
		   }
void key23(void)
           {
		    which();
			if(store==0xbb)
			   {
			    show(0x20);//显示逗号 
			    }
			 if(store==0xb7)//显示句号 
			   {
			    show('.');
			   }
			 if(store==0x7e)//显示感叹号  
			   {
			    show('!');
			   }
			  if(store==0x7d)//显示问号  
			  {
			   show('?');
			  }
		   }

void whichkey(void)
          {
		   P2=0x0f;
		   store2=P2;//由于按键未释放,因此store2的值不是0fH	  
		   store=store1|store2;//得到变化的键值 
		   if(store==0xee)//与键key11值比较	 
		      {
			  key11();//进入键的执行 
			  }
		   if(store==0xed)//与键key21比较 
		      {
			   key21();
			  }
		   if(store==0xeb)
		     {
			  key31();
			 }
		   if(store==0xe7)
		     {
			  key41();
			  }
			
			if(store==0xde)
			 {
			  key12();
			 }
			if(store==0xdd)
			 {
			  key22();
			 }
			if(store==0xdb)
			 {
			  key32();
			 }
			 if(store==0xd7)
			 {
			  key42();
			 }
			 if(store==0xbe)
			 {
			  key13();
			 }
			 if(store==0xbd)
			 {
			  key23();
			 }
		  }
		            			 
void main(void)
          { 
		   rst_lcd();//初始化液晶屏 
		   while(1)	//初始化后cpu一直进行按键的判断 
		   {
		   P2=0xf0;
		   if(P2!=0xf0)
		      {
			  delay(20);
			   if(P2!=0xf0)
			      {
				   store1=P2;
				   whichkey();//键值判断 
				   }
			  }
           }		    
		  }

⌨️ 快捷键说明

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