Why my leds does not response to my interrupts

74 Views Asked by At

Im working on a project which will use pa1 and pa2 as external interrupts and using pb12 pb13 pb14 pb15 as led outputs.My aim is that when i press pa1 leds will turn on from pb12 to pb15 in an order,when i press pa2 leds will turn on from pb15 to pb12 in other words with reverse direction.Also pa1 interrupt's priority is higher than pa2 interrupts's.But when i press the buttons,i cannot get any response from the leds anyway.

#include "stm32f4xx.h"

void delay(uint32_t time) {
    time--;
}

void gpioa_config(void) { //input port (input pins are 1 2)
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
    GPIO_InitTypeDef gpio_init;
    gpio_init.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_2;
    gpio_init.GPIO_Mode=GPIO_Mode_IN;
    gpio_init.GPIO_OType=GPIO_OType_PP;
    gpio_init.GPIO_PuPd=GPIO_PuPd_DOWN;
    gpio_init.GPIO_Speed=GPIO_Speed_100MHz;
    GPIO_Init(GPIOA,&gpio_init);


}
void gpiob_config(void) { //output port(output pins are 12 13 14 15)
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
    GPIO_InitTypeDef gpiob_init;
    gpiob_init.GPIO_Pin=GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    gpiob_init.GPIO_Mode=GPIO_Mode_OUT;
    gpiob_init.GPIO_OType=GPIO_OType_PP;
    gpiob_init.GPIO_PuPd=GPIO_PuPd_NOPULL;
    gpiob_init.GPIO_Speed=GPIO_Speed_100MHz;
    GPIO_Init(GPIOB,&gpiob_init);

}

void exti_config(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
    EXTI_InitTypeDef exti_init;
    exti_init.EXTI_Line= EXTI_Line1 | EXTI_Line2;
    exti_init.EXTI_LineCmd=ENABLE;
    exti_init.EXTI_Mode=EXTI_Mode_Interrupt;
    exti_init.EXTI_Trigger=EXTI_Trigger_Rising;
    EXTI_Init(&exti_init);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    NVIC_InitTypeDef nvic_init;
    nvic_init.NVIC_IRQChannel=EXTI1_IRQn;
    nvic_init.NVIC_IRQChannelCmd=ENABLE;
    nvic_init.NVIC_IRQChannelPreemptionPriority=0; // channel 1's priority is higher than channel 2's
    nvic_init.NVIC_IRQChannelSubPriority=0;
    NVIC_Init(&nvic_init);

    NVIC_InitTypeDef nvic_init_2;
    nvic_init_2.NVIC_IRQChannel=EXTI2_IRQn;
    nvic_init_2.NVIC_IRQChannelCmd=ENABLE;
    nvic_init_2.NVIC_IRQChannelPreemptionPriority=1;
    nvic_init_2.NVIC_IRQChannelSubPriority=0;
    NVIC_Init(&nvic_init_2);





}

void exti1_irqhandler(void) {
    if (EXTI_GetITStatus(EXTI_Line1)!=RESET) {
        GPIO_SetBits(GPIOB,GPIO_Pin_12);
        delay(840000);
        GPIO_SetBits(GPIOB,GPIO_Pin_13);
        delay(840000);
        GPIO_SetBits(GPIOB,GPIO_Pin_14);
        delay(840000);
        GPIO_SetBits(GPIOB,GPIO_Pin_15);
        delay(840000);
        EXTI_ClearITPendingBit(EXTI_Line1);

    }
}

void exti2_irqhandler(void) {
    if(EXTI_GetITStatus(EXTI_Line2)!=RESET) {
        GPIO_SetBits(GPIOB,GPIO_Pin_15);
        delay(840000);
        GPIO_SetBits(GPIOB,GPIO_Pin_14);
        delay(84000);
        GPIO_SetBits(GPIOB,GPIO_Pin_13);
        delay(84000);
        GPIO_SetBits(GPIOB,GPIO_Pin_12);
        delay(840000);
        EXTI_ClearITPendingBit(EXTI_Line2);
    }
}


int main(void)
{
    gpioa_config();
    gpiob_config();
    exti_config();

  while (1)
  {
      GPIO_ResetBits(GPIOB,GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
      exti1_irqhandler();
      exti2_irqhandler();

  }
}

MY PROTEUS SCHEMATIC

is there anything wrong on my proteus schematic or on my code?

I have configured nvic also for determining the priority but nothing changes.the result is same.

Where is my problem?

I was waiting that my leds will turn on due to my interrupts but there is no response on my leds due to my interrupts

1

There are 1 best solutions below

3
GerwazyMucha On

First of all how delay function is supposed to work ? Are you aware that program just goes to that function, in that function the operation 840000-1 is realized and that's it - the delay is the same for every argument time? If you want to use delay use for example empty for loop:

for(int i=0; i<840000; i++)
   {
    // just empty loop for delay
   }

Moreover I suspect that such delay(if works) would be too long to be realized in interrupt.Try with smaller values. It would be also helpful if you tell us more what is the problem. Try debug code and tell whether the program even enters the interrupts ( use breakpoints while debugging). If you don't know how to debug code on stm32 I'm sure you would find plenty tutorials on yt.