Source Code:

    

      
#include <avr/io.h> #define F_CPU 8000000UL #include <util/delay.h> #define KEY_PRT PORTC #define KEY_DDR DDRC #define KEY_PIN PINC unsigned char keypad[4][4] = {{'0','1','2','3'}, {'4','5','6','7'}, {'8','9','A','B'}, {'C','D','E','F'}}; int main () { unsigned char colloc, rowloc; DDRD = 0xFF; while(1) { while(1) { KEY_DDR = 0xF0; KEY_PRT = 0xFF; 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); 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); if(colloc != 0x0F) { rowloc = 3; break; } } if(colloc == 0x0E) PORTD = keypad[rowloc][0]; else if(colloc == 0x0D) PORTD = keypad[rowloc][1]; else if(colloc == 0x0B) PORTD = keypad[rowloc][2]; else PORTD = keypad[rowloc][3]; } return 0; }