打印
[STM32F1]

timer2进不了中断或一开中断就死机了

[复制链接]
2400|16
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
江陵龙少|  楼主 | 2018-7-3 18:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用STM32F103TB,中密度器件,裸机,各位大大给看看

int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */
        uint8 i     = 0;
        uint32 code = 0;
       
  PeriphClkEnable();
        NVIC_Configuration();
        PinCfg();
        Timer2Init();


void PeriphClkEnable(void)
{
  /* DMA clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
       
        RCC_APB2PeriphClockCmd(  RCC_APB2Periph_GPIOA
                               | RCC_APB2Periph_GPIOB
//                                                                                                  | RCC_APB2Periph_GPIOC
                                                                                                 | RCC_APB2Periph_GPIOD
//                                                                                                  | RCC_APB2Periph_GPIOE
                               | RCC_APB2Periph_AFIO
                               | RCC_APB2Periph_USART1        
//                                | RCC_APB2Periph_ADC1
//                                | RCC_APB2Periph_TIM1
                                                                                                 , ENABLE);
       
        RCC_APB1PeriphClockCmd(  RCC_APB1Periph_USART2        
//                                | RCC_APB1Periph_USART3
//                                                                                                  | RCC_APB1Periph_UART4         
//                                                                                                  | RCC_APB1Periph_UART5         
//                                                                                                  | RCC_APB1Periph_I2C1
//                                                                                                  | RCC_APB1Periph_I2C2
//                                | RCC_APB1Periph_DAC
                         | RCC_APB1Periph_TIM2
                                                                                                 , ENABLE);
                                                                                                 
                                                                                                 
                                                                                                 
       
}


void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);
}


void Timer2Init(void)
{
//         NVIC_InitTypeDef NVIC_InitStructure;  
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

//   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//   
//   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
//   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
//   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
//   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//   NVIC_Init(&NVIC_InitStructure);  

  TIM_TimeBaseStructure.TIM_Period = F_MY / 2;                          /* 设置自动重载寄存器值为最大值      */
  TIM_TimeBaseStructure.TIM_Prescaler = (uint16)PRESCALER - 1;        /* 计数频率为主频 / PRESCALER        */
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

        TIM_ARRPreloadConfig(TIM2, ENABLE);       

  TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  TIM_ITConfig(TIM2, TIM_IT_Update , ENABLE);//开启计数中断
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);//开启时钟
}

void TIM2_IRQHandler(void)
{
        if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
        {
                TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
//                 TIM_Cmd(TIM2, DISABLE);
//                 printf("\n\r0x%08x", 0x12345678);
                Flag = 1;
        }
}


评论
xch 2018-7-6 17:58 回复TA
中断向量表在哪里? 
沙发
xuanhuanzi| | 2018-7-3 18:43 | 只看该作者
这个103的例子一堆吧,官方的开发资源包里有这个应用的例子吧

使用特权

评论回复
板凳
mmuuss586| | 2018-7-3 21:00 | 只看该作者
看了下程序没啥问题,你用CUBE软件生成初始化代码看看

使用特权

评论回复
地板
gaoke231| | 2018-7-3 21:15 | 只看该作者
用CUBE生成一个也行,简单不用怎么调试。

使用特权

评论回复
5
gaoke231| | 2018-7-3 21:16 | 只看该作者
你也可以换成、TIM3试试,看下你的定时器配置是否有问题

使用特权

评论回复
6
hanzhen654| | 2018-7-3 21:25 | 只看该作者
换个定时器试下,我没怎么用TIM2 一般TIM3或者TIM5

使用特权

评论回复
7
香水城| | 2018-7-4 08:12 | 只看该作者
感觉进不了中断或一开中断就死机,
注意启动文件里的中断函数名跟你实际函数体是否一致,注意选正确的启动文件;
另外相关中断请求位在中断里做了清除没有;

使用特权

评论回复
8
734774645| | 2018-7-4 23:15 | 只看该作者
cubeF 1里有个TIM3的例子,不过用宏表示了,应该可以直接修改成2,你试试看

使用特权

评论回复
9
734774645| | 2018-7-4 23:15 | 只看该作者
/**
  ******************************************************************************
  * [url=home.php?mod=space&uid=288409]@file[/url]    TIM/TIM_TimeBase/Src/main.c
  * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  * [url=home.php?mod=space&uid=895143]@version[/url] V1.5.0
  * [url=home.php?mod=space&uid=212281]@date[/url]    14-April-2017
  * [url=home.php?mod=space&uid=247401]@brief[/url]   This sample code shows how to use STM32F1xx TIM HAL API to generate
  *          a time base of one second with the corresponding Interrupt request.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/** @addtogroup STM32F1xx_HAL_Examples
  * @{
  */

/** @addtogroup TIM_TimeBase
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* TIM handle declaration */
TIM_HandleTypeDef    TimHandle;

