標(biāo)題: 關(guān)于單片機(jī)矩形按鍵程序的問題 [打印本頁]

作者: zzh985514474    時(shí)間: 2019-5-18 15:17
標(biāo)題: 關(guān)于單片機(jī)矩形按鍵程序的問題
#include "reg52.h"                        

typedef unsigned int u16;         
typedef unsigned char u8;

#define GPIO_DIG P0
#define GPIO_KEY P1

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;

u8 KeyValue;        //用來存放讀取到的鍵值


u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                                        0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//顯示0~F的值


void delay(u16 i)
{
        while(i--);        
}


void KeyDown(void)
{
        char a=0;
        GPIO_KEY=0x0f;
        if(GPIO_KEY!=0x0f)
        {
                delay(1000);
                if(GPIO_KEY!=0x0f)
                {        
                        //測(cè)試列
                        GPIO_KEY=0X0F;
                        switch(GPIO_KEY)
                        {
                                case(0X07):        KeyValue=0;break;
                                case(0X0b):        KeyValue=1;break;
                                case(0X0d): KeyValue=2;break;
                                case(0X0e):        KeyValue=3;break;
                        }
                        //測(cè)試行
                        GPIO_KEY=0XF0;
                        switch(GPIO_KEY)
                        {
                                case(0X70):        KeyValue=KeyValue;break;
                                case(0Xb0):        KeyValue=KeyValue+4;break;
                                case(0Xd0): KeyValue=KeyValue+8;break;
                                case(0Xe0):        KeyValue=KeyValue+12;break;
                        }
                        /*while((a<50)&&(GPIO_KEY!=0xf0))         //檢測(cè)按鍵松手檢測(cè),   這一步松手 有啥子用? 我吧這注釋了也不會(huì)怎么樣,就是會(huì)響
                        {
                                delay(100);
                                a++;
                        }*/
                }
        }
}


void main()
{        
        LSA=0; //給一個(gè)數(shù)碼管提供位選
        LSB=0;
        LSC=0;
        while(1)
        {        
                KeyDown();                   //按鍵判斷函數(shù)
                GPIO_DIG=smgduan[KeyValue];          //
        }               
}







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1