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?


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).

  • \$\begingroup\$ 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. \$\endgroup\$ – Shombol-shagol Mar 10 '14 at 12:46
  • \$\begingroup\$ 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. \$\endgroup\$ – Blup1980 Mar 10 '14 at 13:06
  • \$\begingroup\$ I update the circuit picture. \$\endgroup\$ – Shombol-shagol Mar 10 '14 at 13:12
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – 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;
  • \$\begingroup\$ I wonder how the compiler xc8 is so relax about program syntax errors. \$\endgroup\$ – Shombol-shagol Mar 10 '14 at 12:01
  • \$\begingroup\$ 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? \$\endgroup\$ – Shombol-shagol Mar 10 '14 at 12:06
  • \$\begingroup\$ @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. \$\endgroup\$ – PeterJ Mar 10 '14 at 12:07
  • \$\begingroup\$ Also take note of Blup1980's answer - I didn't spot that but you will want a pull-down resistor as well. \$\endgroup\$ – PeterJ Mar 10 '14 at 12:08
  • 4
    \$\begingroup\$ 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 ==. \$\endgroup\$ – Spehro Pefhany Mar 10 '14 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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