一、简介
关于STM32WBA55CG开发板想必大家都比较熟悉了,开发板由核心板+扩展底板组成,支持多种无线协议。
开发板的特征如下:
二、开箱与环境搭建
拿到板子后,咱们将核心板与扩展底板拆开来看看。
板子布局设计紧凑,扩展出的引脚也比较多,可尽可能得满足客户外设的需求。这样布局设计可有效防止核心板与扩展底板管脚位对接发生错误,这点是非常值得肯定的。
由于板上支持STLINK-V3调试,因此无需外接其它下载调试器。关于开发环境搭建,首先我们通过链接:STM32CubeWBA获得关于该板卡的软件开发工具包,如果采用之前旧版本的STM32CubeMX可能不支持STM32WBA55CG,需要重新下载安装。笔者这里使用的CubeMX版本是V6.13.0,软件工程包可手动下载“en.stm32cubewba-v1-5-0.zip”,当然使用STM32CubeMX设计时,联网后工具会后台自动帮我们下载关于该板卡的Repository数据包,默认保存到C盘个人名下的STM32Cube文件夹下。
笔者习惯于使用MDK开发,因此需要安装该板基于MDK的板间支持包,用户可通过链接:STM32WBAxx_DFP获取到v1.3.0版本的支持包,手动傻瓜式安装即可,这里就不再赘述。
然后参考STM32Cube_FW_WBA_V1.5.0工程包中的Examples,加入点灯代码,编译,轻松实现闪灯功能。
三、工具配置
打开STM32CubeMX工具后,通过选择以官方板卡为创建入口,开始工程的创建。
选择不带TrustZone技术功能的工程,创建全新干净的工程。
然后我们将PA6\PA7\PA8\PB12做为四路PWM输出接口,选择TIM2,并设置好相对应的管脚配置。
四、代码编辑
将上述导出的MDK工程采用Keil V5.38.0打开,稍加一些逻辑处理代码,实现四路PWM呼吸灯效输出占空比。
main.c
/* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /******************************************************************************* * Prototypes ******************************************************************************/ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ TIM_HandleTypeDef htim2; /* USER CODE BEGIN PV */ uint8_t dutyCycle = 0; uint8_t var = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_ICACHE_Init(void); static void MX_TIM2_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void Pwm_Change(void) { if(var == 0) { dutyCycle+=10; } else if(var == 1) { dutyCycle-=10; } __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1, dutyCycle); __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2, dutyCycle); __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, dutyCycle); __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4, dutyCycle); HAL_Delay(40); if(dutyCycle >= 100) var = 1; else if(dutyCycle == 0) var = 0; } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ICACHE_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ /* Start channel 1 */ if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1) != HAL_OK) { /* PWM Generation Error */ Error_Handler(); } /* Start channel 2 */ if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2) != HAL_OK) { /* PWM Generation Error */ Error_Handler(); } /* Start channel 3 */ if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3) != HAL_OK) { /* PWM generation Error */ Error_Handler(); } /* Start channel 4 */ if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4) != HAL_OK) { /* PWM generation Error */ Error_Handler(); } /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ Pwm_Change(); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Supply configuration update enable */ HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY); /** Configure the main internal regulator output voltage */ if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEDiv = RCC_HSE_DIV1; RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL1.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL1.PLLM = 4; RCC_OscInitStruct.PLL1.PLLN = 25; RCC_OscInitStruct.PLL1.PLLP = 2; RCC_OscInitStruct.PLL1.PLLQ = 2; RCC_OscInitStruct.PLL1.PLLR = 2; RCC_OscInitStruct.PLL1.PLLFractional = 0; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2 |RCC_CLOCKTYPE_PCLK7|RCC_CLOCKTYPE_HCLK5; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB7CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.AHB5_PLL1_CLKDivider = RCC_SYSCLK_PLL1_DIV4; RCC_ClkInitStruct.AHB5_HSEHSI_CLKDivider = RCC_SYSCLK_HSEHSI_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { Error_Handler(); } } /** * @brief ICACHE Initialization Function * @param None * @retval None */ static void MX_ICACHE_Init(void) { /* USER CODE BEGIN ICACHE_Init 0 */ /* USER CODE END ICACHE_Init 0 */ /* USER CODE BEGIN ICACHE_Init 1 */ /* USER CODE END ICACHE_Init 1 */ /** Enable instruction cache in 1-way (direct mapped cache) */ if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK) { Error_Handler(); } if (HAL_ICACHE_Enable() != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN ICACHE_Init 2 */ /* USER CODE END ICACHE_Init 2 */ } /** * @brief TIM2 Initialization Function * @param None * @retval None */ static void MX_TIM2_Init(void) { /* USER CODE BEGIN TIM2_Init 0 */ /* USER CODE END TIM2_Init 0 */ TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; /* USER CODE BEGIN TIM2_Init 1 */ /* USER CODE END TIM2_Init 1 */ htim2.Instance = TIM2; htim2.Init.Prescaler = Prescaler_Value; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 1000-1; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_PWM_Init(&htim2) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) { Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 50; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } sConfigOC.Pulse = 50; if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } sConfigOC.Pulse = 50; if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK) { Error_Handler(); } sConfigOC.Pulse = 50; if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM2_Init 2 */ /* USER CODE END TIM2_Init 2 */ HAL_TIM_MspPostInit(&htim2); } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #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 CODE BEGIN 6 */ /* 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) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */
五、硬件连线
根据用户手册,板上的IO管脚分布图可知这些管脚的位置。
正如上图所示,这里的外接LED正极管脚连接PA7,PA6连接L298N模块的IN1输入通道,PB12连接L298N模块的IN4输入通道,将编译后的程序下载到开发板中后运行,可观察到红色指示灯以呼吸灯模式点亮,而L298N电机模块的两组直流电机转速则截然不同,方向相反,转速递增递减也不尽相同。
六、实验效果
直流电机与指示灯呈现效果见如下视频。目前还在琢磨为啥两组电机输出的转速不一致问题?需要排查电机硬件结构因素,两个电机均支持12V供电,但具体型号、轴心不一致。电机转动的噪声与呼吸灯亮度变化同步。