Probably a stupid question, reading pin input levels with Hi-Tech C?

Discussion in 'Embedded Systems and Microcontrollers' started by GuitarMan216, Apr 4, 2012.

  1. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    I'm working on a program, and in a nutshell, I just need to know how to tell if the PORTD pins are high or low, based on push buttons.

    Wasn't sure if I could do a if(RD0<1) blah blah blah.

    Sorry, tried searching, couldn't find an answer. I'll keep searching.

    Thanks for your help!
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    You could try something like this:

    Code ( (Unknown Language)):
    1.  
    2. while (1)
    3. {
    4.   if (RD0)
    5.   {
    6.      do some code
    7.    }
    8.   if (RD1)
    9.   {
    10.      do some other code
    11.    }
    12. else
    13.    {
    14.      do something complete different
    15.    }
    16. } // end while
    17.  
    18.  
    19.  
    I'm sure there are other way to do it. :)
     
  3. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    What exactly is the while(1) doing? And is the if(RD0) checking if RD0 is high?

    Thanks again.
     
  4. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    The
    Code ( (Unknown Language)):
    1.  
    2. while (1)
    3. {
    4.   do something forever
    5. }
    6.  
    Is an endless loop, since 1 is not changing.

    Code ( (Unknown Language)):
    1.  
    2. if (RD0)
    3. {
    4.    do some code
    5. }
    6.  
    Checks if RD0 is 1 or high. You could write:
    Code ( (Unknown Language)):
    1.  
    2. if (RD0==1)
    3. {
    4.   do some code
    5. }
    6.  
     
  5. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Ok, so my original idea of if(RD0<1) would suffice? If that'll work, I think I'm good to go. Just wasn't sure how Hi Tech C dealt with the inputs. I'm more interested in when it's low, than high.

    Thanks again, I appreciate it.
     
  6. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Just in case someone searches this for similar advice, I went with this code, and it works like a charm.

    Thanks guys.

    Code ( (Unknown Language)):
    1.  
    2. if(RD0==0)
    3. {
    4.     String=E_S;
    5.     Tolerance=.224;
    6.     TRISA=0b00100010;
    7. }
    8.  
    9. if(RD1==0)
    10. {
    11.     String=A_S;
    12.     Tolerance=.300;
    13.     TRISA=0b00000001;
    14. }
    15.  
    16. if(RD2==0)
    17. {
    18.     String=D_S;
    19.     Tolerance=.4008;
    20.     TRISA=0b00011000;
    21. }
    22.  
    23. if(RD3==0)
    24. {
    25.     String=G_S;
    26.     Tolerance=.535;
    27.     TRISA=0b00100100;
    28. }
    29.  
    30. if(RD4==0)
    31. {
    32.     String=B_S;
    33.     Tolerance=.674;
    34.     TRISA=0b00110000;
    35. }
    36.  
    37. if(RD5==0)
    38. {
    39.     String=EH_S;
    40.     Tolerance=.899;
    41.     TRISA=0b00100010;
    42. }
    43.  
     
Loading...