1
votes

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;
        }
    }
}
3

3 Answers

0
votes

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;
        }
    }
}
0
votes

The PORTA and the PORTE are analog ports. If you want to use them as Digital Input, you must Prevent PIC to use them as analog inputs. You must add this instruction: ADCON1=0x06; before you set the PORTA or PORTE as input.

This code works successfully:

void main()
{
    ADCON1=0x06;
    TrisA=1;
    TrisE=1;
    TrisC=0;
    PortC=0;
    while (1)
    {
        if (PortA.B0==1)
            PortC.B0=1;
        else
            PortC.B0=0;

        if (PortA.B1==1)
            PortC.B1=1;
        else
            PortC.B1=0;

        if (PortA.B2==1)
            PortC.B2=1;
        else
            PortC.B2=0;

        if (PortA.B3==1)
            PortC.B3=1;
        else
            PortC.B3=0;

        if (PortA.B5==1)
            PortC.B4=1;
        else
            PortC.B4=0;

        if (PortE.B0==1)
            PortC.B5=1;
        else
            PortC.B5=0;

        if (PortE.B1==1)
            PortC.B6=1;
        else
            PortC.B6=0;

        if (PortE.B2==1)
            PortC.B7=1;
        else
            PortC.B7=0;
    }
}
0
votes

hardware connection : wire input port's bit with 5v supply and switch. after this connect it with pull down resistors.

void main(){
TRISB = 1;     //set portB as input       
TRISC = 0;     //set portC as output
while(1){

 if(PORTB.B0 == 0){           //if RB0 == 0 ?
          PORTC.F0 = 1;       //set RC0 = 1 ,(high)
          }else PORTC.F0 = 0; //set RC0 = 0 ,(low)
 if(PORTB.B1 == 0){           //if RB1 == 0 ?
          PORTC.F1 = 1;       //set RC1 = 1 ,(high)
          }else PORTC.F1 = 0; //set RC1 = 0 ,(low)

  //set if else block for numbers of bit as you want.....
       }
}

note that pic port A is anlalog input default and if you want to use this port as digital port change the ADCON registers and follow datasheet.