找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6420|回复: 1
收起左侧

用HAL库写的STM32F103C8T6的定时器控制LED灯1秒闪烁程序

[复制链接]
ID:438796 发表于 2019-3-19 13:52 | 显示全部楼层 |阅读模式
这里用到了STM32CubeMx,用定时器控制LED灯1秒闪烁。
$DD4B~WQ[J2WB])P%AXY1VH.png HFK]2YLGE5B}A[Z_KIN)}SD.png

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f1xx_hal.h"

  4. /* USER CODE BEGIN Includes */

  5. /* USER CODE END Includes */

  6. /* Private variables ---------------------------------------------------------*/
  7. TIM_HandleTypeDef htim1;

  8. /* USER CODE BEGIN PV */
  9. /* Private variables ---------------------------------------------------------*/

  10. /* USER CODE END PV */

  11. /* Private function prototypes -----------------------------------------------*/
  12. void SystemClock_Config(void);
  13. static void MX_GPIO_Init(void);
  14. static void MX_TIM1_Init(void);

  15. /* USER CODE BEGIN PFP */
  16. /* Private function prototypes -----------------------------------------------*/

  17. /* USER CODE END PFP */

  18. /* USER CODE BEGIN 0 */

  19. /* USER CODE END 0 */

  20. /**
  21.   * @brief  The application entry point.
  22.   *
  23.   * @retval None
  24.   */
  25.         
  26.         //定时器中断
  27. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  28. {
  29.         if (htim->Instance == TIM1)
  30.         {
  31.                         /* Toggle LED */
  32.                         HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);//定时时间到后,LED1翻转
  33.         }
  34. }
  35.       

  36. int main(void)
  37. {
  38.   /* USER CODE BEGIN 1 */

  39.   /* USER CODE END 1 */

  40.   /* MCU Configuration----------------------------------------------------------*/

  41.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  42.   HAL_Init();

  43.   /* USER CODE BEGIN Init */

  44.   /* USER CODE END Init */

  45.   /* Configure the system clock */
  46.   SystemClock_Config();

  47.   /* USER CODE BEGIN SysInit */

  48.   /* USER CODE END SysInit */

  49.   /* Initialize all configured peripherals */
  50.   MX_GPIO_Init();
  51.   MX_TIM1_Init();
  52.         
  53.         HAL_TIM_Base_Start_IT(&htim1);
  54.   /* USER CODE BEGIN 2 */
  55.         
  56.   /* USER CODE END 2 */

  57.   /* Infinite loop */
  58.   /* USER CODE BEGIN WHILE */
  59.   while (1)
  60.   {               
  61. //                HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
  62. //                HAL_Delay(500);
  63. //                HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
  64. //                HAL_Delay(1000);
  65.   /* USER CODE END WHILE */

  66.   /* USER CODE BEGIN 3 */

  67.   }
  68.   /* USER CODE END 3 */

  69. }

  70. /**
  71.   * @brief System Clock Configuration
  72.   * @retval None
  73.   */
  74. void SystemClock_Config(void)
  75. {

  76.   RCC_OscInitTypeDef RCC_OscInitStruct;
  77.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  78.     /**Initializes the CPU, AHB and APB busses clocks
  79.     */
  80.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  81.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  82.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  83.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  84.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  85.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  86.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  87.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  88.   {
  89.     _Error_Handler(__FILE__, __LINE__);
  90.   }

  91.     /**Initializes the CPU, AHB and APB busses clocks
  92.     */
  93.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  94.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  95.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  96.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  97.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  98.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  99.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  100.   {
  101.     _Error_Handler(__FILE__, __LINE__);
  102.   }

  103.     /**Configure the Systick interrupt time
  104.     */
  105.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  106.     /**Configure the Systick
  107.     */
  108.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  109.   /* SysTick_IRQn interrupt configuration */
  110.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  111. }

  112. /* TIM1 init function */
  113. static void MX_TIM1_Init(void)
  114. {

  115.   TIM_ClockConfigTypeDef sClockSourceConfig;
  116.   TIM_MasterConfigTypeDef sMasterConfig;

  117.   htim1.Instance = TIM1;
  118.   htim1.Init.Prescaler = 36000-1;                        //预分频系数  APB2当前频率为72MHz,我们希望每秒钟发生1次中断
  119.   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  120.   htim1.Init.Period = 2000-1;                                                //自动重载值为1000-1    72,000,000/36000/2000=1Hz
  121.   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  122.   htim1.Init.RepetitionCounter = 0;
  123.   htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  124.   if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  125.   {
  126.     _Error_Handler(__FILE__, __LINE__);
  127.   }
  128.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  129.   if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  130.   {
  131.     _Error_Handler(__FILE__, __LINE__);
  132.   }
  133.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  134.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  135.   if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  136.   {
  137.     _Error_Handler(__FILE__, __LINE__);
  138.   }

  139. }

  140. /** Configure pins as
  141.         * Analog
  142.         * Input
  143.         * Output
  144.         * EVENT_OUT
  145.         * EXTI
  146. */
  147. static void MX_GPIO_Init(void)
  148. {

  149.   GPIO_InitTypeDef GPIO_InitStruct;

  150.   /* GPIO Ports Clock Enable */
  151.   __HAL_RCC_GPIOC_CLK_ENABLE();
  152.   __HAL_RCC_GPIOD_CLK_ENABLE();

  153.   /*Configure GPIO pin Output Level */
  154.   HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);

  155.   /*Configure GPIO pin : LED1_Pin */
  156.   GPIO_InitStruct.Pin = LED1_Pin;                                                                        //选择对应的LED管脚
  157.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;                        //输出模式
  158.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  159.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;        
  160.   HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);

  161. }

  162. /* USER CODE BEGIN 4 */

  163. /* USER CODE END 4 */

  164. /**
  165.   * @brief  This function is executed in case of error occurrence.
  166.   * @param  file: The file name as string.
  167.   * @param  line: The line in file as a number.
  168.   * @retval None
  169.   */
  170. void _Error_Handler(char *file, int line)
  171. {
  172.   /* USER CODE BEGIN Error_Handler_Debug */
  173.   /* User can add his own implementation to report the HAL error return state */
  174.   while(1)
  175.   {
  176.   }
  177.   /* USER CODE END Error_Handler_Debug */
  178. }

  179. #ifdef  USE_FULL_ASSERT
  180. /**
  181.   * @brief  Reports the name of the source file and the source line number
  182.   *         where the assert_param error has occurred.
  183.   * @param  file: pointer to the source file name
  184.   * @param  line: assert_param error line source number
  185.   * @retval None
  186.   */
  187. void assert_failed(uint8_t* file, uint32_t line)
  188. {
  189.   /* USER CODE BEGIN 6 */
  190.   /* User can add his own implementation to report the file name and line number,
  191.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  192.   /* USER CODE END 6 */
  193. }
  194. #endif /* USE_FULL_ASSERT */

  195. /**
  196.   * @}
  197.   */

  198. /**
  199.   * @}
  200.   */

  201. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
HAL库的LED.7z (2.89 MB, 下载次数: 88)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:421823 发表于 2019-3-19 17:18 | 显示全部楼层
STM32CubeMX是个好东西
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表