Reading input pin of pic microcontroller with MIKROC

Mushfiqul Tuhin picture Mushfiqul Tuhin · Mar 25, 2015 · Viewed 12.1k times · Source

I need to write a program that will check if an input pin of PIC has a voltage. If a voltage exists then it will give voltage to a selected output pin like PORTB.RB1=1;. Else it will give voltage to other selected output pin like PORTC.RC1=1;.

Is it possible? I have tried to do this, but it does not work .

void main() {

    TRISB=0;
    TRISA=1;
    TRISC=0;

    while(1){
        delay_ms(500);
        // PORTB=0;
        if(PORTA==1){
            PORTB.RB1 =1;
        }
        else{
            PORTC.RC1 =1;
        }
    }
}

Answer

Weather Vane picture Weather Vane · Mar 25, 2015

You have not turned off the other port output, and you have not isolated the input pin of PORTA. If it's bit 0 the mask is 1, if it's bit 1 the mask is 2, etc.

void main() {

    TRISB=0;
    TRISA=1;
    TRISC=0;

    while(1){
        delay_ms(500);
        if(PORTA & 1){
            PORTB.RB1 =1;
            PORTC.RC1 =0;
        }
        else{
            PORTB.RB1 =0;
            PORTC.RC1 =1;
        }
    }
}