2

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;
        }
    }
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Mushfiqul Tuhin
  • 666
  • 6
  • 13
  • 31

3 Answers3

0

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;
        }
    }
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • thanks for reply . It works , but sorry to say that , I have added a push button to PORTA.RA1 for turning off the voltage of PORTA to see if it works after turning off the voltage . But it acts like first state , it acts perfectly , but dont work after turning off the voltage ( the code as you corrected above , works when I run . But when I disconnect the voltage (by pressing push button )it should turn off " PORTB.RB1 " voltage and turn on the voltage of "PORTC.RC1" . But it dosent . Do you have any solution about that ? – Mushfiqul Tuhin Mar 26 '15 at 15:31
  • Perhaps the port has a pull-up resistor to stop it floating. – Weather Vane Mar 26 '15 at 19:04
  • Could you please elaborate it ? When I give voltage to PORTA will it return 1? so that I can check programmatically and do whatever I want after checking PORTA voltage . Input voltage will be turned off or turned on any time by user , using a push button. – Mushfiqul Tuhin Mar 26 '15 at 19:22
  • I am saying removing an input from the port (leaving it open circuit) won't necessarily make the input go low. If there is a pull-up resistor it will stay high. This is often done to prevent unconnected inputs from floating. – Weather Vane Mar 26 '15 at 19:39
  • Thank you . actually I was very busy with researching about microcontroller (coz I am new here) . I was googling and working with your corrected code .Though I was delaying But I did not forget to accept your answer . BTW thanks again . – Mushfiqul Tuhin Mar 26 '15 at 20:04
  • If you want to test the input to a port pin, you must specifically drive it low or high with 0v or +5v, not leave it open circuit. – Weather Vane Mar 26 '15 at 20:06
0

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

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.