MSP430FR5969 Question(s) - Page 1
Forum section: electronics / beginners

author: Karamel
author_rank: Regular Contributor
timestamp: March 25, 2017, 03:24:51 pm
content: Hi,I am dealing with a problem which is about msp430 launcher's button. I have written this code which is below. When I debug it, P1IN register changes stupidly. (with or without pressing button) I can not read exact value of P1IN.Why does it behavior like that?Code:#include <msp430.h>volatile unsigned int i;//to prevent optimizationvoid main(void){WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timerPM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode// to activate previously configured port settingsP1DIR |= 0x01; // Set P1.0 to output directionP1REN = 0x00;for(;;){if(P1IN == 0)P1OUT = 0x01;//BIT 0 LED ONelseP1OUT = 0X00;//LED OFF}}

author: Nusa
author_rank: Super Contributor
timestamp: March 25, 2017, 04:48:41 pm
content: Remember that those registers are bytes, with 8 bits associated with 8 physical pins. When you set P1REN to 0, you turned off the internal resistors for all 8 of those pins. That means any of those 8 pins set for input are floating (likely producing random values) if they aren't connected to anything. That probably includes your button switch if it doesn't have an external pullup/down.That also means "if (P1IN == 0)" is actually saying "if ANY of the input pins on P1 are non-zero, return false". I suspect that's not what you wanted.Similarly setting P1OUT = 0 is turning off all outputs on P1. (And would affect resistor usage on inputs, if resisters were enabled.) Since you only have one pin set for output, you sorta get away with this one right now, but it'll get you in trouble later.

author: Karamel
author_rank: Regular Contributor
timestamp: March 25, 2017, 06:42:29 pm
content: Code:#include <msp430.h>volatile unsigned int i;//to prevent optimizationvoid main(void){WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timerPM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode// to activate previously configured port settingsP1DIR |= 0x01; // Set P1.0 to output directionP1REN = 0xFF;P1OUT = 0x00;while(1);}I changed my code like that, I am, now, debuging and still it changes stupidly when it is on while infinite loop. I try to get button press information from P1.1 and wanna show it on P1.0 pin.Could you write this code to try on mine board? Maybe, my board has broken down. I don't know.

author: Nusa
author_rank: Super Contributor
timestamp: March 25, 2017, 07:05:21 pm
content: But does the single bit you care about change stupidly?if ((P1IN & 0x02) == 0) // Test BIT 1P1OUT |= 0x01; // BIT 0 LED ONelseP1OUT &= 0xFE; // BIT 0 LED OFF

author: Karamel
author_rank: Regular Contributor
timestamp: March 25, 2017, 07:22:28 pm
content: what does mean?P1OUT!=0x01; // BIT 0 LED ON

author: Nusa
author_rank: Super Contributor
timestamp: March 25, 2017, 07:28:13 pm
content: Meant |=Stupid typo....fixed it.

author: Karamel
author_rank: Regular Contributor
timestamp: March 25, 2017, 07:47:51 pm
content: Code:void Delay(){i = 1000; // SW Delaydo i--;while(i != 0);}int main(void) {WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timerPM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode// to activate previously configured port settings// P4DIR = 0x00;// P4REN = 0xFF;// P4OUT = 0xFF;P1DIR = 0x01;P1REN = 0xFF;P1OUT = 0xFF;while(1){while((P1IN & 0x02) == 0) // Test BIT 1{Delay();if((P1IN & 0x02) == 1){P1OUT ^= 0x01; // BIT 0 LED ON}}}}I can, now, recognize its pressing down but why it doesn't toggle led?

author: Karamel
author_rank: Regular Contributor
timestamp: March 25, 2017, 07:56:08 pm
content: I solved it like that,Code:while(1){while((P1IN & 0x02) == 0) // Test BIT 1{Delay();while((P1IN & 0x02) == 0);Delay();P1OUT ^= 0x01; // BIT 0 LED ON}}