Thread: Interrupts

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    33

    Interrupts

    Code:
    void main(void)
    Code:
    
    
    Code:
    {
    volatile long int i=0;
    // Stop WDT
    WDTCTL = WDTPW + WDTHOLD;
    // Configure input/output pins
    // Setup P1.0 as an output port (for LED1)
    P1SEL = P1SEL & ~BIT0;
    P1DIR = P1DIR | BIT0;
    // Setup P1.1 as an output port (for LED2)
    P1SEL = P1SEL & ~BIT1;
    P1DIR = P1DIR | BIT1;
    // Setup P2.6 as an input port (for SW1)
    P2SEL = P2SEL & ~BIT6;
    P2DIR = P2DIR & ~BIT6;
    // Enable the internal pull resistor and set as a pullup
    P2REN = P2REN | BIT6;
    P2OUT = P2OUT | BIT6;
    // Set-up the interrupt for P1.6 (SW1)
    // P2.6 Hi/lo edge (falling edge)
    P2IES |= BIT6;
    // P2.6 IFG cleared
    P2IFG &= ~BIT6;
    // P2.6 Interrupt Enable
    P2IE |= BIT6;
    // Enable interrupts globally
    _EINT();
    // Polling loop with software delay
    for(;;) // this is equivalent to while(1)
    {
    // Toggle LED 2
    P1OUT ^= 0x02;
    // Spin round for n cycles
    for(i=0;i<30000;i++){;}
    }
    }
    // Port 2 Interrupt Service Routine (ISR)
    #pragma vector=PORT2_VECTOR
    __interrupt void Port_2(void)
    {
    // P1.0 = toggle LED 1
    P1OUT ^= BIT0;
    // clear P2.6 IFG
    P2IFG &= ~BIT6; }


    I'm very new to programming and my task is to modify this code so that when Switch 1 is released, LED 1 will remain on and when it's pressed, LED 1 will turn off.

    The way the code is right now, LED 1 will turn on and off every time I click switch 1 and LED 2 will blink constantly on a loop.

    Can someone tell me if this is correct what I've done to complete my task? I added:
    Code:
    
    
    Code:
    void main(void)
    {
    volatile long int i=0;
    // Stop WDT
    WDTCTL = WDTPW + WDTHOLD;
    // Configure input/output pins
    // Setup P1.0 as an output port (for LED1)
    P1SEL = P1SEL & ~BIT0;
    P1DIR = P1DIR | BIT0;
    // Setup P1.1 as an output port (for LED2)
    P1SEL = P1SEL & ~BIT1;
    P1DIR = P1DIR | BIT1;
    // Setup P2.6 as an input port (for SW1)
    P2SEL = P2SEL & ~BIT6;
    P2DIR = P2DIR & ~BIT6;
    // Enable the internal pull resistor and set as a pullup
    P2REN = P2REN | BIT6;
    P2OUT = P2OUT | BIT6;
    // Set-up the interrupt for P1.6 (SW1)
    // P2.6 Hi/lo edge (falling edge)
    P2IES |= BIT6;
    // P2.6 IFG cleared
    P2IFG &= ~BIT6;
    // P2.6 Interrupt Enable
    P2IE |= BIT6;
    // Enable interrupts globally
    _EINT();
    // Polling loop with software delay
    for(;;) // this is equivalent to while(1)
    {
    // Toggle LED 2
    P1OUT ^= 0x02;
    // Spin round for n cycles
    for(i=0;i<30000;i++){;}
    }
    }
    // Port 2 Interrupt Service Routine (ISR)
    #pragma vector=PORT2_VECTOR
    __interrupt void Port_2(void)
    { P2IES ^=BIT6;
    if(P2IN & BIT6)
    { // Turn on the LED P1OUT |= BIT0; } else { // Turn off the LED P1OUT &= ~BIT0; }
    // clear P2.6 IFG
    P2IFG &= ~BIT6; }


    Will this code work so that when LED 1 is released, it will stay off and when it's pressed, it will stay on. Also, will LED 2 continue to keep blinking on the loop as this happens?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,983
    Are you asking, or have you actually tried it and found it doesn't work?

    Because we don't have your exact hardware setup, so it's unlikely that we can test what you wrote.

    Also, watch out for bouncing switches.
    Switch Bounce and How to Deal with It
    If you're too quick in the code, you'll see rapid on/off sequences, which means if you're expecting to just toggle the LED state once, you could be in for surprises.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    33
    I'm asking if it looks like it will work. I've not got the hardware to test it right now but does it look ok?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,983
    Well I don't think you should be messing with P2IES inside the ISR.

    > // Set-up the interrupt for P1.6 (SW1)
    > // P2.6 Hi/lo edge (falling edge)
    > P2IES |= BIT6;
    So when exactly do you get an interrupt?
    A naive interpretation and simply wiring the switch to the interrupt pin suggests you only get an interrupt when you turn the switch off.

    Do you get any information at all when you turn a switch on?

    Can you read the switch state at all?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. interrupts
    By Nero in forum C Programming
    Replies: 3
    Last Post: 11-30-2009, 01:14 PM
  2. Replies: 6
    Last Post: 11-30-2009, 03:32 AM
  3. dos interrupts in C
    By =viki= in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 10:02 PM
  4. interrupts
    By Mr.Pink in forum C++ Programming
    Replies: 2
    Last Post: 07-08-2005, 12:57 PM
  5. Interrupts in PM?
    By VirtualAce in forum C Programming
    Replies: 0
    Last Post: 07-29-2003, 12:20 PM

Tags for this Thread