#include #include #define No_key 255 #define K1_11 #define K1_22 #define K1_33 #define K1_4 4 #define K2_15 #define K2_26 #define K2_37 #define K2_4 8 #define K3_19 #define K3_20 #define K3_310 #define K3_4 11 #define K4_112 #define K4_213 #define K4_314 #define K4_4 15 #define Key_mask0b00001111 #define data PORTB.0 #define clk PORTB.1 flash unsigned char led[16]={0x28,0xeb,0x19,0x89,0xca,0x8c,0x0c,0xe9,0x08,0x88, 0xdf,0x4a,0x1c,0x3e,0x7e,0x2a}; unsigned charkey_stime_counter; unsigned char key_temp; bitkey_stime_ok; //D0接鍵盤,低4位列線輸入,高4位行線輸出 unsigned char read_keyboard() { static unsigned char key_state = 0, key_value, key_line; unsigned char key_return = No_key,i; switch (key_state) { case 0: key_line = 0b00010000; for (i=1; i<=4; i++)// 掃描鍵盤 { PORTA = ~key_line;// 輸出行線電平 PORTA = ~key_line;// 必須送2次?。?! key_value = Key_mask & PINA;// 讀列電平 if (key_value == Key_mask) key_line <<= 1;// 沒有按鍵,繼續(xù)掃描 else { key_state++;// 有按鍵,停止掃描 break;// 轉(zhuǎn)消抖確認(rèn)狀態(tài) } } break; case 1: if (key_value == (Key_mask & PINA))// 再次讀列電平, { switch (key_line | key_value)// 與狀態(tài)0的相同,確認(rèn)按鍵 {// 鍵盤編碼,返回編碼值 case 0b00011110: key_return = K1_1; break; case 0b00011101: key_return = K1_2; break; case 0b00011011: key_return = K1_3; break; case 0b00010111: key_return = K1_4; break; case 0b00101110: key_return = K2_1; break; case 0b00101101: key_return = K2_2; break; case 0b00101011: key_return = K2_3; break; case 0b00100111: key_return = K2_4; break; case 0b01001110: key_return = K3_1; break; case 0b01001101: key_return = K3_2; break; case 0b01001011: key_return = K3_3; break; case 0b01000111: key_return = K3_4; break; case 0b10001110: key_return = K4_1; break; case 0b10001101: key_return = K4_2; break; case 0b10001011: key_return = K4_3; break; case 0b10000111: key_return = K4_4; break; } key_state++;// 轉(zhuǎn)入等待按鍵釋放狀態(tài) } else { key_state--; delay_ms(5); }// 兩次列電平不同返回狀態(tài)0,(消抖處理) break; case 2:// 等待按鍵釋放狀態(tài) PORTA = 0b00001111;// 行線全部輸出低電平 PORTA = 0b00001111;// 重復(fù)送一次 if ( (Key_mask & PINA) == Key_mask) key_state=0;// 列線全部為高電平返回狀態(tài)0 break; } return key_return; } //向數(shù)碼管送入數(shù)據(jù) void sendbyte(unsigned char byte) { unsigned char num,c; num=led[byte]; for(c=0;c<8;c++) { clk=0; data=num&0x01; clk=1; num>>=1; } } void display(void) { if (key_stime_ok) { key_stime_ok = 0;// 10ms到 key_temp = read_keyboard();// 調(diào)用鍵盤接口函數(shù)讀鍵盤 if (key_temp != No_key) {// 有按鍵按下 sendbyte(key_temp); delay_ms(10); } } } void main(void) { DDRB = 0xFF; //PORTC = 0xFF;// 鍵盤接口初始化 DDRA = 0xF0;// PD2、PD1、PD0列線,輸入方式,上拉有效 // T/C0 初始化 TCCR0=0x0B;// 內(nèi)部時鐘,64分頻(4M/64=62.5KHz),CTC模式 TCNT0=0x00; OCR0=0x7C;// OCR0 = 0x7C(124),(124+1)/62.5=2ms TIMSK=0x02;// 允許T/C0比較匹配中斷 #asm("sei")// 開放全局中斷 while (1) { display(); } } // Timer 0 比較匹配中斷服務(wù),2ms定時 interrupt [TIM0_COMP] void timer0_comp_isr(void) { //display();// 調(diào)用LED掃描顯示 if (++key_stime_counter >=5) { key_stime_counter = 0; key_stime_ok = 1;// 10ms到 } }
評論