# PIC16F877A microcontroller security project

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 i;
void main() {
trisc.f0=0;
portc.f0=0;
lcd_init();
lcd_cmd(_lcd_cursor_off);
lcd_out(1,1,"Welcome");
delay_ms(2000);
lcd_cmd(_lcd_clear);
Begin:

for(i = 0 ; i<= 4 ; i++)
{
kp = 0;
do
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;

}
lcd_out(2,i+1,"*");
}
{
lcd_cmd(_lcd_clear);
lcd_out(1,1,"Welcome");
portc.f0=1;
}
else
{
lcd_cmd(_lcd_clear);
delay_ms(2000);
goto Begin;
}

}


and this is the output:

• Any weason to use goto instead of while loop? I guess the main problem is in the Keypad_Key_Click function, which you, of course, have chosen to hide from us... – Eugene Sh. Aug 23 '16 at 13:57
• Post the function Keypad_Key_Click() – Armandas Aug 23 '16 at 13:58
• keypad_key_click() is a function that stores the value read by the keypad into the variable called kp – Mahmoud Mansour Aug 23 '16 at 14:01
• We understand what it is supposed to do. But apparently it doesn't do it. So post the relevant code. – Eugene Sh. Aug 23 '16 at 14:04
• Well, according to documentation it looks like it should wait for the input indeed. If it doesn't, I would suspect some hardware problem. You better post your schematics here. – Eugene Sh. Aug 23 '16 at 14:10

From the photos so far, I see 3 hardware problems:

1. No Vdd & Vss connections to pins 32 & 31. This is required for reliable operation. You cannot assume that the PIC will behave correctly at all times, if you only connect to the power pins 11 & 12. You must connect to pins 32 & 31 as well.

2. No decoupling capacitors close to the PIC for either Vdd & Vss pins 11 & 12 nor 32 & 31. These are required for reliable operation.

3. No sign of pull-up resistors near the keypad connector on Port B, near pins 40-33. Without these, you are likely to get sporadic false keypress detection, which fits with your symptoms.

Therefore:

• Please supply another photo which includes all of the breadboard, preferably taken from directly above the breadboard itself.