0

I'm facing a really struggling issue with input reading.

I just wanted to use a push button to active several led but i don't know why the specific PORT doesn't change to 0 to 1 when i press the button.

I've seen that it could be related to the analog but i turned it to digital

  • PIC16F18875
  • Using Mplabx v5.40

Here is the code

#define _XTAL_FREQ 4000000
#define button TRISDbits.RD7
#include <xc.h>
ledLoop(void){
    char run = 1;
    while(1){
        if(PORTDbits.RD7==1){
           LATB=run;
           run *= 2;
           __delay_ms(200);
           
        }
        else{
            LATB=0;
        }
    }
}
void main(void)
{
    ANSELDbits.ANSD7=0;
    TRISDbits.TRISD7=1;
    TRISA=0;
    LATA=0x00;
    ledLoop();
    
}
 

Also my push button is connected as it follow:

3V -> LED -> 10 Ohm resistance -> push button -> to mass and to RD7 port

EDIT


enter image description here

The 4 leds works if the if condition for RD==0 so its working. And for the button part, if i press on the button the led works, but dont change PORTDbits.RD7 to 1

4
  • 1
    That schematic sounds like it doesn't provide the proper drive to the pin, but it's not clear. Please edit with a proper schematic image. Commented Aug 29, 2020 at 14:53
  • I've just edited the post, thank for your help Commented Aug 29, 2020 at 17:22
  • 2
    I can't quite tell because this is a photo of a breadboard and not a schematic, but it looks like your button GPIO might just be wired to ground. Use a multimeter or oscilloscope to check the voltage you see at the input pin as the button is pressed and released. Commented Aug 29, 2020 at 17:35
  • Change LATB=0; to LATB=~LATB; __delay_ms(500); and see if things flash that way. Commented Aug 29, 2020 at 18:29

2 Answers 2

1

You need to connect the "high" side of the button to RD7, and the "low" side of the button to ground:

schematic

That way the high level of 3,3V gets to the input pin if the button is open. When you press the button, the low level of ground gets to the input pin.

(The correct statement for this would sound different, but I wanted to say it as simple as possible.)

Sign up to request clarification or add additional context in comments.

2 Comments

So, it seems you were rigth. I thougth i had to connect my button to 3V and then the pin RD7 change his value when i press the button. But something strange when my finger is very near the button, the led routine works
Sure, as the input is "open" there is just very little current necessary to make its level swing. And your finger collects enough electromagnetic noise to build up some voltage to influence that pin. -- With the suggested solution, the impedance to 3,3V is low enough to stop this effect.
1

Is your schematic look like this ? LEDs:3v -> LEDs -> 10R -> PORTB and BUTTON:0v -> BUTTON -> RD7 Perhaps add a Pullup between Button/RD7 to avoid electric floating value

1 Comment

I've just edited the post, maybe it's going to be clearer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.