Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've build this circuit :

  • The diode is actually a LED.

enter image description here

With this code on MPLAB x,XC8 :

#include <xc.h>
#define _XTAL_FREQ 20000000

void main(){
TRISIO = 0x001000;
GPIO = 0b000000;
    while(GPIO = 0b001000){
        GPIO = 0b110111;


But the GP0(pin7) doesn't react on GP3(pin7) pressed button, why?

share|improve this question
up vote 2 down vote accepted

You must add a pull down resistor to your button. enter image description here The GPIO pins are sensitive to voltages. When you press the button, the voltage (vs GND) at the GPIO is set to 5V, and the MCU will see this. But when you release the button, what is the voltage at the GPIO? 0V? probabliy not, what forces that voltage to be 0V?

When left open, or floating, the value of the GPIO is undefined. It could be 1, or 0. Or could switch quickly (oscillate). On AT Tiny, when left open under some condition the input stage starts to consume a lot of current... (which is bad for a low power MCU...)

Thus, you could solve this by adding a resistor (called a "pull down") between your GPIO and GND (10k is fine).

Or you may connect your button to GND instead of 5V and enable the internal pull up resistor of the MCU GPIO (I don't know if this particular model offers that feature).

share|improve this answer
I put one 16k resistor between key, but when my hand get near to key PIC act like i press the button!it does not really react on my button.it lite the led for a while and then it turn it off. – Shombol-shagol Mar 10 '14 at 12:46
Where did you put the resistor exactly? It's not across the button but across the GPIO pin and GND. Your observed behavior is exactly what happens when an input is left floating. – Blup1980 Mar 10 '14 at 13:06
I update the circuit picture. – Shombol-shagol Mar 10 '14 at 13:12
Ok, This is not where you have to put the resistor. You should connect your resistor between the P4 pin of your micro and the minus (-) port of your 5V source. – Blup1980 Mar 10 '14 at 13:15

In the following code a single equals sign is an assignment operator so it will be trying to set the GPIO value. It should be like this for a comparison:

while(GPIO == 0b001000){
    GPIO = 0b110111;

However you're doing an exact comparison so all other bits must be zero for it to match, what you probably really want is something like this that includes a bitwise-AND to just check a single bit:

while(GPIO & 0b001000){
    GPIO = 0b110111;
share|improve this answer
I wonder how the compiler xc8 is so relax about program syntax errors. – Shombol-shagol Mar 10 '14 at 12:01
Thank's sir, i understand what you mean but how i can tell it to compare only fourth bit of GPIO with Zero?could i use 0bxx1xxx? – Shombol-shagol Mar 10 '14 at 12:06
@user38322, that's not really a syntax error, sometimes assigning in an if statement can be helpful, some compilers would give you an error. The only the fourth bit is because of that value near the & - you might want to spend a bit of time learning about bitwise operators they can be a bit confusing. – PeterJ Mar 10 '14 at 12:07
Also take note of Blup1980's answer - I didn't spot that but you will want a pull-down resistor as well. – PeterJ Mar 10 '14 at 12:08
If you make a habit of putting the constant value on the left it will be an invalid expression (generating an error) if you use = instead of ==. – Spehro Pefhany Mar 10 '14 at 12:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.