找回密码
 注册

QQ登录

只需一步,快速开始

搜索

stm32采用Cube配置基于HAL库的蜂鸣器调试程序

[复制链接]
路漫漫 发表于 2020-5-29 02:00:15 | 显示全部楼层 |阅读模式
在Cube中配置正点原子的DS0 、DS1和蜂鸣器的脚位,配置如附件所示,配置自行开发的程序指示灯,这样,无论是在正点原子的开发板,还是在自行开发的嵌入式主板中,都能够观察到正常的程序运行指示灯的闪烁和蜂鸣器的定时鸣叫。其中时钟配置如附件所示截图,GPIO的配置如附件截图所示,在这个4.23.0版本的Cube程序运行正常, 没有BUG。 完整源码DS01.7z (743.08 KB, 售价: 1 E币)
081826fphkt1huhei5zv7i.png 082327mimmai4zsmbfuh4x.png 082334i3ssrmzzmmavpvmw.png
部分源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f4xx_hal.h"
  4. #include "gpio.h"

  5. /* USER CODE BEGIN Includes */

  6. /* USER CODE END Includes */

  7. /* Private variables ---------------------------------------------------------*/

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

  10. /* USER CODE END PV */

  11. /* Private function prototypes -----------------------------------------------*/
  12. void SystemClock_Config(void);

  13. /* USER CODE BEGIN PFP */
  14. /* Private function prototypes -----------------------------------------------*/

  15. /* USER CODE END PFP */

  16. /* USER CODE BEGIN 0 */

  17. /* USER CODE END 0 */

  18. int main(void)
  19. {

  20.   /* USER CODE BEGIN 1 */

  21.   /* USER CODE END 1 */

  22.   /* MCU Configuration----------------------------------------------------------*/

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

  25.   /* USER CODE BEGIN Init */

  26.   /* USER CODE END Init */

  27.   /* Configure the system clock */
  28.   SystemClock_Config();

  29.   /* USER CODE BEGIN SysInit */

  30.   /* USER CODE END SysInit */

  31.   /* Initialize all configured peripherals */
  32.   MX_GPIO_Init();

  33.   /* USER CODE BEGIN 2 */

  34.   /* USER CODE END 2 */

  35.   /* Infinite loop */
  36.   /* USER CODE BEGIN WHILE */
  37.   while (1)
  38.   {
  39.   /* USER CODE END WHILE */

  40.   /* USER CODE BEGIN 3 */
  41.                         HAL_Delay(500);
  42.   /* yangji */      
  43.                                 HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_12);//led out
  44. //                                HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_13);//BEEP
  45.         /* kaifaban */      
  46.       HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_8);//bee
  47.                         HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_9);//DS0
  48.             HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_10);//DS1

  49.   }
  50.   /* USER CODE END 3 */

  51. }

  52. /** System Clock Configuration
  53. */
  54. void SystemClock_Config(void)
  55. {

  56.   RCC_OscInitTypeDef RCC_OscInitStruct;
  57.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  58.     /**Configure the main internal regulator output voltage
  59.     */
  60.   __HAL_RCC_PWR_CLK_ENABLE();

  61.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  62.     /**Initializes the CPU, AHB and APB busses clocks
  63.     */
  64.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  65.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  66.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  67.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  68.   RCC_OscInitStruct.PLL.PLLM = 4;
  69.   RCC_OscInitStruct.PLL.PLLN = 168;
  70.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  71.   RCC_OscInitStruct.PLL.PLLQ = 4;
  72.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  73.   {
  74.     _Error_Handler(__FILE__, __LINE__);
  75.   }

  76.     /**Initializes the CPU, AHB and APB busses clocks
  77.     */
  78.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  79.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  80.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  81.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  82.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  83.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  84.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  85.   {
  86.     _Error_Handler(__FILE__, __LINE__);
  87.   }

  88.     /**Enables the Clock Security System
  89.     */
  90.   HAL_RCC_EnableCSS();

  91.     /**Configure the Systick interrupt time
  92.     */
  93.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  94.     /**Configure the Systick
  95.     */
  96.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  97.   /* SysTick_IRQn interrupt configuration */
  98.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  99. }

  100. /* USER CODE BEGIN 4 */

  101. /* USER CODE END 4 */

  102. /**
  103.   * @brief  This function is executed in case of error occurrence.
  104.   * @param  None
  105.   * @retval None
  106.   */
  107. void _Error_Handler(char * file, int line)
  108. {
  109.   /* USER CODE BEGIN Error_Handler_Debug */
  110.   /* User can add his own implementation to report the HAL error return state */
  111.   while(1)
  112.   {
  113.   }
  114.   /* USER CODE END Error_Handler_Debug */
  115. }

  116. #ifdef USE_FULL_ASSERT

  117. /**
  118.    * @brief Reports the name of the source file and the source line number
  119.    * where the assert_param error has occurred.
  120.    * @param file: pointer to the source file name
  121.    * @param line: assert_param error line source number
  122.    * @retval None
  123.    */
  124. void assert_failed(uint8_t* file, uint32_t line)
  125. {
  126.   /* USER CODE BEGIN 6 */
  127.   /* User can add his own implementation to report the file name and line number,
  128.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  129.   /* USER CODE END 6 */

  130. }

  131. #endif
复制代码


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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-12-23 08:10

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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