Hi,
below is the partial code that i am working on:
#include <msp430g2452.h>
void main(void){ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR = 0x07; // P1.0 output, else input P1OUT = 0x10; // P1.4 set, else reset P1REN |= 0x10; // P1.4 pullup
while (1) // Test P1.4 { if (0x10 & P1IN){
P1OUT |= 0x01; // if P1.4 set, set P1.0P1OUT |= 0x02;P1OUT |= 0x04; STUCK IN THIS LOOP FOREVER
} else{ P1OUT &= ~0x01; // else resetP1OUT &= ~0x02;P1OUT &= ~0x04; }
}}
The problem is STUCK IN THIS LOOP FOREVER. Even if input pin P1.4 reset the program is stuck in the P1.4 set loop. I am using TI MSP430G2352.
Please let me know what will be wrong potentially.
Thanks,
Abhishek
Anybody has any recommendations on given issue.
abhishek SabnisP1REN |= 0x10; // P1.4 pullup
This will always result to a logic HIGH at P1.4 if this input is floating (unconnected).
You will get a Logic LOW at the P1.4 input if you pull this pin to Ground.
BR,
Mo.
Mo already explained why you're always entering this branch: P1.4 is configured as input, pulled high by internal pullup resistor (P1REN |=0x10 / P1OUT = 0x10). So (P1IN & 0x10) is always true unless you externally pull the pin down to GND. If you do so, you'll seem stuck in the else branch instead.
abhishek Sabnis P1OUT |= 0x01; // if P1.4 set, set P1.0P1OUT |= 0x02;P1OUT |= 0x04;
This code is unnecessarily long and also leads to a larger program than necessary (the compiler is forced to generate separate access code for each access of a hardwar eregister).
P1OUT |= (BIT0 | BIT1 | BIT2);
will result in only one write access of all three btis simultaneously (smaller and faster). The use of BITx isntead oc numeric values also makes clear that you doN't want to write a number, but rather want to set specific (port) bits.
Similarly, the code in the else can be simplified to
P1OUT &= ~ (BIT0 | BIT1 | BIT2);
Note that I'm not adding the bits. While BIT0+BIT1 is the same as BIT0|BIT1, there is a huge difference between BIT0+BIT0 (== BIT1) and BIT0|BIT0 (==BIT0). Since you're setting bits and not adding values, the use of | shol dbe preferred to 'group' bits instead of adding them with an addition.
_____________________________________Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.
Jens and Mo,
Thanks for suggestions. I have mosfet driving port 1.4. so externally , MOSFET is pulling down P1.4 to 0. The thing is it seems to go in respective loops some times but sometimes it gets stuck in same loop even if P1.4 changes. I have no other inputs other than P1.4.
Abhi
Thsi is strange.
The compiler won't 'cache' the value of P1IN. And the hardware won't cache it too. So if the input pin changes below the low-going threshold voltage, P1IN.4 will be reset and the code should properly execute the else branch.
So I'd say check your hardware. Best do a check of the P1,4 pin voltage with a scope. A logic analyzer might interpret high and low differently than the MSP if the signal is in a critical range.
Jens,
I monitored P1.4 on scope. It changes from 3.3V to 0V upon transition but some how outputs wont, they are staying in the same state.
One more thing have 3 outputs who changes its state depending upon input signal at P1.4
1. one output drives LED
2. Drives Relay
3. pushpull Controller through mosfet.
Do you think i have noise problem? i am monitering VCC but its stable, i am sure that micro is resetting.
I think your board design with it's mosfet is probably wrong, learn more about pull-up and how to sink it.
You could test your code on LaunchPad.
LaunchPad board has LEDs at P1.0 and P1.6. And a push-button that grounds P1.3 when pushed. You only need very small changes in your code to accommodate the above I/O pin assignments.
abhishek SabnisI have mosfet driving port 1.4. so externally , MOSFET is pulling down P1.4 to 0. The thing is it seems to go in respective loops some times but sometimes it gets stuck in same loop even if P1.4 changes. I have no other inputs other than P1.4.
since you are polling an pin input state in a while loop, I would suggest you to enable the interrupt at this pin to see if you get the interrupt upon a high-low transition on this pin.