I ve written a function in mikroC that scan the pressed key in a 4x4 keypad
void scan_key()
{
PORTB = 0B11111110;
if ( PORTB == 0b11101110){
Row = 1;
Column = 1;
return;
}
if ( PORTB == 0b11011110){
Row = 2;
Column = 1;
return;
}
if ( PORTB == 0b10111110){
Row = 3;
Column = 1;
return;
}
if ( PORTB == 0b01111110){
Row = 4;
Column = 1;
return;
}
PORTB = 0B11111101;
if ( PORTB == 0b11101101){
Row = 1;
Column = 2;
return;
}
if ( PORTB == 0b11011101){
Row = 2;
Column = 2;
return;
}
if ( PORTB == 0b10111101){
Row = 3;
Column = 2;
return;
}
if ( PORTB == 0b01111101){
Row = 4;
Column = 2;
return;
}
PORTB = 0B11111011;
if ( PORTB == 0b11101011){
Row = 1;
Column = 3;
return;
}
if ( PORTB == 0b11011011){
Row = 2;
Column = 3;
return;
}
if ( PORTB == 0b10111011){
Row = 3;
Column = 3;
return;
}
if ( PORTB == 0b01111011){
Row = 4;
Column = 3;
return;
}
PORTB = 0B11110111;
if ( PORTB == 0b11100111){
Row = 1;
Column = 4;
return;
}
if ( PORTB == 0b11010111){
Row = 2;
Column = 4;
return;
}
if ( PORTB == 0b10110111){
Row = 3;
Column = 4;
return;
}
if ( PORTB == 0b01110111){
Row = 4;
Column = 4;
return;
}
PORTB = 0B11110000;
}
Is there a way to convert this algorithm into a loop?