这是4X4按键映射为0-F的16个字符输入并显现在屏上的程序:
[这儿要额定的把单片机p1的8个脚和矩阵键盘的8个脚相连]
1 /*--2 称号:LCD16023 论坛:www.doflye.net4 编写:shifang5 内容:经过矩阵键盘输入,顺次显现0-F16中字符6 引脚界说如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK7 */8 #include//包括头文件,一般状况不需要改动,头文件包括特别功用寄存器的界说9 #include 10 11 sbit RS = P2^4; //界说端口 12 sbit RW = P2^5;13 sbit EN = P2^6;14 15 #define RS_CLR RS=0 16 #define RS_SET RS=117 18 #define RW_CLR RW=0 19 #define RW_SET RW=1 20 21 #define EN_CLR EN=022 #define EN_SET EN=123 24 #define DataPort P025 #define KeyPort P126 27 unsigned char code dofly_code[]={0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F};//转换成液晶显现的字符28 /*29 uS延时函数,含有输入参数 unsigned char t,无回来值30 unsigned char 是界说无符号字符变量,其值的规模是31 0~255 这儿运用晶振12M,准确延时请运用汇编,大致延时32 长度如下 T=tx2+5 uS 33 */34 void DelayUs2x(unsigned char t)35 { 36 while(--t);37 }38 /*39 mS延时函数,含有输入参数 unsigned char t,无回来值40 unsigned char 是界说无符号字符变量,其值的规模是41 0~255 这儿运用晶振12M,准确延时请运用汇编42 */43 void DelayMs(unsigned char t)44 {45 46 while(t--)47 {48 //大致延时1mS49 DelayUs2x(245);50 DelayUs2x(245);51 }52 }53 /*54 判忙函数55 */56 bit LCD_Check_Busy(void) 57 { 58 DataPort= 0xFF; 59 RS_CLR; 60 RW_SET; 61 EN_CLR; 62 _nop_(); 63 EN_SET;64 return (bit)(DataPort & 0x80);65 }66 /*67 写入指令函数68 */69 void LCD_Write_Com(unsigned char com) 70 { 71 // while(LCD_Check_Busy()); //忙则等候72 DelayMs(5);73 RS_CLR; 74 RW_CLR; 75 EN_SET; 76 DataPort= com; 77 _nop_(); 78 EN_CLR;79 }80 /*81 写入数据函数82 */83 void LCD_Write_Data(unsigned char Data) 84 { 85 //while(LCD_Check_Busy()); //忙则等候86 DelayMs(5);87 RS_SET; 88 RW_CLR; 89 EN_SET; 90 DataPort= Data; 91 _nop_();92 EN_CLR;93 }94 /*95 清屏函数96 */97 void LCD_Clear(void) 98 { 99 LCD_Write_Com(0x01); 100 DelayMs(5);101 }102 /*103 写入字符串函数104 */105 void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) 106 { 107 if (y == 0) 108 { 109 LCD_Write_Com(0x80 + x); 110 }else 112 { 113 LCD_Write_Com(0xC0 + x); 114 } 115 while (*s) 116 { 117 LCD_Write_Data( *s); 118 s ++; 119 }120 }121 /*122 写入字符函数123 */124 void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) 125 { 126 if (y == 0) 127 { 128 LCD_Write_Com(0x80 + x); 129 } 130 else 131 { 132 LCD_Write_Com(0xC0 + x); 133 } 134 LCD_Write_Data( Data); 135 }136 /*137 初始化函数138 */139 void LCD_Init(void) 140 {141 LCD_Write_Com(0x38); /*显现形式设置*/ 142 DelayMs(5); 143 LCD_Write_Com(0x38); 144 DelayMs(5); 145 LCD_Write_Com(0x38); 146 DelayMs(5); 147 LCD_Write_Com(0x38); 148 LCD_Write_Com(0x08); /*显现封闭*/ 149 LCD_Write_Com(0x01); /*显现清屏*/ 150 LCD_Write_Com(0x06); /*显现光标移动设置*/ 151 DelayMs(5); 152 LCD_Write_Com(0x0C); /*显现开及光标设置*/153 }154 155 /*156 按键扫描函数,回来扫描键值157 */158 unsigned char KeyScan(void) //键盘扫描函数,运用队伍回转扫描法159 {160 unsigned char cord_h,cord_l;//队伍值中心变量161 KeyPort=0x0f; //行线输出全为0162 cord_h=KeyPort&0x0f; //读入列线值163 if(cord_h!=0x0f) //先检测有无按键按下164 { 165 DelayMs(10); //去抖166 if((KeyPort&0x0f)!=0x0f)167 {168 cord_h=KeyPort&0x0f; //读入列线值169 KeyPort=cord_h0xf0; //输出当前列线值170 cord_l=KeyPort&0xf0; //读入行线值171 172 while((KeyPort&0xf0)!=0xf0);//等候松开并输出173 174 return(cord_h+cord_l);//键盘最终组合码值175 }176 }return(0xff); //回来该值177 }178 /*179 按键值处理函数,回来扫键值180 */181 unsigned char KeyPro(void)182 {183 switch(KeyScan())184 {185 case 0x7e:return 0;break;//0 按下相应的键显现相对应的码值186 case 0x7d:return 1;break;//1187 case 0x7b:return 2;break;//2188 case 0x77:return 3;break;//3189 case 0xbe:return 4;break;//4190 case 0xbd:return 5;break;//5191 case 0xbb:return 6;break;//6192 case 0xb7:return 7;break;//7193 case 0xde:return 8;break;//8194 case 0xdd:return 9;break;//9195 case 0xdb:return 10;break;//a196 case 0xd7:return 11;break;//b197 case 0xee:return 12;break;//c198 case 0xed:return 13;break;//d199 case 0xeb:return 14;break;//e200 case 0xe7:return 15;break;//f201 default:return 0xff;break;202 }203 }204 205 /*206 主函数207 */ 208 void main(void) 209 { 210 unsigned char i,j,num; 211 212 LCD_Init(); 213 LCD_Write_Com(0x0F);//光标开,光标业绩开214 215 LCD_Write_String(0,0,"Press the key !");216 217 while (1) 218 { 219 num=KeyPro();220 if(num!=0xff)221 {if((i==0)&&(j==0))//回到榜首个字符时清屏223 LCD_Clear();//清屏224 LCD_Write_Char(0+i,0+j,dofly_code[num]);//顺次显现输入字符225 i++;226 if(i==16)//假如榜首行显现满,转到第二行227 {228 i=0;j++;229 if(j==2)//假如2行都显现满,清屏后从头从榜首行显现230 {231 j=0;232 }233 }234 }235 }236 }