View previous topic :: View next topic |
Author |
Message |
The Rookie Guest
|
Checking to see if a pin is High or Low |
Posted: Wed May 12, 2004 3:42 pm |
|
|
I would like to develop a routine that checks to see if a pin is High or Low.
If the Pin is high it will do one thing of it is low it will do another.
Thanks
The Rookie |
|
|
Joyce Guest
|
|
Posted: Wed May 12, 2004 4:12 pm |
|
|
if(input(PIN_Ax)) // pin high
{
do high
}
else
{
do low
} |
|
|
Suus
Joined: 11 May 2004 Posts: 8
|
|
Posted: Thu May 13, 2004 1:06 am |
|
|
Hello,
Can I make it shorter like:
if(PIN_Ax)
{
}
else
{
}
????
Susan |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Thu May 13, 2004 2:24 am |
|
|
Not in that format, but you can define a 'bit' like this:
#bit RA1=0x05.1 //On PIC16F877, 0x05 is the address of PORTA.
Now you can say:
RA1=1;
or
if(RA1)
{
...
}
else
{
...
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu May 13, 2004 2:53 am |
|
|
Please note that the for the shorter version to work you will also have to define the I/O pin as input first! While in the 'longer' version the compiler will do this for you.
So, what is 'shorter'.... ???
Both examples lead to similar assembly code, so I guess it's a matter of taste. |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Thu May 13, 2004 3:00 am |
|
|
Actually the pin doesn't need to be configured as input. You can use this method to read/verify the state of an output pin as well. |
|
|
Suus
Joined: 11 May 2004 Posts: 8
|
|
Posted: Thu May 13, 2004 4:20 am |
|
|
And how to check if a pin is low?
(input(!PIN_B0))
or
(!input(PIN_B0))
Susan |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Thu May 13, 2004 4:27 am |
|
|
input() is a C function, so:
if(!input(PIN_B0))
{
...
} |
|
|
|