EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: aep9690 on March 20, 2013, 04:41:02 am

Title: MSP430 Noob
Post by: aep9690 on March 20, 2013, 04:41:02 am
I just got a MSP-EXP430G2 Launchpad from TI.  I've been fooling around on it, making the LEDs blink and everything.  I'm working on a simple bit of code that blinks the red LED on PIN0, turns it off and then turns on the green LED on PIN6.  However, I want it so that when I press the button attached to PIN3 the flashing will stop.  I am attempting to accomplish this with the following setup code:

   // Define the GPIO Pins, 0b01001001
   P1DIR = 0x01;
   P1DIR |= 0x40;
        P1DIR |= 0x08;

   // Write PIN0 to HIGH, 0b00000001
   P1OUT = 0x01;

   // Define PIN3 as an input, 0b00001000
   P1IN = 0x08;

However, when I execute this code on the MSP430 I see that the the actual values written to the P1DIR, P1OUT, and P1IN registries are:

   P1IN = 0b00000111

   P1OUT = 0b00000001

   P1DIR = 0b01001001

The P1DIR and P1OUT registries are as I expected and desired.  However the P1IN isn't.  PIN0 is written to a 1 when it should be 0, and PIN3 is 0 when it should be a 1.  I suspect that PIN1 and PIN2 are set to 1 because they are the UART pins.

Am I using incorrect code?  I am pretty new to microcontrollers in general but I think I set it up correctly.  I would appreciate any help I can get.
Title: Re: MSP430 Noob
Post by: ve7xen on March 20, 2013, 05:57:55 am
The output looks correct to me.

Bit 0 is set because you've configured it as an output (PxDIR bit set) and set it (PxOUT bit set); the pin should be high as well
Bits 1 and 2 are set because they're configured as an input (PxDIR bit clear) and are floating or externally pulled up
Bit 3 is clear because you've configured it as output (PxDIR bit set) and cleared it (PxOUT bit clear)

I'm unclear on what exactly the problem you're having is. What are you trying to do, how have you got your circuit wired, and what's not happening? Maybe you've got your notion of PxDIR incorrect? When a bit is set in PxDIR, that pin is an output.
Title: Re: MSP430 Noob
Post by: komet on March 20, 2013, 06:38:04 am
        P1DIR |= 0x08;

Remove the above line because it sets P1.3 as an output. The PxDIR register should contain a 1 where you want an output line and a 0 where you want an input.

   P1IN = 0x08;

This doesn't do anything. P1IN is a read-only register.

Once you have set up P1.3 as an input, P1IN contains the status of the button. So you could do

P1DIR = 0x41;
for (;; ) {
  if (P1IN & 0x08)
    P1OUT &= 0xFE;
    P1OUT |= 0x01;

and the LED will light up when you press the button and go out when you release it.

Title: Re: MSP430 Noob
Post by: aep9690 on March 20, 2013, 05:05:47 pm
So I made the changes to my code.

#include  <msp430g2553.h>

unsigned int i = 0;

void main(void){

   // Define the GPIO Pins
   P1DIR = 0x41;

   // Write PIN0 to HIGH
   P1OUT = 0x01;

      if((P1IN & 0x08) == 0x08){
         P1OUT &= 0xFE;
         P1OUT |= 0x01;
      for(i = 0;i < 16000; i++){}

However the only thing that happens is that LED1 on PIN0 lights up.  The P1IN register is still set to 0x07.

I was suspicious that PIN3 was at ground all the time so I broke out my multimeter and measured it.  The pin was sitting at about .98V, which is strange for a digital system.  I looked at the schematic files for the board ( ( and found that the pull up resistor and debounce capacitor are R34 and C24.  I looked at my board and sure enough they weren't populated (even though R34 should be).  I think the solution to my problem is to enable a pull up resistor however I will have to try this later as I have to get to class.  I'll let you guys know if it works.
Title: Re: MSP430 Noob
Post by: oPossum on March 20, 2013, 06:27:41 pm
Code: [Select]

#include <msp430.h>

void main(void) {

 P1DIR = BIT6 | BIT0;  // P1.6 and P1.0 as outputs

 P1OUT = BIT3 | BIT0;  // Use pull-up on P1.3, Turn on P1.0

 P1REN = BIT3;  // Enable pull-up on P1.3 (SW2)

 for(;;) {
  (P1IN & BIT3) ? (P1OUT &= ~BIT0) : (P1OUT |= BIT0);
Title: Re: MSP430 Noob
Post by: aep9690 on March 20, 2013, 08:27:26 pm
Thanks that fixed it
Title: Re: MSP430 Noob
Post by: linguisticat on March 22, 2013, 01:40:47 am
I would just add one line below your P1DIR to ensure that P1.3 is an input (in case there is junk in the P1DIR register).

P1DIR &= ~BIT3;

This guarantees that the button is an input.