0
\$\begingroup\$

I have a PIC starter kit. I am doing a simple program of turning on led when switch is pressed and then turning them off when switch is released like following:

if(switch==1)
{
  led = 1;
}
else
{
  led = 0;
}

I was actually not able to do above code in MPLAB. Then I found a tutorial online. I used that code and it was working.

Switch is connected to RD7
Led is connected to RD0

CODE

if(PORTAbits.RD7 == 0) 
{
  PORTDbits.RD0 = 1;
}
else if(PORTAbits.RD7 == 1)
{
  PORTDbits.RD0 = 0;
}

Now I am confused. Why led is turning on when switch(RD7) is low. Can anyone explain me this logic.?

\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

You should refer to the User Guide. It says that

Push button switches provide the following functionality:

• SW1: Active-low switch connected to RD6

• SW2: Active-low switch connected to RD7

• SW3: Active-low switch connected to RD13

also

The switches do not have any debounce circuitry and require the use of internal pull-up resistors; this allows you to investigate software debounce techniques.

So I think this answers your question of why led is turning on when switch(RD7) is low

\$\endgroup\$
1
\$\begingroup\$

If you read the documentation for your board, you will see the following, which I think is self explanatory:

doc

\$\endgroup\$

Your Answer

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

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