版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
版权
单片机专栏收录该内容
14 篇文章0 订阅
订阅专栏

矩阵键盘

矩阵键盘用来控制功能的选择
按键的返回值又分为物理键值和逻辑键值,物理键值是按键实际的01代码,而逻辑键值是01代码翻译而来的十进制数。

key.c

#include "myhead.h" unsigned char key_scan(void)//返回按键的物理键值 { unsigned char keyvalue; KEYPORT =0xf0;if(0xf0 != KEYPORT)//有按键按下 { delay_ms(10);if(0xf0 != KEYPORT)//确实有按键按下 { KEYPORT =0xfe;if(0xfe != KEYPORT) { keyvalue = KEYPORT; keyvalue &=0xf0; keyvalue +=0x0e;while(0xfe != KEYPORT);//有按键抬起 delay_ms(10);while(0xfe != KEYPORT);//确实有按键抬起return keyvalue; } KEYPORT =0xfd;if(0xfd != KEYPORT) { keyvalue = KEYPORT; keyvalue &=0xf0; keyvalue +=0x0d;while(0xfd != KEYPORT);//有按键抬起 delay_ms(10);while(0xfd != KEYPORT);//确实有按键抬起return keyvalue; } KEYPORT =0xfb;if(0xfb != KEYPORT) { keyvalue = KEYPORT; keyvalue &=0xf0; keyvalue +=0x0b;while(0xfb != KEYPORT);//有按键抬起 delay_ms(10);while(0xfb != KEYPORT);//确实有按键抬起return keyvalue; } KEYPORT =0xf7;if(0xf7 != KEYPORT) { keyvalue = KEYPORT; keyvalue &=0xf0; keyvalue +=0x07;while(0xf7 != KEYPORT);//有按键抬起 delay_ms(10);while(0xf7 != KEYPORT);//确实有按键抬起return keyvalue; } } }