When setting pin as input it getts pulled high, why


I've hooked up a push button to GP1 and a LED to GP2 on my PIC12F683 and this is my code:

unsigned char i = 0;

void main() {
     TRISIO = 0b00000010;
     CMCON0 = 0x07;

     ANSEL = 0b00000000;

              i = GPIO.B1;
              if(i == 0){ GPIO.B2 = 0; }
              if(i == 1){ GPIO.B2 = 1; }

But when I plug in the power my LED is turned on, even when I remove the switch from the breadboard.

I'm using MikroC. When I set GP1 to output the LED was turned off.

What is the problem likely to be?

My config:

enter image description here

enter image description here

Best Answer

Disable the internal pull-up resistor of your input pin. In AVR it's usually done by configuring the pin as input and setting it to 0. (I don't know much about the PIC but it's done this way in Atmel's AVR). also if your push-button is connected to VCC, you should put a pull-down resistor on the input pin, so when the switch is off, the input will be connected to GND and the value will be zore.

if you are confused by the thing's i've said, just put a 1Kohm resistor from your input pin to ground. It should solve your problem! (it just gonna use more power, but it's not much!) :-)

also here is a good refference on pin configurations for PIC (with lot's of pictures!): http://www.mikroe.com/chapters/view/4/chapter-3-i-o-ports/