MSP430 Noob - Page 1
Forum section: electronics / general

author: aep9690
author_rank: Regular Contributor
timestamp: March 20, 2013, 04:41:02 am
content: 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, 0b01001001P1DIR = 0x01;P1DIR |= 0x40;P1DIR |= 0x08;// Write PIN0 to HIGH, 0b00000001P1OUT = 0x01;// Define PIN3 as an input, 0b00001000P1IN = 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 = 0b00000111P1OUT = 0b00000001P1DIR = 0b01001001The 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.

author: ve7xen
author_rank: Super Contributor
timestamp: March 20, 2013, 05:57:55 am
content: 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 wellBits 1 and 2 are set because they're configured as an input (PxDIR bit clear) and are floating or externally pulled upBit 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.

author: komet
author_rank: Unknown
timestamp: March 20, 2013, 06:38:04 am
content: Quote from: aep9690 on March 20, 2013, 04:41:02 amRemove 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.Quote from: aep9690 on March 20, 2013, 04:41:02 amThis 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 doP1DIR = 0x41;for (;; ) {if (P1IN & 0x08)P1OUT &= 0xFE;elseP1OUT |= 0x01;}and the LED will light up when you press the button and go out when you release it.

author: aep9690
author_rank: Regular Contributor
timestamp: March 20, 2013, 05:05:47 pm
content: So I made the changes to my code.#include <msp430g2553.h>unsigned int i = 0;void main(void){WDTCTL = WDTPW + WDTHOLD;// Define the GPIO PinsP1DIR = 0x41;// Write PIN0 to HIGHP1OUT = 0x01;while(1){if((P1IN & 0x08) == 0x08){P1OUT &= 0xFE;}else{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.UPDATE: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 (http://www.ti.com/tool/msp-exp430g2) 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.

author: oPossum
author_rank: Super Contributor
timestamp: March 20, 2013, 06:27:41 pm
content: Code:#include <msp430.h>void main(void) {WDTCTL = WDTPW | WDTHOLD;P1DIR = BIT6 | BIT0; // P1.6 and P1.0 as outputsP1OUT = BIT3 | BIT0; // Use pull-up on P1.3, Turn on P1.0P1REN = BIT3; // Enable pull-up on P1.3 (SW2)for(;;) {(P1IN & BIT3) ? (P1OUT &= ~BIT0) : (P1OUT |= BIT0);__delay_cycles(50000);}}

author: aep9690
author_rank: Regular Contributor
timestamp: March 20, 2013, 08:27:26 pm
content: Thanks that fixed it

author: linguisticat
author_rank: Newbie
timestamp: March 22, 2013, 01:40:47 am
content: 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.