32HAL——舵机DRV8833驱动电机
一、舵机(SG90)
50hz
角度:0 - 180°
占空比:2.5% - 12.5%
cubemx配置

72000000 / 720 / 2000 = 50;

示例程序:实现旋转编码器控制舵机
改变COUNT_MAX值 可而改变控制精度 当COUNT_MAX = 20时,编码器计数值加1,duty+10;
当COUNT_MAX = 200时,编码器计数值加1,duty+1;duty范围是50-250;
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2025 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"#include <stdio.h>#include "i2c.h"
#include "tim.h"
#include "gpio.h"
#include "oled.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes *//* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define COUNT_MAX 20
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 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_TIM1_Init();MX_TIM4_Init();MX_I2C2_Init();/* USER CODE BEGIN 2 */HAL_Delay(20);OLED_Init();HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);int counter = 0;float duty = 0;char message[50];/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){OLED_NewFrame();counter = __HAL_TIM_GET_COUNTER(&htim1);if (counter > 60000) {counter = 0;__HAL_TIM_SET_COUNTER(&htim1, 0);}else if (counter > COUNT_MAX) {counter = COUNT_MAX;__HAL_TIM_SET_COUNTER(&htim1, COUNT_MAX);}duty = (10 * counter / (float)COUNT_MAX + 2.5) / 100.0 * 2000;__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, duty);sprintf(message, "duty: %.2f", duty);OLED_PrintASCIIString(0, 0, message, &afont8x6, OLED_COLOR_NORMAL);OLED_ShowFrame();/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}二、电机


cubemx配置


示例程序:实现旋转编码器控制电机正反转以及速度调节
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2025 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"#include <stdio.h>#include "drv8833.h"
#include "i2c.h"
#include "tim.h"
#include "gpio.h"
#include "oled.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes *//* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define COUNT_MID 20 //因为编码器转一圈是40计数值 所以取中值20
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 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_I2C2_Init();MX_TIM1_Init();MX_TIM2_Init();/* USER CODE BEGIN 2 */HAL_Delay(20);OLED_Init();DRV8833_Init();HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);__HAL_TIM_SET_COUNTER(&htim1, COUNT_MID);int counter = 0;int speed = 0;char message[50];/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){OLED_NewFrame();counter = __HAL_TIM_GET_COUNTER(&htim1);if (counter > 60000) {counter = 0;__HAL_TIM_SET_COUNTER(&htim1, 0);}else if (counter > COUNT_MID * 2) {counter = COUNT_MID * 2;__HAL_TIM_SET_COUNTER(&htim1, COUNT_MID * 2);}//反转 离20越远速度越快if (counter < COUNT_MID) {speed = (COUNT_MID - counter) * 100 / COUNT_MID;DRV8833_Backward(speed);}else if (counter > COUNT_MID) {speed = (counter - COUNT_MID) * 100 / COUNT_MID;DRV8833_Forward(speed);}sprintf(message, "speed: %d", speed);OLED_PrintASCIIString(0, 0, message, &afont8x6, OLED_COLOR_NORMAL);OLED_ShowFrame();/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
注:上述PPT均来自keysking up
