#include <avr/io.h> #define F_CPU 16000000UL #include <util/delay.h> #define KEY_PRT PORTD //keyboard PORT #define KEY_DDR DDRD #define KEY_PIN PIND unsigned char keypad[4][4] =//{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; {'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'}; int main(void) { unsigned char colloc, rowloc; DDRB = 0xFF; KEY_DDR = 0xF0; KEY_PRT = 0xFF; while(1) { do { KEY_PRT &= 0x0F; asm("NOP"); colloc = (KEY_PIN & 0x0F); } while(colloc != 0x0F); do { do { _delay_ms(20); colloc =(KEY_PIN&0x0F); } while(colloc == 0x0F); _delay_ms(20); colloc = (KEY_PIN & 0x0F); }while(colloc == 0x0F); while(1) { KEY_PRT = 0xEF; asm("NOP"); colloc = (KEY_PIN & 0x0F); if(colloc != 0x0F) { rowloc = 0; break; } KEY_PRT = 0xDF; asm("NOP"); colloc = (KEY_PIN & 0x0F); if(colloc != 0x0F) { rowloc = 1; break; } KEY_PRT = 0xBF; asm("NOP"); colloc = (KEY_PIN & 0x0F); if(colloc != 0x0F) { rowloc = 2; break; } KEY_PRT = 0x7F; asm("NOP"); colloc = (KEY_PIN & 0x0F); rowloc = 3; break; } if(colloc == 0x0E) PORTB = (keypad[rowloc][0]); else if(colloc == 0x0D) PORTB = (keypad[rowloc][1]); else if(colloc == 0x0B) PORTB = (keypad[rowloc][2]); else PORTB = (keypad[rowloc][3]); } return 0; }