jdenozi
jdenozi

Reputation: 212

Cannot read input from pins PIC

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

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

Upvotes: 0

Views: 813

Answers (2)

the busybee
the busybee

Reputation: 12600

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

Upvotes: 1

Daniel
Daniel

Reputation: 49

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

Upvotes: 1

Related Questions