0

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;
}
docp
  • 307
  • 1
  • 9
  • Two things to try, firstly can you toggle the LED in normal (non interrupt code), to check your IO is configured correctly? Secondly is the interrupt actually ocurring? Can you repeatably hit a breakpoint in the interrupt? Try to narrow the problem down. – Realtime Rik Dec 19 '16 at 10:42

1 Answers1

1

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

Guillaume Michel
  • 1,189
  • 8
  • 14
  • You don't have to use STM32CubeMX. It can be easier for beginners but personally I prefer not to use it. I do tend to use the HAL drivers though, although they are bit messy between older and newer versions. – Realtime Rik Dec 19 '16 at 10:44