stm32f407 hal库定时器3PWM驱动sg90舵机
时间: 2023-07-26 14:17:06 浏览: 196
STM32定时器PWM控制舵机程序
好的,针对你的问题,我可以给出以下步骤:
1. 初始化定时器3,使用HAL库函数`HAL_TIM_PWM_Init()`进行初始化,设置PWM模式为模式1,周期为20ms。
2. 配置定时器3的PWM输出通道,使用HAL库函数`HAL_TIM_PWM_ConfigChannel()`进行配置,设置通道号为TIM_CHANNEL_1、TIM_CHANNEL_2、TIM_CHANNEL_3,设置占空比为0。
3. 使能定时器3的PWM输出通道,使用HAL库函数`HAL_TIM_PWM_Start()`进行使能,这样PWM输出才能正常工作。
4. 控制sg90舵机的转动,由于sg90舵机的控制信号是一个周期为20ms的PWM信号,占空比在0.5ms到2.5ms之间,对应的脉宽为500us到2500us。因此,我们可以通过改变定时器3的PWM输出占空比来控制sg90舵机的转动。
下面是示例代码,可以参考一下:
```c
#include "main.h"
#include "stm32f4xx_hal.h"
TIM_HandleTypeDef htim3;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM3_Init();
while (1)
{
// 控制sg90舵机转到0度
TIM3->CCR1 = 500; // 0.5ms
TIM3->CCR2 = 500; // 0.5ms
TIM3->CCR3 = 500; // 0.5ms
HAL_Delay(1000);
// 控制sg90舵机转到90度
TIM3->CCR1 = 1500; // 1.5ms
TIM3->CCR2 = 1500; // 1.5ms
TIM3->CCR3 = 1500; // 1.5ms
HAL_Delay(1000);
// 控制sg90舵机转到180度
TIM3->CCR1 = 2500; // 2.5ms
TIM3->CCR2 = 2500; // 2.5ms
TIM3->CCR3 = 2500; // 2.5ms
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/**
* Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**
* Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**
* Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_TIM3_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 83;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 19999;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim3);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8, GPIO_PIN_RESET);
/*Configure GPIO pins : PA6 PA7 PA8 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
```
在上述代码中,我们使用了定时器3的通道1、通道2、通道3来控制三个sg90舵机的转动,具体的占空比设置可以根据实际情况进行调整。另外,为了使定时器3能够输出PWM信号,我们还需要将PA6、PA7、PA8这三个引脚配置为复用功能,并设置复用功能为TIM3的PWM输出。
阅读全文