|
马上注册,结交更多好友,享用更多功能,让你轻松玩转英山论坛。
您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 华尔 于 2011-1-4 10:24 编辑
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar key=16,key_p=16;
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, //共阴数码管段选码表,无小数点
-
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
- void delay(uint ms)
- {
- uchar i;
- while(ms--)
- for(i=0;i<110;i++);
- }
- void key_press()
- {
- uchar kess;
- P1=0x0f;
- delay(1);
- kess=P1^0x0f; //P1口的状态异或上0x0f,如果下了高4位不管,看低4位是多少
- switch (kess)
- {
- case 1:key=0; break;
- case 2:key=1; break;
- case 4:key=2; break;
- case 8:key=3; break;
- default: key = 16;
- }
- P1=0xf0;
- delay(1);
- kess=P1>>4^0x0f;
- switch (kess)
- {
- case 1:key+=12;break;
- case 2:key+=8; break;
- case 4:key+=4; break;
- case 8:key+=0; break;
-
- }
-
-
- }
- void key_tst()
- {
- if(P1!=0xf0)
- delay(5);
- if(P1!=0xf0)
- key_press();
- if(key_p!=key)
-
- {
- P0=table[key];
- P2=0xff;
-
- key_p=key;
- }
- delay(100);
- }
- void main()
- {
- P0=0x00;
- while(1)
- {
- key_tst();
- P2=0xfe;
- }
- }
复制代码 |
|