This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2352 P2.6 NOT PULLING DOWN

Hi,

i am trying following code in my application

#include <msp430G2352.h>

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

P1DIR = BIT0;
P2SEL |= 0x00;
P2OUT |= BIT6;
P2REN |= BIT6;

while (1) 
{
if(BIT6 & P2IN)

P1OUT |= BIT0;
}
else 
{
P1OUT &= ~BIT0;
}
}
}

So essential i have P2.6 internally pulled up and set as input.

and if it is pulled high set P1.0 which is LED. and if P2.6 low turn off LED P1.0.

Now i am shorting P2.6 to GND purposly to see if LED turns off , but its not.

SO somehow P2.6 is not  pulling low to turn off LED.

what is wrong in my code?

is P2.6 is not I/O pin?

  • Do you know what the does "P2SEL |= 0x00;" do?

  • P2sel is port selection register. It is set to 0 so that it is selected as I / O

  • Sorry, my fingers did not listen to my brain when I send you the last msg.

    What I meant was, that statement did not do what you intended to do.

    I think you are another victim of "leaning c by examples".

  • Sorry i didnt understand what you are saying  victim of "leaning c by examples".

    All i want is changing P1.0 output with change in P2.6.

    if i swap P2.6 with P1.1 code works fine. it will change P1.0's output according to P1.1. 

    If anything is wrong in coding let me know.

  • I think you do not understand the difference between "=" and "|=". You just use "|=" because you saw it being used in "examples". There are too many bad examples including examples that "works".

    "<something> |= <expression>;" Is equivalent to: "<something> = <something> | <expression>;"

    Your "P2SEL |= 0x00;" is equivalent to: "P2SEL = P2SEL | 0x00;" which in turn is equivalent to: "P2SEL = P2SEL;" Thus you have accomplish nothing other than reading P2SEL, writing the same thing back, and wasting a few CPU cycles. If P2SEL were 0x00 to begin with, your code would have "worked perfectly" and you would have provided another "example" for other victims to follow.

  • Thanks for reply.

    so during initilization of ports i need to use = insted |= right?

  • abhishek Sabnis said:
    ... so during initilization of ports i need to use = insted |= right?

    Is that a trick question?

    Engineers like to follow rules. What you said is a bad rule. A better rule is: one should try to understand the meaning and effects of each statement and use it appropriately.

  • abhishek Sabnis said:
    so during initilization of ports i need to use = insted |= right?

    Yes and no. it depends on your intention.

    = assigns a completely new configuration fotall bits in teh register, no matter what the previous value was.

    |= sets individual bits in the regisster while keeping all others untouched. This is whay |=0 does nothing as it doesn't set any bit and keeps the rest unchanged.

    &=~(x) clears individual bits and keeps the others.

    And in case you have a bitfield, you'll have to clear all bits of the bifield first before setting the new ones, or else you might just add bits and keep the others and end up with something completely different.

    However, what OCY tries to tell you is that all this is plain standard C language and by no means related to the MSP430.

    The MSP related part is to know that the P1DIR register, or all others, appear as normal variables to the C compiler, but the content of these variables has an immediate hardware effect. If a bit is set or cleared influences the behavior of the port pin that is connected to this register. What effect this is is explained in detail in the digital I/O chapter of the users guide.

**Attention** This is a public forum