我是初學(xué)的,我是想用矩陣按鍵鍵盤控制8*8的點陣顯示數(shù)字,可是我寫的程序按鍵控制不了數(shù)字,不知道哪里出現(xiàn)了問題,幫我改一下,謝謝~~
- #include<reg51.h>
- typedef unsigned int u16;
- typedef unsigned char u8;
- //--定義要使用的IO口--//
- #define READPORTS P1
- #define GPIO_KEY P2
- #define COMMONPORTS P0
-
- u8 KeyValue;
- 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)
- {
-
- 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;
- }
-
- 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))
- {
- delay(1000);
- a++;
- }
- }
- }
- }
- //--列選通控制--//
- unsigned char code TAB[8] = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
- //--點陣字碼--//
- unsigned char code CHARCODE[16][8]={
- {0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00}, //0
- {0x00,0x00,0x00,0x00,0x21,0x7f,0x01,0x00}, //1
- {0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00}, //2
- {0x00,0x00,0x22,0x49,0x49,0x49,0x36,0x00}, //3
- {0x00,0x00,0x0c,0x14,0x24,0x7f,0x04,0x00}, //4
- {0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00}, //5
- {0x00,0x00,0x3e,0x49,0x49,0x49,0x26,0x00}, //6
- {0x00,0x00,0x40,0x40,0x40,0x4f,0x70,0x00}, //7
- {0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00}, //8
- {0x00,0x00,0x32,0x49,0x49,0x49,0x3e,0x00}, //9
- {0x00,0x00,0x7F,0x48,0x48,0x30,0x00,0x00}, //P
- {0x00,0x00,0x7F,0x48,0x4C,0x73,0x00,0x00}, //R
- {0x00,0x00,0x7F,0x49,0x49,0x49,0x00,0x00}, //E
- {0x00,0x00,0x3E,0x41,0x41,0x62,0x00,0x00}, //C
- {0x00,0x00,0x7F,0x08,0x08,0x7F,0x00,0x00}, //H
- {0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00}, //I
- };
- void main()
- {
- unsigned char tab, j;
- unsigned int i; //無符號整數(shù)
- while(1)
- {
-
-
- for(i= 0; i<5000; i++ ) //兩個字之間的掃描間隔時間
- {
- KeyDown();
- COMMONPORTS=CHARCODE[KeyValue];
- for(tab=0;tab<8;tab++)
- {
- READPORTS=0xFF; //消隱
- COMMONPORTS=CHARCODE[j][tab]; //輸出字碼
- READPORTS=TAB[tab];
- }
- }
- j++;
- if(j == 16)
- {
- j = 0;
- }
- }
- }
復(fù)制代碼
|