smt32 interrupts. why my diode is not blinking?

225 Views Asked by At

I have smt32l1xx board and this code below is not working. Debugger shows pinA5 is set, but diode connected to this pin is still not lightening. I dont know why. Even i add delay after setting bit it is not working. diode is connected to PA5 and GND on board.

#include <stm32l1xx.h>

#define ENABLE 1
#define DISABLE 0


void TIM2_IRQHandler() //interrupt
{
 if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
 {
 TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

 if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_5))
 GPIO_ResetBits(GPIOA, GPIO_Pin_5); //LED OFF
 else
 GPIO_SetBits(GPIOA, GPIO_Pin_5); //LED ON <- im here and still nothing

 }
}

int main(void)
{
    /* gpio init struct */
    GPIO_InitTypeDef gpio;
    TIM_TimeBaseInitTypeDef tim;
    NVIC_InitTypeDef nvic;
    /* reset rcc */
    RCC_DeInit();


    RCC_APB2PeriphClockCmd(RCC_AHBENR_GPIOAEN, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    GPIO_StructInit(&gpio);
        /* use pin 0 */
        gpio.GPIO_Pin = GPIO_Pin_5;
        /* mode: output */
        gpio.GPIO_Mode = GPIO_Mode_OUT;
        /* apply configuration */
        GPIO_Init(GPIOA, &gpio);

     TIM_TimeBaseStructInit(&tim); //timer
     tim.TIM_CounterMode = TIM_CounterMode_Up;
     tim.TIM_Prescaler = 64000 - 1;
     tim.TIM_Period = 1000 - 1;
     TIM_TimeBaseInit(TIM2, &tim);

     TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
     TIM_Cmd(TIM2, ENABLE);

     nvic.NVIC_IRQChannel = TIM2_IRQn; //interrupt
     nvic.NVIC_IRQChannelPreemptionPriority = 0;
     nvic.NVIC_IRQChannelSubPriority = 0;
     nvic.NVIC_IRQChannelCmd = ENABLE;
     NVIC_Init(&nvic);


     while (1)
     {

     }
    /* never reached */
    return 0;
}
1

There are 1 best solutions below

1
Guillaume Michel On BEST ANSWER

To make sure that your hardware is properly initialized, you should use STM32CubeMX. It seems that the GPIOA clock is on the AHB bus, however you call RCC_APB2PeriphClockCmd which is for APB2. So try to use the equivalent for AHB something like RCC_AHBPeriphClockCmd