Recently I was working on a security project using PIC16F877A. The program works fine on Proteus, but when I build my circuit on a breadboard, it gives random sentences. It should wait for the input through the keypad, but instead it enters random inputs by itself and displays wrong password.
Here is the code:
char keypadPort at PORTb;
// Lcd pinout settings
sbit LCD_RS at Rd4_bit;
sbit LCD_EN at Rd5_bit;
sbit LCD_D7 at Rd3_bit;
sbit LCD_D6 at Rd2_bit;
sbit LCD_D5 at Rd1_bit;
sbit LCD_D4 at Rd0_bit;
// Pin direction
sbit LCD_RS_Direction at TRISd4_bit;
sbit LCD_EN_Direction at TRISd5_bit;
sbit LCD_D7_Direction at TRISd3_bit;
sbit LCD_D6_Direction at TRISd2_bit;
sbit LCD_D5_Direction at TRISd1_bit;
sbit LCD_D4_Direction at TRISd0_bit;
int kp;
int password[5];
int DefaultPassword[5]={1,2,3,4,5};
int i;
void main() {
trisc.f0=0;
portc.f0=0;
Keypad_Init();
lcd_init();
lcd_cmd(_lcd_cursor_off);
lcd_out(1,1,"Welcome");
delay_ms(2000);
lcd_cmd(_lcd_clear);
Begin:
lcd_out(1,1,"Enter Password");
for(i = 0 ; i<= 4 ; i++)
{
kp = 0;
do
kp = Keypad_Key_Click();
while (!kp);
switch(kp)
{
case 1: kp = 1; break;
case 2: kp = 2; break;
case 3: kp = 3; break;
case 4: kp = 'A'; break;
case 5: kp = 4; break;
case 6: kp = 5; break;
case 7: kp = 6; break;
case 8: kp = 'B'; break;
case 9: kp = 7; break;
case 10: kp = 8; break;
case 11: kp = 9; break;
case 12: kp = 'C'; break;
case 13: kp = '*'; break;
case 14: kp = 0; break;
case 15: kp = '#'; break;
case 16: kp = 'D'; break;
}
password[i]=kp;
lcd_out(2,i+1,"*");
}
if( strcmp(password,DefaultPassword) ==0)
{
lcd_cmd(_lcd_clear);
lcd_out(1,1,"Welcome");
portc.f0=1;
}
else
{
lcd_cmd(_lcd_clear);
lcd_out(1,1,"Wrong Password");
delay_ms(2000);
goto Begin;
}
}
and this is the output:
Added: This is the schematic:
goto
instead ofwhile
loop? I guess the main problem is in theKeypad_Key_Click
function, which you, of course, have chosen to hide from us... \$\endgroup\$