转载(留着慢慢看):void main(void) - the Wrong Thing
毕业设计的开始:传感器无线网络——入手的几个模块

关于单片机8个I/0口 实现36个按键功能

G-one posted @ 2010年3月28日 23:21 in program , 3013 阅读

 

算是闲着无聊弄了弄,不弄不知道一弄发现原来那么简单

先上图

这是整体仿真图,用51单片机接,数码管显示每个按键的返回值;

按键以8个起始 递减的链接,7、6、5...  因此共36个按键

下面是检测按键的简单程序

#include<reg51.h>
#include<intrins.h>

#define uchar unsigned char
#define uint unsigned int
#define LED P0
#define Keycheck P1

sbit LED1=P2^0;
sbit LED2=P2^1;
sbit LED3=P2^2;
sbit LED4=P2^3;


void delay(uchar);

uchar code NUM[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//公阳
	  			//0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71	公阴
uint DATA=0;


void LEDplay(void);
void Keych(void);
void delay(uchar);

int main(void)
{
   DATA=0X00;

	while(1)
	{
	Keych();
	LEDplay();
	}

}


void Keych(void)
{
uchar i=0,temp,Keycode;
uchar Keynum=0xfe;
Keycheck=0xff;


if(Keycheck!=0xff)	//I/O口全为高电平扫描列8个按键
	{ delay(5);
	  if(Keycheck!=0xff)
	    Keycode=Keycheck;
	}

else				   
	{								
		while((Keynum&0x80)!=0)		//每次一个低电平口,循环移动扫描按键
		{								   
		Keycheck=Keynum;
			if(Keycheck!=Keynum)
			{
			  delay(5);
			  if(Keycheck!=Keynum)
			  Keycode=Keycheck;	
			  break;
			}
			else
			Keynum=_crol_(Keynum,1);
			

		}

	} 
//else Keycode=0x00;   //无按键按下的返回值,这里为保持所找按键值,估不设定返回


 if(temp!=Keycode)
	{
	temp=Keycode;
	DATA=Keycode;
	}


}


void LEDplay(void)
{
P2=0x00;



	LED=NUM[DATA&0X0F];LED4=1;delay(3);LED4=0; 
	LED=NUM[DATA>>4];  LED3=1;delay(3);LED3=0; 
    LED=0x89;LED2=1;delay(3);LED2=0; 	  //0x89为共阳数码管H
    LED=NUM[0];LED1=1;delay(3);LED1=0; 

}



void delay(uchar x)
{
uchar k;
while(x--)
for(k=0;k<128;k++);

}

 

 

哎呀~ 为啥行距那么宽呢~?  下回再弄

按照图上的那种接法按键返回值分别为:

                                                                                                                                 0XFE

                                                                                                                0XFC        0XFD 

                                                                                               0XF9        0XFA        0XFB

                                                                              0XF3        0XF5        0XF6        0XF7

                                                            0XE7       0XEB        0XED        0XEE        0XEF

                                          0XCF        0XD7       0XDB       0XDD       0XDE        0XDF

                         0X9F        0XAF        0XB7        0XBB        0XBD       0XBE        0XBF

        0x3F        0X5F        0X6F        0X77        0X7B        0X7D        0X7E          0X7F

若返回值有出入纯粹 链接模式差异或程序上的的差异。原理一样即可;

Avatar_small
TLHL28 说:
2010年4月10日 10:24

噢噢.不错不错.
这个blog好把.:)


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter