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.

Interrupt routine



Hi guys,

I've got the following problem:

I want that the MCU sleeps all the time in LPM3 until an interrupt pin is set thorugh a low-high signal. I have implemented the following code:

  while(1)
  {
    __bis_SR_register(LPM3_bits + GIE);     // Enter Low Power Mode 3 (LPM3) w/interrupt
                                          
  }

Can I then jump to the interrupt routine?

#pragma vector=PORT2_VECTOR
  __interrupt void Port_2(void)
  {
    readADC12();                             // begin ADC operation
   
    P2IFG &= ~0x040;                          // P2.4 IFG cleared
  }

thanks a lot!

 

 

  • If the port has been properly configured for the expected port interrupt (expected edge selected, input direction, perhasp pullups enabled, and of course the interrupt for this pin enabled), then there is no reason why the ISR shouldn't be called when the signal edge is detected.
    The posted code alone won't work, since it doesn't configure the port for interrupts.

    However, it is not the best idea calling other functions from inside the ISR, especially before you cleared the IFG bit. It clobbers registers (making hte ISR longer and slower than necessary), bears a risk that the funciton will enable interrupts (sending you into an endless interrupt loop until the stack overflows) and may corrupt all you timings.

    Also, if the signal source is a pushbutton or some other mechanical device, you might experience multiple interrupts ('bouncing'). In this case a delay timer is necessary, to block all further interrupts for some time.

  • As a suggestion, when you set bits, you're better off using LPM3_bits | GIE, rather than + to combine them.

    The C bitwise-OR operator will keep the expected behavior, which is to turn on both sets of bits.

    In this case, it doesn't hurt, since LPM3_bits and GIE have no bits in common, and that's true for most of the bitmasks, but if there is an overlap, you'll get something unexpected.

    Take for example a bit pattern of 00001001 (0x0b) and 00001010 (0x0c).  If you + them, you'll end up with 00010011 (0x13) which is different than the 00001011 you'd probably wanted, and might have some a very different result in the hardware.

    --Joe

  • Joe Moore said:
    As a suggestion, when you set bits, you're better off using LPM3_bits | GIE, rather than + to combine them.


    I prefer this way too. However, as long as the values itself are correct, both methods give the same result.

    The difference only happens if values are combined that do not belong to the same register. Depending of '+' or '|' used, the (in both cases unwanted) behavior is different. And in case of a '+' it is more difficult to track.

    If x and y are unrelated bits in a bitfield, x+y and x|y gives the same result.

    But "a += x' will have a different result than 'a |= x', if the bit or bit pattern represented by 'x' is alreready set or partially set in a.

    The difference between logical and arithmetical operations.

     

**Attention** This is a public forum