STM32CUBEIDE的使用【二】PWM

使用CUBEMX软件配置PWM IO
在这里插入图片描述
PSC配置为 80-1 因为时钟为 80Mhz
重装载 设置为 1000-1

编写代码

#include "app_motor_control.h"

extern TIM_HandleTypeDef htim2;

#define motor_tim htim2

/* @brief 电机初始化
 * @
 */
void app_motor_init(void)
{
	HAL_TIM_PWM_Start(&motor_tim, TIM_CHANNEL_1); 	//开启PWM通道1
	HAL_TIM_PWM_Start(&motor_tim, TIM_CHANNEL_2); 	//开启PWM通道2
}

/* @brief 电机运动
 * @
 */
void app_motor_run(uint8_t dir,float percentum)
{
	uint32_t index =0;

	index  = percentum /100.0f * htim2.Init.Period;
	if(dir)
	{
		__HAL_TIM_SET_COMPARE(&motor_tim,TIM_CHANNEL_1,index);
		__HAL_TIM_SET_COMPARE(&motor_tim,TIM_CHANNEL_2,0);
	}
	else
	{
		__HAL_TIM_SET_COMPARE(&motor_tim,TIM_CHANNEL_2,index);
		__HAL_TIM_SET_COMPARE(&motor_tim,TIM_CHANNEL_1,0);
	}
}

/* @brief 电机停止
 * @
 */
void app_motor_stop(void)
{
	HAL_TIM_PWM_Stop(&motor_tim, TIM_CHANNEL_1); 	//停止PWM通道1
	HAL_TIM_PWM_Stop(&motor_tim, TIM_CHANNEL_2); 	//停止PWM通道2
}

使用STM32CubeIde 编译下载
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cat_milk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值