C macro to turn a pin on or off
up vote
3
down vote
favorite
I know it seems like this has been asked before but I spent a few hours looking for and testing things and couldn't find a solution.
I want to replace this:
#define LED_ON PORTA|=(1<<6)
#define LED_OFF PORTA&=~(1<<6)
with something that works like this:
#define ON 1
#define OFF 0
#define LED(x) if (x==ON) PORTA|=(1<<6) else if (x==OFF) PORTA&=~(1<<6)
So what is the right way (or best way?) to do this?
c
add a comment |
up vote
3
down vote
favorite
I know it seems like this has been asked before but I spent a few hours looking for and testing things and couldn't find a solution.
I want to replace this:
#define LED_ON PORTA|=(1<<6)
#define LED_OFF PORTA&=~(1<<6)
with something that works like this:
#define ON 1
#define OFF 0
#define LED(x) if (x==ON) PORTA|=(1<<6) else if (x==OFF) PORTA&=~(1<<6)
So what is the right way (or best way?) to do this?
c
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I know it seems like this has been asked before but I spent a few hours looking for and testing things and couldn't find a solution.
I want to replace this:
#define LED_ON PORTA|=(1<<6)
#define LED_OFF PORTA&=~(1<<6)
with something that works like this:
#define ON 1
#define OFF 0
#define LED(x) if (x==ON) PORTA|=(1<<6) else if (x==OFF) PORTA&=~(1<<6)
So what is the right way (or best way?) to do this?
c
I know it seems like this has been asked before but I spent a few hours looking for and testing things and couldn't find a solution.
I want to replace this:
#define LED_ON PORTA|=(1<<6)
#define LED_OFF PORTA&=~(1<<6)
with something that works like this:
#define ON 1
#define OFF 0
#define LED(x) if (x==ON) PORTA|=(1<<6) else if (x==OFF) PORTA&=~(1<<6)
So what is the right way (or best way?) to do this?
c
c
edited Nov 22 at 3:38
asked Nov 21 at 23:48
hvguy
184
184
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
Like this?
#define LED(x) (x==ON) ? (PORTA|=(1<<6)) : (PORTA&=~(1<<6))
EDIT: Slight error, I apologise. Is this what you're going for?
1
Yes! I tried several things but was definitely over complicating it. Minor edit, port assignments had to be in parenthesis i.e. (PORTA|=(1<<6))
– hvguy
Nov 22 at 3:36
add a comment |
up vote
0
down vote
You can use the ternary operator alternatively.
#define ON 1
#define OFF 0
#define LED(X) (X)?(PORTA|=(1<<6)):(PORTA &= ~(1<<6));
You can to make it more generic macro by replacing the pin number as:
#define ON 1
#define OFF 0
#define LED(STATE, PIN) (STATE)?(PORTA|=(1<<PIN)):(PORTA &= ~(1<<PIN));
Also, You can make it generic for all ports by replacing the PortName as:
#define ON 1
#define OFF 0
#define LED(STATE, PORT, PIN) (STATE)?(PORT|=(1<<PIN)):(PORT&=~(1<<PIN));
If it wasn't for the fact that these are distributed over different registers this would work well.
– hvguy
Nov 22 at 3:37
@hvguy, I have edited the answer and added the generic part for all ports
– Ahmed Yasen
Nov 22 at 10:12
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Like this?
#define LED(x) (x==ON) ? (PORTA|=(1<<6)) : (PORTA&=~(1<<6))
EDIT: Slight error, I apologise. Is this what you're going for?
1
Yes! I tried several things but was definitely over complicating it. Minor edit, port assignments had to be in parenthesis i.e. (PORTA|=(1<<6))
– hvguy
Nov 22 at 3:36
add a comment |
up vote
5
down vote
accepted
Like this?
#define LED(x) (x==ON) ? (PORTA|=(1<<6)) : (PORTA&=~(1<<6))
EDIT: Slight error, I apologise. Is this what you're going for?
1
Yes! I tried several things but was definitely over complicating it. Minor edit, port assignments had to be in parenthesis i.e. (PORTA|=(1<<6))
– hvguy
Nov 22 at 3:36
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Like this?
#define LED(x) (x==ON) ? (PORTA|=(1<<6)) : (PORTA&=~(1<<6))
EDIT: Slight error, I apologise. Is this what you're going for?
Like this?
#define LED(x) (x==ON) ? (PORTA|=(1<<6)) : (PORTA&=~(1<<6))
EDIT: Slight error, I apologise. Is this what you're going for?
edited Nov 22 at 3:38
answered Nov 21 at 23:55
Nunchy
838410
838410
1
Yes! I tried several things but was definitely over complicating it. Minor edit, port assignments had to be in parenthesis i.e. (PORTA|=(1<<6))
– hvguy
Nov 22 at 3:36
add a comment |
1
Yes! I tried several things but was definitely over complicating it. Minor edit, port assignments had to be in parenthesis i.e. (PORTA|=(1<<6))
– hvguy
Nov 22 at 3:36
1
1
Yes! I tried several things but was definitely over complicating it. Minor edit, port assignments had to be in parenthesis i.e. (PORTA|=(1<<6))
– hvguy
Nov 22 at 3:36
Yes! I tried several things but was definitely over complicating it. Minor edit, port assignments had to be in parenthesis i.e. (PORTA|=(1<<6))
– hvguy
Nov 22 at 3:36
add a comment |
up vote
0
down vote
You can use the ternary operator alternatively.
#define ON 1
#define OFF 0
#define LED(X) (X)?(PORTA|=(1<<6)):(PORTA &= ~(1<<6));
You can to make it more generic macro by replacing the pin number as:
#define ON 1
#define OFF 0
#define LED(STATE, PIN) (STATE)?(PORTA|=(1<<PIN)):(PORTA &= ~(1<<PIN));
Also, You can make it generic for all ports by replacing the PortName as:
#define ON 1
#define OFF 0
#define LED(STATE, PORT, PIN) (STATE)?(PORT|=(1<<PIN)):(PORT&=~(1<<PIN));
If it wasn't for the fact that these are distributed over different registers this would work well.
– hvguy
Nov 22 at 3:37
@hvguy, I have edited the answer and added the generic part for all ports
– Ahmed Yasen
Nov 22 at 10:12
add a comment |
up vote
0
down vote
You can use the ternary operator alternatively.
#define ON 1
#define OFF 0
#define LED(X) (X)?(PORTA|=(1<<6)):(PORTA &= ~(1<<6));
You can to make it more generic macro by replacing the pin number as:
#define ON 1
#define OFF 0
#define LED(STATE, PIN) (STATE)?(PORTA|=(1<<PIN)):(PORTA &= ~(1<<PIN));
Also, You can make it generic for all ports by replacing the PortName as:
#define ON 1
#define OFF 0
#define LED(STATE, PORT, PIN) (STATE)?(PORT|=(1<<PIN)):(PORT&=~(1<<PIN));
If it wasn't for the fact that these are distributed over different registers this would work well.
– hvguy
Nov 22 at 3:37
@hvguy, I have edited the answer and added the generic part for all ports
– Ahmed Yasen
Nov 22 at 10:12
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use the ternary operator alternatively.
#define ON 1
#define OFF 0
#define LED(X) (X)?(PORTA|=(1<<6)):(PORTA &= ~(1<<6));
You can to make it more generic macro by replacing the pin number as:
#define ON 1
#define OFF 0
#define LED(STATE, PIN) (STATE)?(PORTA|=(1<<PIN)):(PORTA &= ~(1<<PIN));
Also, You can make it generic for all ports by replacing the PortName as:
#define ON 1
#define OFF 0
#define LED(STATE, PORT, PIN) (STATE)?(PORT|=(1<<PIN)):(PORT&=~(1<<PIN));
You can use the ternary operator alternatively.
#define ON 1
#define OFF 0
#define LED(X) (X)?(PORTA|=(1<<6)):(PORTA &= ~(1<<6));
You can to make it more generic macro by replacing the pin number as:
#define ON 1
#define OFF 0
#define LED(STATE, PIN) (STATE)?(PORTA|=(1<<PIN)):(PORTA &= ~(1<<PIN));
Also, You can make it generic for all ports by replacing the PortName as:
#define ON 1
#define OFF 0
#define LED(STATE, PORT, PIN) (STATE)?(PORT|=(1<<PIN)):(PORT&=~(1<<PIN));
edited Nov 22 at 10:11
answered Nov 22 at 0:19
Ahmed Yasen
37211
37211
If it wasn't for the fact that these are distributed over different registers this would work well.
– hvguy
Nov 22 at 3:37
@hvguy, I have edited the answer and added the generic part for all ports
– Ahmed Yasen
Nov 22 at 10:12
add a comment |
If it wasn't for the fact that these are distributed over different registers this would work well.
– hvguy
Nov 22 at 3:37
@hvguy, I have edited the answer and added the generic part for all ports
– Ahmed Yasen
Nov 22 at 10:12
If it wasn't for the fact that these are distributed over different registers this would work well.
– hvguy
Nov 22 at 3:37
If it wasn't for the fact that these are distributed over different registers this would work well.
– hvguy
Nov 22 at 3:37
@hvguy, I have edited the answer and added the generic part for all ports
– Ahmed Yasen
Nov 22 at 10:12
@hvguy, I have edited the answer and added the generic part for all ports
– Ahmed Yasen
Nov 22 at 10:12
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422032%2fc-macro-to-turn-a-pin-on-or-off%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown