在51hei論壇吸取各位前輩的經(jīng)驗,將之前二極管用量多的問題優(yōu)化一下,目前不用二極管能接6鍵,2只二極管能接12鍵,6只二極管能接18鍵,9只二極管能接21鍵,第22鍵要單獨占用3只二極管最不化算。
實驗用89S51作試驗,電路接線就是P1.2,P1.3,P1.4接鍵盤,P1.0接顯示器。
- /*==================================================================*
- * 3個IO接識別22鍵測試程序 *
- * ------------------------------------------------ *
- * MCU: AT89C2051 *
- * OSC: 12M cysytel *
- * 程序設(shè)計:Cowboy *
- * 程序版本:V1.0 *
- *==================================================================*/
- #include<reg52.h>
- //================== IO口線連接 ==================
- sbit Bus = P1^0;
- sbit IO_a = P1^4;
- sbit IO_b = P1^3;
- sbit IO_c = P1^2;
- //================== 變量聲明 ====================
- unsigned char Disp_buf[3];
- unsigned char Dig;
- unsigned char Key_count;
- unsigned char bdata Key_state;
- sbit KB0 = Key_state^0;
- sbit KB1 = Key_state^1;
- sbit KB2 = Key_state^2;
- sbit KB3 = Key_state^3;
- sbit KB4 = Key_state^4;
- sbit KB5 = Key_state^5;
- //================== 表格數(shù)據(jù) ====================
- code unsigned char LED_font[24]=
- {
- 0x84,0x9f,0xa2,0x8a,0x99,0xc8,0xc0,0x9e,0x80, //012345678
- 0x88,0x90,0xc1,0xe4,0x83,0xe0,0xf0,0xff,0xfb, //9abcdef -
- };
- code unsigned char Key_tab[64]= //鍵碼映射表
- {// 0 1 2 3 4 5 6 7 8 9
- 22, 0, 2, 0, 0, 0, 0, 0, 4, 0, //0
- 0, 0, 0, 0, 0,18, 0, 0, 0, 0, //1X
- 0, 0, 0, 0, 0, 0, 3,14, 0, 0, //2X
- 20,10, 6, 0, 0, 0, 0, 0, 1,19, //3X
- 0, 5, 0, 0, 0,15, 0,11, 0, 0, //4X
- 0,17, 0, 0,13, 8, 0,21, 0, 9, //5X
- 16,12, 7, 0 //6X
- };
- //=============== 檢測按鍵 =================
- void Key_scan()
- {
- unsigned char i;
- Key_count --; //掃描次序
- Key_count &= 3;
- switch (Key_count) //按次序處理
- {
- case 2: //第一輪掃描
- KB0 = IO_b;
- KB1 = IO_c;
- IO_a = 1;
- IO_b = 0;
- break;
- case 1: //每二輪掃描
- KB2 = IO_c;
- KB3 = IO_a;
- IO_b = 1;
- IO_c = 0;
- break;
- case 0: //每三輪掃描
- KB4 = IO_a;
- KB5 = IO_b;
- IO_c = 1;
- break;
- default: //每四輪掃描
- if (!IO_a) KB0 = 0;
- if (!IO_b) KB2 = 0;
- if (!IO_c) KB4 = 0;
- IO_a = 0;
- //======更新顯示緩沖區(qū)=======
- i = Key_tab[Key_state];
- if (i == 0)
- {
- Disp_buf[2] = 0x11; //顯示三橫
- Disp_buf[1] = 0x11;
- Disp_buf[0] = 0x11;
- }
- else
- {
- Disp_buf[2] = 0x0c; //字符"C"
- Disp_buf[1] = i / 10; //鍵碼十位
- Disp_buf[0] = B;于 //鍵碼個位
- }
- Key_state = 0;
- }
- }
- /*===================================================================
- ONE WIRE 顯示總線驅(qū)動程序
- ===================================================================*/
- //=============== 發(fā)送一位 =================
- void Send_bit(bit Dat)
- {
- unsigned char i = 3;
- if (!Dat) Bus = 0;
- else
- {
- Bus = 0;
- Bus = 1;
- }
- while(--i); //延時8us
- Bus = 1;
- }
- //=============== 總線驅(qū)動 =================
- void Bus_drive()
- {
- unsigned char i = 0;
- unsigned char Sdat;
- Send_bit(1); //Bit6消隱
- do Bus = 1; while(--i); //延時768us
- do Bus = 0; while(--i); //延時768us
- Bus = 1;
- Sdat = LED_font[Disp_buf[Dig++]]; //獲取顯示數(shù)據(jù)
- Send_bit(Sdat & 0x01); //發(fā)送位0
- Send_bit(Sdat & 0x02); //發(fā)送位1
- Send_bit(Sdat & 0x04); //發(fā)送位2
- Send_bit(Sdat & 0x08); //發(fā)送位3
- Send_bit(Sdat & 0x10); //發(fā)送位4
- Send_bit(Sdat & 0x20); //發(fā)送位5
- Send_bit(Dig & 0x01); //發(fā)送位選1
- Send_bit(Dig & 0x02); //發(fā)送位選2
- while(--i); //延時512us
- Send_bit(Sdat & 0x40); //發(fā)送位6
- for (i = 7;i> 0;i--) Send_bit(1); //位6移至Dout
- if (Dig == 3) Dig = 0;
- }
- /*===================================================================
- 延時 5ms 程序
- ===================================================================*/
- void Delay_5ms()
- {
- while(!TF1);
- TF1 = 0;
- TH1 = (- 5000) / 256;
- TL1 = (- 5000) % 256;
- }
- /*===================================================================
- 主程序
- ===================================================================*/
- void main()
- {
- TMOD = 0x10; //定時器1,16位模式
- TCON = 0xc0; //TR1=1;TF1=1;
- while(1) //主循環(huán)
- {
- Bus_drive(); //顯示總線驅(qū)動
- Key_scan(); //檢測按鍵
- Delay_5ms(); //延時5MS
- }
- }
復制代碼 |