Jump to content
Sign in to follow this  
JustRic

Total Noob With Interrupt Problems

Recommended Posts

I just started playing with the MSP430 with the G2553 chip and am having problems with interrupts.  I have the following code just for testing and sometimes pressing the button works and sometimes it doesn't.  I thought maybe I had a bad board and replaced it but I still have the same problem.  I am beginning to think I am just doing something wrong in the debugging or something.  But, here is my code:

#include <msp430g2553.h>

#define LED1  BIT0

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer
    BCSCTL1 = CALBC1_1MHZ;      //Set DCO to 1Mhz
    DCOCTL = CALDCO_1MHZ;
    P1OUT=BIT0;
    P1DIR=BIT0;                 //P1.0 (LED) as output)

    P1IES |= BIT3;              // high -> low is selected with IESx = 1.
    P1IFG &= ~BIT3;             // To prevent an immediate interrupt, clear the flag for
                                // P1.3 before enabling the interrupt.
    P1IE |= BIT3;               // Enable interrupts for P1.3
    
    __enable_interrupt();
    
    P1OUT |= BIT0;              //Initially the led will glow


    while(1)
    {}

    return 0;
}

#pragma vector = PORT1_VECTOR   //PORT1 interrupt vector name
__interrupt void P1_ISR(void) {


             P1OUT ^= BIT0;
             P1IFG &= ~BIT3;   // clear the interrupt flag
}

So I hit the debug button in CCS 7.4, and it loads to the board.  I have a breakpoint at stopping the watchdog timer and a breakpoint at toggling BIT0 in the ISR.

I hit resume at the watchdog timer and start trying the button at P1.3.  Maybe 1 out of 10 times I'll hit the breakpoint in the ISR but the rest of the time I get nothing.

I apologize if this is just a really basic and dumb question, but I have Googled everything I can think of and searched the forums and come up with nothing.

Any help is appreciated,

Share this post


Link to post
Share on other sites

Well, you've never turned on the power to the switch... so to speak.  Also, you'll want to enable the internal resistor on BIT3

The following seems to work ok for me. And, since you're using CCS, take a look at the port registers in the debugger; it's really a useful resource to see if what you did is what you intended.

include <msp430.h>

#define LED1  BIT0

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer
    BCSCTL1 = CALBC1_1MHZ;      //Set DCO to 1Mhz
    DCOCTL = CALDCO_1MHZ;

    
    P1OUT |= (LED1  + BIT3);       //set both bit0 and bit3
    P1DIR |= LED1;                      //as output
    P1REN = BIT3;              // enable the pull up resistor
    P1IES |= BIT3;              // high -> low is selected with IESx = 1.
    P1IFG &= ~BIT3;             // To prevent an immediate interrupt, clear the flag for
                                // P1.3 before enabling the interrupt.
    P1IE |= BIT3;               // Enable interrupts for P1.3

    __enable_interrupt();
    P1OUT |= LED1  ;              //Initially the led will glow

 

    while(1)
    {

        __no_operation(); //handy for debugger
    }

}

#pragma vector = PORT1_VECTOR   //PORT1 interrupt vector name
__interrupt void P1_ISR(void) {

             __no_operation();
             P1OUT ^= LED1  ;
             P1IFG &= ~BIT3;   // clear the interrupt flag
}

 

Share this post


Link to post
Share on other sites

BTW - you will likely see that the LED toggles from off->on, or on->off.  Switches exhibit bounce, so High->Low, Lo->High happens many many times with each button press.

Share this post


Link to post
Share on other sites

Thank you so much, NurseBob!  It works as it should now, and I can see the switch bounce you are talking about.  But it is interrupting like it should.  I was thinking of BIT3 as an input and apparently this was wrong, but I don't understand that logic.  To me, pressing the button is an input that causes the interrupt.  Why is that not the case?

 

Thanks again!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×