Jump to content
43oh

Basic Control Register Control for LED Blink Issue


Recommended Posts

Hello everyone,

 

I am new to MSP430G2553 and a beginner at programming. But I am trying to do something simple to start off which is to do the blink example, but without using digitalWrite and instead using P1DIR/P1OUT/etc.

 

Goal: Take the MSP430G2 Launchpad Development Board and blink P1.6 only.

 

Issue: I noticed that the following code doesn't work and that I need to initialize P1.7 along with P1.6 for it to work:

 

void setup() {                
// initialize pin 1.6 as output
P1DIR = 01000000;
}
 
// the loop routine runs over and over again  to blink the LED:
void loop() {
  P1OUT = 01000000;
  delay(1000);               // wait for a second
  P1OUT = 00000000;
  delay(1000);               // wait for a second
}
 
 
 
-----------------------The code below works fine (initializing P1.7 as well)------------------------------------
 
void setup() {                
P1DIR = 11000000;
}
 
// the loop routine runs over and over again  to blink the LED:
void loop() {
  P1OUT = 11000000;
  delay(1000);               // wait for a second
  P1OUT = 00000000;
  delay(1000);               // wait for a second
}

 

Link to post
Share on other sites

The first thing is that the numeric values, such as in the line `P1OUT = 01000000;' are treated as octal values due to the leading zero. Without a leading zero or base specified, numerics are treated as decimal. As you intend this to be binary, you can try `0b01000000'. Some compilers will accept this as binary, though it is not standard. The best way is use hexidecimal: `0x40', which all C compilers will accept.

Link to post
Share on other sites

For ease-of-use - I like using the BIT0, BIT1, etc. defines.  Also - the reason why you're running into problems with initialization is you're always using the plain assignment operator.  You need to look at some of the other operators like  |=, &= and ^= - they will make it easier to control single bits without affecting the others.  For instance - P1DIR |= BIT6 will only add bit6 to whatever is defined in P1DIR.

 

Typical on/off code:

void loop() {
  P1OUT |= BIT6;
  delay(1000);               // wait for a second
  P1OUT &= ~BIT6;
  delay(1000);               // wait for a second
}

or another option - toggle

void loop() {
  P1OUT ^= BIT6;
  delay(1000);               // wait for a second
}
Link to post
Share on other sites
  • 2 weeks later...

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...