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

[复制链接]
3236|16
 楼主| 江陵龙少 发表于 2018-7-3 18:33 | 显示全部楼层 |阅读模式
使用STM32F103TB,中密度器件,裸机,各位大大给看看

  1. int main(void)
  2. {
  3.   /*!< At this stage the microcontroller clock setting is already configured,
  4.        this is done through SystemInit() function which is called from startup
  5.        file (startup_stm32f10x_xx.s) before to branch to application main.
  6.        To reconfigure the default setting of SystemInit() function, refer to
  7.        system_stm32f10x.c file
  8.      */
  9.         uint8 i     = 0;
  10.         uint32 code = 0;
  11.        
  12.   PeriphClkEnable();
  13.         NVIC_Configuration();
  14.         PinCfg();
  15.         Timer2Init();


  1. void PeriphClkEnable(void)
  2. {
  3.   /* DMA clock enable */
  4.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  5.        
  6.         RCC_APB2PeriphClockCmd(  RCC_APB2Periph_GPIOA
  7.                                | RCC_APB2Periph_GPIOB
  8. //                                                                                                  | RCC_APB2Periph_GPIOC
  9.                                                                                                  | RCC_APB2Periph_GPIOD
  10. //                                                                                                  | RCC_APB2Periph_GPIOE
  11.                                | RCC_APB2Periph_AFIO
  12.                                | RCC_APB2Periph_USART1        
  13. //                                | RCC_APB2Periph_ADC1
  14. //                                | RCC_APB2Periph_TIM1
  15.                                                                                                  , ENABLE);
  16.        
  17.         RCC_APB1PeriphClockCmd(  RCC_APB1Periph_USART2        
  18. //                                | RCC_APB1Periph_USART3
  19. //                                                                                                  | RCC_APB1Periph_UART4         
  20. //                                                                                                  | RCC_APB1Periph_UART5         
  21. //                                                                                                  | RCC_APB1Periph_I2C1
  22. //                                                                                                  | RCC_APB1Periph_I2C2
  23. //                                | RCC_APB1Periph_DAC
  24.                          | RCC_APB1Periph_TIM2
  25.                                                                                                  , ENABLE);
  26.                                                                                                  
  27.                                                                                                  
  28.                                                                                                  
  29.        
  30. }


  1. void NVIC_Configuration(void)
  2. {
  3.   NVIC_InitTypeDef NVIC_InitStructure;

  4.   /* Enable the TIM2 global Interrupt */
  5.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  6.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  7.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  8.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  9.   NVIC_Init(&NVIC_InitStructure);
  10. }


  1. void Timer2Init(void)
  2. {
  3. //         NVIC_InitTypeDef NVIC_InitStructure;  
  4.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  5. //   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  6. //   
  7. //   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  8. //   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  9. //   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  10. //   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  11. //   NVIC_Init(&NVIC_InitStructure);  

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

  16.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  17.         TIM_ARRPreloadConfig(TIM2, ENABLE);       

  18.   TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  19.   TIM_ITConfig(TIM2, TIM_IT_Update , ENABLE);//开启计数中断
  20.   /* TIM2 enable counter */
  21.   TIM_Cmd(TIM2, ENABLE);//开启时钟
  22. }

  1. void TIM2_IRQHandler(void)
  2. {
  3.         if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
  4.         {
  5.                 TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
  6. //                 TIM_Cmd(TIM2, DISABLE);
  7. //                 printf("\n\r0x%08x", 0x12345678);
  8.                 Flag = 1;
  9.         }
  10. }


评论

xch
中断向量表在哪里?  发表于 2018-7-6 17:58
xuanhuanzi 发表于 2018-7-3 18:43 | 显示全部楼层
这个103的例子一堆吧,官方的开发资源包里有这个应用的例子吧
mmuuss586 发表于 2018-7-3 21:00 | 显示全部楼层
看了下程序没啥问题,你用CUBE软件生成初始化代码看看
gaoke231 发表于 2018-7-3 21:15 | 显示全部楼层
用CUBE生成一个也行,简单不用怎么调试。
gaoke231 发表于 2018-7-3 21:16 | 显示全部楼层
你也可以换成、TIM3试试,看下你的定时器配置是否有问题
hanzhen654 发表于 2018-7-3 21:25 | 显示全部楼层
换个定时器试下,我没怎么用TIM2 一般TIM3或者TIM5
香水城 发表于 2018-7-4 08:12 | 显示全部楼层
感觉进不了中断或一开中断就死机,
注意启动文件里的中断函数名跟你实际函数体是否一致,注意选正确的启动文件;
另外相关中断请求位在中断里做了清除没有;
734774645 发表于 2018-7-4 23:15 | 显示全部楼层
cubeF 1里有个TIM3的例子,不过用宏表示了,应该可以直接修改成2,你试试看
734774645 发表于 2018-7-4 23:15 | 显示全部楼层
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    TIM/TIM_TimeBase/Src/main.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V1.5.0
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    14-April-2017
  7.   * [url=home.php?mod=space&uid=247401]@brief[/url]   This sample code shows how to use STM32F1xx TIM HAL API to generate
  8.   *          a time base of one second with the corresponding Interrupt request.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

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

  40. /** @addtogroup STM32F1xx_HAL_Examples
  41.   * @{
  42.   */

  43. /** @addtogroup TIM_TimeBase
  44.   * @{
  45.   */

  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /* Private macro -------------------------------------------------------------*/
  49. /* Private variables ---------------------------------------------------------*/
  50. /* TIM handle declaration */
  51. TIM_HandleTypeDef    TimHandle;

  52. /* Prescaler declaration */
  53. uint32_t uwPrescalerValue = 0;

  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. static void Error_Handler(void);

  57. /* Private functions ---------------------------------------------------------*/

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

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

  78.   /* Configure LED3 & LED4 */
  79.   BSP_LED_Init(LED3);
  80.   BSP_LED_Init(LED4);

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

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

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

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

  104.   /* Initialize TIMx peripheral as follows:
  105.        + Period = 10000 - 1
  106.        + Prescaler = (SystemCoreClock/10000) - 1
  107.        + ClockDivision = 0
  108.        + Counter direction = Up
  109.   */
  110.   TimHandle.Init.Period            = 10000 - 1;
  111.   TimHandle.Init.Prescaler         = uwPrescalerValue;
  112.   TimHandle.Init.ClockDivision     = 0;
  113.   TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  114.   TimHandle.Init.RepetitionCounter = 0;
  115.   TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

  116.   if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
  117.   {
  118.     /* Initialization Error */
  119.     Error_Handler();
  120.   }

  121.   /*##-2- Start the TIM Base generation in interrupt mode ####################*/
  122.   /* Start Channel1 */
  123.   if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
  124.   {
  125.     /* Starting Error */
  126.     Error_Handler();
  127.   }

  128.   while (1)
  129.   {
  130.   }
  131. }

  132. /**
  133.   * @brief  Period elapsed callback in non blocking mode
  134.   * @param  htim : TIM handle
  135.   * @retval None
  136.   */
  137. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  138. {
  139.   BSP_LED_Toggle(LED3);
  140. }

  141. /**
  142.   * @brief  System Clock Configuration
  143.   *         The system Clock is configured as follow :
  144.   *            System Clock source            = PLL (HSE)
  145.   *            SYSCLK(Hz)                     = 24000000
  146.   *            HCLK(Hz)                       = 24000000
  147.   *            AHB Prescaler                  = 1
  148.   *            APB1 Prescaler                 = 1
  149.   *            APB2 Prescaler                 = 1
  150.   *            HSE Frequency(Hz)              = 8000000
  151.   *            HSE PREDIV1                    = 2
  152.   *            PLLMUL                         = 6
  153.   *            Flash Latency(WS)              = 0
  154.   * @param  None
  155.   * @retval None
  156.   */
  157. void SystemClock_Config(void)
  158. {
  159.   RCC_ClkInitTypeDef clkinitstruct = {0};
  160.   RCC_OscInitTypeDef oscinitstruct = {0};
  161.   
  162.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  163.   oscinitstruct.OscillatorType  = RCC_OSCILLATORTYPE_HSE;
  164.   oscinitstruct.HSEState        = RCC_HSE_ON;
  165.   oscinitstruct.HSEPredivValue  = RCC_HSE_PREDIV_DIV2;
  166.   oscinitstruct.PLL.PLLState    = RCC_PLL_ON;
  167.   oscinitstruct.PLL.PLLSource   = RCC_PLLSOURCE_HSE;
  168.   oscinitstruct.PLL.PLLMUL      = RCC_PLL_MUL6;
  169.   if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)
  170.   {
  171.     /* Initialization Error */
  172.     while(1);
  173.   }

  174.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  175.      clocks dividers */
  176.   clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  177.   clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  178.   clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  179.   clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
  180.   clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  181.   if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_0)!= HAL_OK)
  182.   {
  183.     /* Initialization Error */
  184.     while(1);
  185.   }
  186. }


  187. /**
  188.   * @brief  This function is executed in case of error occurrence.
  189.   * @param  None
  190.   * @retval None
  191.   */
  192. static void Error_Handler(void)
  193. {
  194.   /* Turn LED4 on */
  195.   BSP_LED_On(LED4);
  196.   while (1)
  197.   {
  198.   }
  199. }
  200. #ifdef  USE_FULL_ASSERT

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

  212.   /* Infinite loop */
  213.   while (1)
  214.   {
  215.   }
  216. }

  217. #endif

  218. /**
  219.   * @}
  220.   */

  221. /**
  222.   * @}
  223.   */

  224. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
