/* * Vaja4.c * * Created: 22.03.2013 13:12:32 * Author: maticpi */ #include void Init(); char GetKey(); void ReadKBD(); char lastkey; int main(void) { char cmd; Init(); while(1) { ReadKBD(); if ((cmd=GetKey())) { switch (cmd) { case 1: PORTB++; break; case 2: PORTB--; break; case 3: PORTB = 0; break; case 4: PORTD ^= 0x80; break; default: break; } } } } void Init() { //IO port init PORTA=0x08; PORTB=0x01; PORTC=0x3E; //0011 1110 PORTD=0x7D; //0111 1101 //1-out, 0-in DDRA=0xF0; DDRB=0xFF; DDRC=0xC1; DDRD=0x82; } void ReadKBD() { static char oldD; char newD=(PIND>>2) & 0x0F; char pressed = (newD ^ oldD) & oldD; if (pressed & 0x01) lastkey=1; if (pressed & 0x02) lastkey=2; if (pressed & 0x04) lastkey=3; if (pressed & 0x08) lastkey=4; oldD=newD; } char GetKey() { char tmp=lastkey; lastkey=0; return tmp; }