Jump to content
43oh

digitalWrite (P1_0, 1); vs bitSet(P1OUT,P1_0);


Recommended Posts

Hi

 

Doing my first step with my LP I can't get the instruction :

bitSet(P1OUT,P1_0);

working.

 

It should be equivalent to "digitalWrite (P1_0, 1);" but the previous instruction don't light up the led !

 

Do I do something wrong ?

 

The same happen with "P1OUT |= (1<<P1_0)". I can't get the led on !

Link to post
Share on other sites

In Energia, P1_0 is a placeholder for the pin number on the headers (e.g. P1_0 = 2 on the MSP-EXP430G2 LaunchPad). DigitalWrite translates that number to the actual port and bit.

 

If you want to directly work with P1OUT, use the bit number of the desired pin. E.g. bitSet(P1_0,0) or P1OUT |= BV(0)).

Link to post
Share on other sites

I think the main difference is in the fact that digitalWrite will set the pin to output mode, while bitSet does no such thing.

If you'd like to use bitSet, be sure that during setup or directly before setting the big high, you make the pin an output by either calling digitalWrite once, or call bitSet(P1DIR,P1_0);

 

Using P1OUT |= (1<<P1_0); is a very bad idea, since P1_0 is not the bit within the byte but the pin within the device, In such a case, batter use P1OUT |= 1 << 0; (where 0 is the bit number)

Link to post
Share on other sites

I think the main difference is in the fact that digitalWrite will set the pin to output mode, while bitSet does no such thing.

If you'd like to use bitSet, be sure that during setup or directly before setting the big high, you make the pin an output by either calling digitalWrite once, or call bitSet(P1DIR,P1_0);

 

Umm, while digitalWrite might do that under the hoods, pinMode is what should be used to set a pin as output.

Link to post
Share on other sites

Umm, while digitalWrite might do that under the hoods, pinMode is what should be used to set a pin as output.

Depends on your frame of usage, either use the pinMode/digitalWrite etc calls from the Energia API, or use the GPIO registers P1OUT/P1REN/P1DIR etc from the msp430.

It is awkward to use pinMode and then use P1OUT, but it's fine to use pinMode together with digitalWrite, as is using P1DIR together with P1OUT.

Link to post
Share on other sites

If you are working with a BoosterPack and want the code compatible across LaunchPads you should stick to just using the pin number in your function calls. That way if someone wants to port it to another LaunchPad all the pin numbers are the same even though the underlying pins are different. 

 

This will work across all launchpads:

pinMode(1, OUTPUT)

digitalWrite(1, HIGH)

 

This will always use P1_0, which may be in a different place on each launchpad:

pinMode(P1_0, OUTPUT)

digitalWrite(P1_0, HIGH)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...