/* Prescaler declaration */
uint32_t uwPrescalerValue = 0;

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void Error_Handler(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F1xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user
         can eventually implement his proper time base source (a general purpose
         timer for example or other time source), keeping in mind that Time base
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 24 MHz */
  SystemClock_Config();

  /* Configure LED3 & LED4 */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);

  /*##-1- Configure the TIM peripheral #######################################*/
  /* -----------------------------------------------------------------------
    In this example TIM3 input clock (TIM3CLK)  is set to APB1 clock (PCLK1),
    since APB1 prescaler is equal to 1.
      TIM3CLK = PCLK1
      PCLK1 = HCLK
      => TIM3CLK = HCLK = SystemCoreClock
    To get TIM3 counter clock at 10 KHz, the Prescaler is computed as following:
    Prescaler = (TIM3CLK / TIM3 counter clock) - 1
    Prescaler = (SystemCoreClock /10 KHz) - 1

    Note:
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f1xx.c file.
     Each time the core clock (HCLK) changes, user had to update SystemCoreClock
     variable value. Otherwise, any configuration based on this variable will be incorrect.
     This variable is updated in three ways:
      1) by calling CMSIS function SystemCoreClockUpdate()
      2) by calling HAL API function HAL_RCC_GetSysClockFreq()
      3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  ----------------------------------------------------------------------- */

  /* Compute the prescaler value to have TIMx counter clock equal to 10000 Hz */
  uwPrescalerValue = (uint32_t)(SystemCoreClock / 10000) - 1;

  /* Set TIMx instance */
  TimHandle.Instance = TIMx;

  /* Initialize TIMx peripheral as follows:
       + Period = 10000 - 1
       + Prescaler = (SystemCoreClock/10000) - 1
       + ClockDivision = 0
       + Counter direction = Up
  */
  TimHandle.Init.Period            = 10000 - 1;
  TimHandle.Init.Prescaler         = uwPrescalerValue;
  TimHandle.Init.ClockDivision     = 0;
  TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  TimHandle.Init.RepetitionCounter = 0;
  TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

  if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /*##-2- Start the TIM Base generation in interrupt mode ####################*/
  /* Start Channel1 */
  if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
  {
    /* Starting Error */
    Error_Handler();
  }

  while (1)
  {
  }
}

/**
  * @brief  Period elapsed callback in non blocking mode
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  BSP_LED_Toggle(LED3);
}

/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow :
  *            System Clock source            = PLL (HSE)
  *            SYSCLK(Hz)                     = 24000000
  *            HCLK(Hz)                       = 24000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 1
  *            APB2 Prescaler                 = 1
  *            HSE Frequency(Hz)              = 8000000
  *            HSE PREDIV1                    = 2
  *            PLLMUL                         = 6
  *            Flash Latency(WS)              = 0
  * @param  None
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef clkinitstruct = {0};
  RCC_OscInitTypeDef oscinitstruct = {0};
  
  /* Enable HSE Oscillator and activate PLL with HSE as source */
  oscinitstruct.OscillatorType  = RCC_OSCILLATORTYPE_HSE;
  oscinitstruct.HSEState        = RCC_HSE_ON;
  oscinitstruct.HSEPredivValue  = RCC_HSE_PREDIV_DIV2;
  oscinitstruct.PLL.PLLState    = RCC_PLL_ON;
  oscinitstruct.PLL.PLLSource   = RCC_PLLSOURCE_HSE;
  oscinitstruct.PLL.PLLMUL      = RCC_PLL_MUL6;
  if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
     clocks dividers */
  clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
  clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_0)!= HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }
}


/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
static void Error_Handler(void)
{
  /* Turn LED4 on */
  BSP_LED_On(LED4);
  while (1)
  {
  }
}
#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}

#endif

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

使用特权

评论回复
10
dongnanxibei| | 2018-7-5 11:31 | 只看该作者
解决了没。例子里用的回调函数,你是直接用的中断处理,不太清楚这两个有什么关系。

使用特权

评论回复
11
小灵通2018| | 2018-7-5 20:01 | 只看该作者
能不能debug,看看死哪个循环了。

使用特权

评论回复
12
小灵通2018| | 2018-7-5 20:02 | 只看该作者
能不能debug,看看死哪个循环了。

使用特权

评论回复
13
江陵龙少|  楼主 | 2018-7-6 14:48 | 只看该作者
各位大大,终于找到原因了,原因在system_stm32f10x.c文件中的 #define VECT_TAB_OFFSET  0x1000。改为0就可以了,由于之前使用了bootloader,导致这里的宏定义忘了改,感谢大家的帮助,请大家吸取教训,引以为戒,现附上正确的代码图片

使用特权

评论回复
14
江陵龙少|  楼主 | 2018-7-6 14:50 | 只看该作者
手机上传图片太困难了

使用特权

评论回复
15
江陵龙少|  楼主 | 2018-7-6 18:40 | 只看该作者
关于中断,自我总结下供各位参考:1,NVIC开了对应通道中断没? 2,外设开了中断没? 3,中断服务函数写对了没? 4,向量偏移设对了没?从以上4方面考虑

使用特权

评论回复
16
木木guainv| | 2018-7-19 13:38 | 只看该作者
你一进中断就关闭timer2试试呢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

85

主题

279

帖子

2

粉丝