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?










share|improve this question




























    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?










    share|improve this question


























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 3:38

























      asked Nov 21 at 23:48









      hvguy

      184




      184
























          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?






          share|improve this answer



















          • 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


















          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));





          share|improve this answer























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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?






          share|improve this answer



















          • 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















          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?






          share|improve this answer



















          • 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













          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?






          share|improve this answer














          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?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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














          • 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












          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));





          share|improve this answer























          • 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















          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));





          share|improve this answer























          • 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













          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));





          share|improve this answer














          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));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Trompette piccolo

          Slow SSRS Report in dynamic grouping and multiple parameters

          Simon Yates (cyclisme)