dongnanxibei 发表于 2018-7-5 11:31 | 显示全部楼层
解决了没。例子里用的回调函数,你是直接用的中断处理,不太清楚这两个有什么关系。
小灵通2018 发表于 2018-7-5 20:01 | 显示全部楼层
能不能debug,看看死哪个循环了。
小灵通2018 发表于 2018-7-5 20:02 | 显示全部楼层
能不能debug,看看死哪个循环了。
 楼主| 江陵龙少 发表于 2018-7-6 14:48 来自手机 | 显示全部楼层
各位大大,终于找到原因了,原因在system_stm32f10x.c文件中的 #define VECT_TAB_OFFSET  0x1000。改为0就可以了,由于之前使用了bootloader,导致这里的宏定义忘了改,感谢大家的帮助,请大家吸取教训,引以为戒,现附上正确的代码图片
 楼主| 江陵龙少 发表于 2018-7-6 14:50 来自手机 | 显示全部楼层
手机上传图片太困难了
 楼主| 江陵龙少 发表于 2018-7-6 18:40 来自手机 | 显示全部楼层
关于中断,自我总结下供各位参考:1,NVIC开了对应通道中断没? 2,外设开了中断没? 3,中断服务函数写对了没? 4,向量偏移设对了没?从以上4方面考虑
木木guainv 发表于 2018-7-19 13:38 | 显示全部楼层
你一进中断就关闭timer2试试呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

85

主题

286

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部