找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2413|回复: 0
打印 上一主题 下一主题
收起左侧

抛弃Timer定时器的STM32的呼吸灯程序

[复制链接]
跳转到指定楼层
楼主
ID:544243 发表于 2019-5-21 22:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
抛弃Timer定时器,用延时时间来控制呼吸灯的闪烁时间

单片机源程序如下:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2019 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */

  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "gpio.h"

  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */

  25. /* USER CODE END Includes */

  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */

  28. /* USER CODE END PTD */

  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */

  31. /* USER CODE END PD */

  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */

  34. /* USER CODE END PM */

  35. /* Private variables ---------------------------------------------------------*/

  36. /* USER CODE BEGIN PV */

  37. /* USER CODE END PV */

  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. /* USER CODE BEGIN PFP */

  41. /* USER CODE END PFP */

  42. /* Private user code ---------------------------------------------------------*/
  43. /* USER CODE BEGIN 0 */

  44. void delay0(unsigned int n)
  45. {
  46.         unsigned int x;
  47.         while(n--)
  48.         {
  49.                 x = 50;
  50.                 while(x--);
  51.                 }
  52. }

  53. void twinkle_once(unsigned char darktime)
  54. {
  55.         HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
  56.         delay0(100-darktime);
  57.         HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
  58.         delay0(darktime);
  59. }
  60.        
  61. /* USER CODE END 0 */

  62. /**
  63.   * @brief  The application entry point.
  64.   * @retval int
  65.   */
  66. int main(void)
  67. {
  68.   /* USER CODE BEGIN 1 */

  69.   /* USER CODE END 1 */

  70.   /* MCU Configuration--------------------------------------------------------*/

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

  73.   /* USER CODE BEGIN Init */

  74.   /* USER CODE END Init */

  75.   /* Configure the system clock */
  76.   SystemClock_Config();

  77.   /* USER CODE BEGIN SysInit */

  78.   /* USER CODE END SysInit */

  79.   /* Initialize all configured peripherals */
  80.   MX_GPIO_Init();
  81.   /* USER CODE BEGIN 2 */

  82.   /* USER CODE END 2 */

  83.   /* Infinite loop */
  84.   /* USER CODE BEGIN WHILE */
  85.         //int x = 0;
  86.         char darktime = 0,dir = 1;
  87.         unsigned int cnt = 0;
  88.   while (1)
  89.   {
  90.                 cnt ++;
  91.                 twinkle_once(darktime);
  92.                 if(cnt == 4)
  93.                 {
  94.                         cnt = 0;
  95.                         if(dir)
  96.                                 darktime += 1;
  97.                         else
  98.                                 darktime -= 1;
  99.                        
  100.                         if(darktime >= 100)
  101.                                 dir = 1;
  102.                         else
  103.                                 dir = 0;
  104.                 }
  105.                 /*x = HAL_GPIO_ReadPin(BUTTON_GPIO_Port,BUTTON_Pin);
  106.                 if(x == 0)
  107.                         HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
  108.                 else
  109.                         HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);*/
  110.     /* USER CODE END WHILE */

  111.     /* USER CODE BEGIN 3 */
  112.   }
  113.   /* USER CODE END 3 */
  114. }

  115. /**
  116.   * @brief System Clock Configuration
  117.   * @retval None
  118.   */
  119. void SystemClock_Config(void)
  120. {
  121.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  122.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  123.   /** Initializes the CPU, AHB and APB busses clocks
  124.   */
  125.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  126.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  127.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  128.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  129.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  130.   {
  131.     Error_Handler();
  132.   }
  133.   /** Initializes the CPU, AHB and APB busses clocks
  134.   */
  135.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  136.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  137.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  138.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  139.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  140.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  141.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  142.   {
  143.     Error_Handler();
  144.   }
  145. }

  146. /* USER CODE BEGIN 4 */

  147. /* USER CODE END 4 */

  148. /**
  149.   * @brief  This function is executed in case of error occurrence.
  150.   * @retval None
  151.   */
  152. void Error_Handler(void)
  153. {
  154.   /* USER CODE BEGIN Error_Handler_Debug */
  155.   /* User can add his own implementation to report the HAL error return state */

  156.   /* USER CODE END Error_Handler_Debug */
  157. }

  158. #ifdef  USE_FULL_ASSERT
  159. /**
  160.   * @brief  Reports the name of the source file and the source line number
  161.   *         where the assert_param error has occurred.
  162.   * @param  file: pointer to the source file name
  163.   * @param  line: assert_param error line source number
  164.   * @retval None
  165.   */
  166. void assert_failed(uint8_t *file, uint32_t line)
  167. {
  168.   /* USER CODE BEGIN 6 */
  169.   /* User can add his own implementation to report the file name and line number,
  170.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  171.   /* USER CODE END 6 */
  172. }
  173. #endif /* USE_FULL_ASSERT */

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

所有资料51hei提供下载:
TEST382.7z (2.89 MB, 下载次数: 9)


评分

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

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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