#include <htc.h>
__CONFIG(0x3f72);
void ScanCol(void); //Column Scan Function
void ScanRow(void); //Row Scan Function
void DelayMs(unsigned int);
void setout();
unsigned char KeyArray[4][4]= { '1','2','3','4',
'5','6','7','8',
'9','A','B','C',
'D','E','F','0'};
int Col=0,Row=0,i,j;
void main()
{
TRISD=0xFF;
PORTD=0x00;
DelayMs(50);
nRBPU=0; //Enable PORTB Pullup values
while(1)
{
TRISB=0x0f; // Enable the 4 LSB as I/P & 4 MSB as O/P
PORTB=0x00;
while(PORTB==0x0f); // Get the ROW value
ScanRow();
TRISB=0xf0; // Enable the 4 LSB as O/P & 4 MSB as I/P
PORTB=0x00;
while(PORTB==0xf0); // Get the Column value
ScanCol();
DelayMs(150);
setout();
DelayMs(200);
}
}
void setout()
{
switch(KeyArray[Row][Col])
{
case '1':
PORTD=0x01;
break;
case '2':
PORTD=0x02;
break;
case '3':
PORTD=0x03;
break;
case '4':
PORTD=0x04;
break;
case '5':
PORTD=0x05;
break;
case '6':
PORTD=0x06;
break;
case '7':
PORTD=0x07;
break;
case '8':
PORTD=0x08;
break;
case '9':
PORTD=0x09;
break;
case 'A':
PORTD=0x0A;
break;
case 'B':
PORTD=0x0B;
break;
case 'C':
PORTD=0x0C;
break;
case 'D':
PORTD=0x0D;
break;
case 'E':
PORTD=0x0E;
break;
case 'F':
PORTD=0x0F;
break;
case '0':
PORTD=0xFF;
break;
default:
PORTD=0xFF;
break;
}
return;
}
void ScanRow() // Row Scan Function
{
switch(PORTB)
{
case 0x07:
Row=3; // 4th Row
break;
case 0x0b:
Row=2; // 3rd Row
break;
case 0x0d:
Row=1; // 2nd Row
break;
case 0x0e:
Row=0; // 1st Row
break;
}
return;
}
void ScanCol() // Column Scan Function
{
TRISB=0xf0; // Enable the 4 LSB as O/P & 4 MSB as I/P
switch(PORTB)
{
case 0x70:
Col=3; // 4th Column
break;
case 0xb0:
Col=2; // 3rd Column
break;
case 0xd0:
Col=1; // 2nd Column
break;
case 0xe0:
Col=0; // 1st Column
break;
}
return;
}
void DelayMs(unsigned int Ms)
{
int delay_cnst;
while(Ms>0)
{
Ms--;
for(delay_cnst = 0;delay_cnst <220;delay_cnst++);
}
}