Reading input pin of pic microcontroller with MIKROC

12,615

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;
        }
    }
}
Share:
12,615
Mushfiqul Tuhin
Author by

Mushfiqul Tuhin

Updated on June 04, 2022

Comments

  • Mushfiqul Tuhin
    Mushfiqul Tuhin almost 2 years

    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;
            }
        }
    }
    
  • Mushfiqul Tuhin
    Mushfiqul Tuhin about 9 years
    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 ?
  • Weather Vane
    Weather Vane about 9 years
    Perhaps the port has a pull-up resistor to stop it floating.
  • Mushfiqul Tuhin
    Mushfiqul Tuhin about 9 years
    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.
  • Weather Vane
    Weather Vane about 9 years
    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.
  • Mushfiqul Tuhin
    Mushfiqul Tuhin about 9 years
    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 .
  • Weather Vane
    Weather Vane about 9 years
    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.