Hi everyone,
I'm using PIC16F1459 and MPLAB v8.92 with the XC8 compiler...
I am trying to display a variable onto an LCD, the variable is the number pressed on a keypad. I get a few numbers now and again and sometimes it lags or doesn't respond. It is definitely a bug in the code so have a look for yourself and see if you can spot something.
All LCD/keypad/Pic config is done earlier in the code but that is all correct, the problem lies somewhere in this code.
Cheers
JSwale
I'm using PIC16F1459 and MPLAB v8.92 with the XC8 compiler...
I am trying to display a variable onto an LCD, the variable is the number pressed on a keypad. I get a few numbers now and again and sometimes it lags or doesn't respond. It is definitely a bug in the code so have a look for yourself and see if you can spot something.
Code:
//--------------------Keypad Scan------------------------------------
void Scan(void)
{
numberpressed = 12;
while (1) //Starting point, there is no number 12, means its polling
{
col1=0;
col2=1;
col3=1; //B4 is 0, scans for 1,4,7,*
if (row1==0)
{
__delay_ms(10); //used for de-bounce purposes
while (row1==0); //waits for switch to be released
__delay_ms(10); //used for de-bounce purposes
numberpressed=1;
return; //'return' leaves current subroutine
}
if (row2==0)
{
__delay_ms(10);
while (row2==0);
__delay_ms(10);
numberpressed=4;
return;
}
if (row3==0)
{
__delay_ms(10);
while (row3==0);
__delay_ms(10);
numberpressed=7;
return;
}
if (row4==0)
{
__delay_ms(10);
while (row4==0);
__delay_ms(10);
numberpressed=10;
return;
}
col1=1; //B5 is 0, scans for 2,5,8,0
col2=0;
col3=1;
if (row1==0)
{
__delay_ms(10);
while (row1==0);
__delay_ms(10);
numberpressed=2;
return;
}
if (row2==0)
{
__delay_ms(10);
while (row2==0);
__delay_ms(10);
numberpressed=5;
return;
}
if (row3==0)
{
__delay_ms(10);
while (row3==0);
__delay_ms(10);
numberpressed=8;
return;
}
if (row4==0)
{
__delay_ms(10);
while (row4==0);
__delay_ms(10);
numberpressed=0;
return;
}
col1=1; //B6 is 0, scans for 3,6,9,#
col2=1;
col3=0;
if (row1==0)
{
__delay_ms(10);
while (row1==0);
__delay_ms(10);
numberpressed=3;
return;
}
if (row2==0)
{
__delay_ms(10);
while (row2==0);
__delay_ms(10);
numberpressed=6;
return;
}
if (row3==0)
{
__delay_ms(10);
while (row3==0);
__delay_ms(10);
numberpressed=9;
return;
}
if (row4==0)
{
__delay_ms(10);
while (row4==0);
__delay_ms(10);
numberpressed=11;
return;
}
}
}
//======================= MAIN =====================================
void main (void)
{
initIO(); // init the chip IO
__delay_ms(300); // power up delay for LCD - adjust as necessary
lcd_init(); // init the LCD
lcd_WriteStr("I am Working");
lcd_SetCursor(lcdLINE2);
while(1)
{
if (numberpressed==1) break;
while(1)
{
Scan(); //poll for number pressed
num1=(numberpressed + 0x30); //add 0x30 for ascii
__delay_ms(1); //1ms delay
lcd_SetCursor(lcdLINE2); //set cursor to line1
lcd_data(num1); //display key pressed
break; //break out of while loop
} //while
} //while
lcd_SetCursor(lcdLINE1); //sets cursor to line1
lcd_WriteStr("UNLOCKED ");
lcd_SetCursor(lcdLINE2); //sets cursor to line2
lcd_WriteStr("UNLOCKED ");
while(1); // loop forever
} //main
Cheers
JSwale