# Need help with switch in pic16f887

I have recently started learning to use pic micro controller. I am using pic16f887 mc and for programing using mplabx with xc8 compiler. My micro controller is getting reading from switch and turning on led when I use any digital i/o pin except rb0-rb5. I don't know why those pins are not working. I made the circuit in proteus also.It is giving me the same result. Can anybody tell me why this is happening? Here are codes and schematic where I used rc7 for input and that works perfectly.

void main(void) {
OSCCON=0x76;
TRISC7 = 1; //RC7 as INPUT PIN
TRISD1 = 0; //RD0 as OUTPUT PIN

RD1 = 0; //LED Off

while(1)
{
if(RC7 == 0) //If Switch Pressed
{
__delay_ms(100);
if(RC7==0)
{
RD1 = 1; //LED ON
__delay_ms(3000); //3 Second Delay
RD1 = 0; //LED OFF

}

}
}
return;
}


• I see you have defined RD0 as input but don't use it. I don't see where you have defined RC7. – Transistor Mar 17 '16 at 14:42
• From the comments on lines 3-4 I am guessing the TRIS registers were set wrong. Please show the non-working code that uses RB0-RB5. – Bruce Abbott Mar 17 '16 at 14:52
• I'm guessing that you haven't disabled the analog mode on those pins. The PIC powers up with them set as analog inputs and you have to set them as digital using the ANSEL registers. Have you tried any of the RA pins - you should have the same trouble there if you don't configure the ANSEL correctly. – brhans Mar 17 '16 at 14:57
• On the PICs I've worked with (18F4685), an input requires the TRIS bit to be set to 1, while an output is '0'. – A.Mac Mar 17 '16 at 16:20
• @transistor Sorry i commented wrong. RD0 is output pin. I have used that. RC7 is code for the trisc7 register .We don't need to define it. – MD.Tanvir Mushfique Mar 18 '16 at 2:07

Pins with analog inputs are set to analog input mode by default. To read a digital input you must clear the corresponding ANSEL or ANSELH bit. RB0 is AN12, so you need to clear ANS12 (bit 4 in ANSELH).