Created
August 9, 2012 11:43
-
-
Save liang67/3303503 to your computer and use it in GitHub Desktop.
矩阵键盘-线反转法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <reg52.h> | |
| #define uchar unsigned char | |
| #define uint unsigned int | |
| #define ulong unsigned long | |
| // 常用数组定义 | |
| //uchar code DIS[16] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, // 0-F对应的字符 | |
| // 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8eF | |
| // }; | |
| uchar code DIS2[17] = // 多出来的是- | |
| { | |
| 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, // 0-F对应的字符 | |
| 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e, 0xbf | |
| }; | |
| uchar code KEY_CODE[16] = {0xee, 0xde, 0xbe, 0x7e, 0xed, 0xdd, 0xbd, 0x7d, | |
| 0xeb, 0xdb, 0xbb, 0x7b, 0xe7, 0xd7, 0xb7, 0x77 | |
| }; | |
| // 独立按键定义 | |
| sbit K5 = P1 ^ 4; | |
| sbit K6 = P1 ^ 5; | |
| sbit K7 = P1 ^ 6; | |
| sbit K8 = P1 ^ 7; | |
| sbit FMQ = P3 ^ 6; | |
| sbit P27 = P2 ^ 7; | |
| uchar key; | |
| // 常用函数 | |
| void delay1ms(uint t) // 延时 t ms | |
| { | |
| uchar i; | |
| while (t--) | |
| { | |
| for (i = 0; i < 115; ++i) | |
| { | |
| ; | |
| } | |
| } | |
| } | |
| void delay500us() // 500us延时 | |
| { | |
| uchar i; | |
| for (i = 0; i < 57; ++i) | |
| { | |
| ; | |
| } | |
| } | |
| void beep() //产生1KHZ频率声音的函数 | |
| { | |
| FMQ = 0; | |
| delay500us(); | |
| FMQ = 1; | |
| delay500us(); | |
| } | |
| // 键盘扫描子程序,返回一个uchar | |
| uchar keyScan() | |
| { | |
| uchar scan1, scan2, keycode, i; | |
| // P1 = 0XFF; // 先写1,再做IO | |
| P1 = 0xF0; | |
| scan1 = P1; // 取P1的值 | |
| if ((P1 & 0xF0) != 0xF0) // 与后不等于0xF0证明有按键按下 | |
| { | |
| delay1ms(20); // 延时消抖 | |
| scan1 = P1; // 在取P1的值 | |
| if ((P1 & 0xF0) != 0xF0) // 消抖后二次取值 | |
| { | |
| P1 = 0x0F; // 反转 | |
| scan2 = P1; // 取P1值 | |
| keycode = scan1 | scan2; // 取余 | |
| for (i = 0; i < 16; i++) | |
| { | |
| if ( keycode == KEY_CODE[i]) { | |
| key = i; | |
| return key; | |
| } | |
| } | |
| } | |
| } | |
| else P1 = 0xff; | |
| return 16; | |
| } | |
| void main() | |
| { | |
| uchar tt; | |
| P0 = 0xbf; // 数码管显示的字符"-" | |
| P27 = 0; // 数码管 | |
| P1 = 0XFF; | |
| while (1) | |
| { | |
| P1 = 0xf0; | |
| if ((P1 & 0xf0) != 0xf0) | |
| { | |
| keyScan(); | |
| P0 = DIS2[key]; | |
| for (tt = 0; tt < 200; tt++) | |
| { | |
| beep(); | |
| } | |
| FMQ = 1; | |
| delay1ms(200); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
为什么用函数的返回值不行?用全局变量就可以了