1
\$\begingroup\$

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;

     while(1){
              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

Scematics: enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ I don't know much about PICs but is there a input pull-up built into the chip you're using? It could be pulling the line high. \$\endgroup\$
    – tangrs
    Oct 11, 2014 at 12:16
  • 2
    \$\begingroup\$ I think most PICs have internal pull-ups disabled by default but if the pin is floating and you're not using an external pull-down resistor you'll get unexpected results. Perhaps you could post a circuit or describe exactly how the push button is connected? Best way is probably to enable the internal pull-up and connect the other side of the button to ground. \$\endgroup\$
    – PeterJ
    Oct 11, 2014 at 12:28

1 Answer 1

0
\$\begingroup\$

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/

\$\endgroup\$
3
  • 2
    \$\begingroup\$ "configuring the pin as output and setting it to 0" - I'm not sure, but isn't it rather "as input"? \$\endgroup\$ Oct 11, 2014 at 13:06
  • \$\begingroup\$ @MisterMystère, Yes, you are right. I'll correct it right now! Thanks. For more information: in AVR we set the port direction with DDR like DDRA = 0x00 that tells MCU that A is input and DDRA = 0xFF tells that A is output. When the port is output we set the output by PORT, like PORTA = 0xFF which make all the output go high, but if it's input, the PORT command will enable/disable it's pullups! PORTA = 0x00 will disable all the pullups, and PORTA = 0xFF will enable all the pullups! it's still an input port!! I'm sure PIC should have some different way of doing it! \$\endgroup\$
    – Shamim
    Oct 11, 2014 at 13:23
  • 2
    \$\begingroup\$ That's completely wrong for PICs such as the 12F683. Control bits WPUx enable (bit set) or disable (bit cleared) each pull-up. They are turned off by default. \$\endgroup\$ Oct 11, 2014 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.