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.

MSP430G2553: How to xor just one bit with button interrupt?

Part Number: MSP430G2553


Hi, i am new with msp430.

Im trying to toggle just selcted pin.But when a pin is active high and when i xor another one, first pin gets low.I just want to toggle only selected pin.

Here is my code

P1DIR = BIT0+BIT5+BIT6+BIT7; //leds
     P1OUT=0x00;
    
    P1IE = BIT4;
    P1IES=BIT4; //falling edge 
    __enable_interrupt();
    while(1){    }


#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{   
 
    counter++;
    if(countery==1){startTimer();  
    P1IFG = ~BIT4;  }
    else if(counter==2){stopTimer(); time=readTimer(); clearTimer(); counter=0; ;  
    }
  
    
    if(time>7000&& time<8000){ P1OUT ^= BIT0; __delay_cycles(50000); 
     P1IFG = ~BIT4;

else if(time>13500&& time<15000){ P1OUT ^= BIT5; __delay_cycles(50000);
P1IFG = ~BIT4;
}


} }

  • Hey Sunil,

    I don't fully understand the question/issue, but It looks like you are XORing correctly to me.  Where are you seeing the issue?  

    P1OUT ^= BIT0;

    This is correct XOR.  This will toggle Bit 0 regardless of what it is currently set to.  The rest of the bits should be preserved.  

    P1IFG = ~BIT4;
    // and
    P1IE = BIT4;

    It looks like you have some mistakes here.  It looks like you are trying to just set or clear a single bit, but you are actually modifying all bits. For the top example, you are clearing Bit 4, but actually setting every other bit. 

    This is probably the best way to set and clear a single bit:    

    P1IFG &= ~BIT4;  // Clear single bit, preserve others
    
    P1IE |= BIT4;   // Set Single Bit, preserve others

    Thanks,

    JD

**Attention** This is a public forum