Home > Net >  IRQHandler variable
IRQHandler variable

Time:10-29

I wrote interrupt handler code here. The purpose of my code is to interrupt when I press the button. However, there is a problem. When the button is pressed, it interrupts and enters the handler, but does not increase the Buttonpress. I see the debugger reads buttonpress , but after exiting the handler, the code freezes when it comes to the switch. Buttonpress = 1 does not happen. What is the reason of this? Do I need to assign the variable differently when assigning it?

Here is my Code :


/* 1 sec is 1600000 */
void delay(volatile uint32_t);

uint32_t ButtonPress = 0;

/* Interrupt Handlers */
void EXTI0_1_IRQHandler(void){

    ButtonPress  ;
    EXTI->RPR1 |= (1U << 1);
}



int main(void) {

    uint32_t ButtonPress = 0;

    /* Enable GPIOA clock */
    RCC->IOPENR |= (1U << 0);

    /* Setup PA0 as output */
    GPIOA->MODER &= ~(3U << 2*0);
    GPIOA->MODER |= (1U << 2*0);

    /* Setup PA1 as input */
    GPIOA->MODER &= ~(3U << 2*1);
    GPIOA->MODER |= (0U << 2*1);

    /* External interrupt at PA1 port */
    EXTI->EXTICR[0] |= (0U << 8*1);

    /* Mask and Rising on Px1 */
    EXTI->IMR1  |= (1U << 1);
    EXTI->RTSR1 |= (1U << 1);
    EXTI->FTSR1 |= (1U << 1);

    /* Setup NVIC */
    NVIC_SetPriority(EXTI0_1_IRQn, 0);
    NVIC_EnableIRQ(EXTI0_1_IRQn);

    while(1) {

        switch(ButtonPress){

            case 0:
                /* Turn off LED */
                GPIOA->ODR = (0U << 0);
                break;

            case 1:
                /* Turn on LED 2sec interval */
                GPIOA->ODR |= (1U << 0);
                delay(3200000);
                GPIOA->ODR ^= (1U << 0);
                delay(3200000);
                break;

            case 2:
                /* Turn on LED 1sec interval */
                GPIOA->ODR |= (1U << 0);
                delay(1600000);
                GPIOA->ODR ^= (1U << 0);
                delay(1600000);
                break;

            case 3:
                /* Turn on LED 0.5sec interval */
                GPIOA->ODR |= (1U << 0);
                delay(800000);
                GPIOA->ODR ^= (1U << 0);
                delay(800000);
                break;

            case 4:
                /* Turn on LED 0.1sec interval */
                GPIOA->ODR |= (1U << 0);
                delay(160000);
                GPIOA->ODR ^= (1U << 0);
                delay(160000);
                break;

            case 5:
                /* Turn on LED */
                GPIOA->ODR |= (1U << 0);
                break;

            case 6:
                /* Button Clear */
                ButtonPress = 0;
                break;
        }
    }
    return 0;
}

void delay(volatile uint32_t s) {
    for(; s>0; s--);
}

`

I wrote interrupt handler code here. The purpose of my code is to interrupt when I press the button. However, there is a problem. When the button is pressed, it interrupts and enters the handler, but does not increase the Buttonpress. I see the debugger reads buttonpress , but after exiting the handler, the code freezes when it comes to the switch. Buttonpress = 1 does not happen. What is the reason of this? Do I need to assign the variable differently when assigning it?

CodePudding user response:

You have declared the ButtonPress variable twice: once in the global scope and once in main. When the interrupt is called, only the global scope version is in scope and therefore that variable is incremented. When it comes to the switch statement in main however, the other variable is in (a smaller) scope and so that variable is used instead. To fix this, you could remove the declaration of ButtonPress at the beginning of main.

  • Related