STM32F429I定时器中断

时间:2016-10-01 08:58:56

标签: c timer microcontroller interrupt stm32

我尝试在STM32F429I上实现定时器中断,但我还没有成功。 Timer运行正常(用调试器观察),但中断函数永远不会被调用。如果我激活中断然后调用函数GPIO_WriteBit(),系统似乎挂起,至少当我使用调试器并调用函数时,调试器挂起。这是迄今为止的代码:

#include "stm32f4xx.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_syscfg.h"

//****************************************************************************
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* GPIOA clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

  /*  pin configuration */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  //GPIO_PinAFConfig(GPIOC, GPIO_PinSource6,  GPIO_AF_TIM2); // PA6 TIM2_CH1
}

//****************************************************************************

void TIM2_Configuration(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  int i;

  /* Enable TIM2 Peripheral clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  TIM_TimeBaseStructure.TIM_Prescaler = 60000;
  TIM_TimeBaseStructure.TIM_Period = 1500;      //1Hz Interrupt frequency
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_Cmd(TIM2, ENABLE);

  // Likely will interrupt initially unless we clear it

  for(i=0; i<3; i++)
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
      TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}

//****************************************************************************

void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    GPIO_ToggleBits(GPIOA, GPIO_Pin_6);
  }
}

//****************************************************************************

void NVIC_Configuration(void)
{
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

}

//****************************************************************************

int main(void)
{
    SystemInit();

    if (SysTick_Config(SystemCoreClock / 1000))
    {
        /* Capture error */
        while (1);
    }

    NVIC_Configuration();

    GPIO_Configuration();

    TIM2_Configuration();

    GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_SET);

    while(1) {

    }
}

//******************************************************************************

0 个答案:

没有答案