1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
一、前期准备
单片机:STM32F407VET6 开发环境:MDK5.14 库函数:STM32Cube_FW_F4_V1.16.0 二、实验效果 1)按键KEY1按下,正转->反转->停止,循环下去; 2)按键KEY2按下,PWM增加5%,一直加到100%; 3)按键KEY3按下,PWM减小5%,一直减到0%。 4)PWM波为20K,正转启动要到30%,反转启动要到50%, 三、驱动原理 直流有刷电机的驱动十分简单,通电即可转动。运用H桥可以直接驱动电机正反转。 当Q1、Q4导通,电机正转;Q2、Q3导通电机反转。驱动电路使用淘宝上的H桥模块 逻辑输入的IN1、IN2为OUT1与OUT2的控制脚。IN1、IN2电平相反时候,电机实现正反转;IN1、IN2电平相同时,电机停转。 CubeMX TIM2配置如下: 四、驱动代码 motor.h #ifndef __MOTOR_H__ #define __MOTOR_H__ #include "stm32f4xx_hal.h" #include "user_gpio.h" #include "stdio.h" #include "string.h" #include "stdlib.h" #define KEY1 PEin(10) #define KEY2 PEin(11) #define KEY3 PEin(12) extern uint8_t PWMCapture1,PWMCapture2; extern uint8_t MoterMode; extern void Moter_StartPWM(void); extern void Motor_Test(void); #endif motor.c #include "motor.h" extern TIM_HandleTypeDef htim2; uint8_t PWMCapture1 = 30, PWMCapture2 = 30; uint8_t MoterMode = 0; void Moter_StartPWM(void) { HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 100); __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 100); } void Motor_Test(void) { if (KEY1 == 0) //正转 { HAL_Delay(5); if (KEY1 == 0) { while(KEY1 == 0); MoterMode ++; } } switch(MoterMode) { case 1: //正转 PWMCapture2 = 0; __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, PWMCapture2); if (KEY2 == 0) { HAL_Delay(5); if (KEY2 == 0) { while(KEY2 == 0); PWMCapture1 += 5; if (PWMCapture1 >= 100) //Duty Cycle Max = 100% { PWMCapture1 = 100; } printf("CH1 Duty Cycle = %02d%%rn", PWMCapture1); } } if (KEY3 == 0) { HAL_Delay(5); if (KEY3 == 0) { while(KEY3 == 0); PWMCapture1 -= 5; if (PWMCapture1 <= 5) //Duty Cycle Min = 5% { PWMCapture1 = 5; } printf("CH1 Duty Cycle = %02d%%rn", PWMCapture1); } } __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, PWMCapture1); break; case 2: //反转 PWMCapture1 = 0; __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, PWMCapture2); if (KEY2 == 0) { HAL_Delay(5); if (KEY2 == 0) { while(KEY2 == 0); PWMCapture2 += 5; if (PWMCapture2 >= 100) //Duty Cycle Max = 100% { PWMCapture2 = 100; } printf("CH2 Duty Cycle = %02d%%rn", PWMCapture2); } } if (KEY3 == 0) { HAL_Delay(5); if (KEY3 == 0) { while(KEY3 == 0); PWMCapture2 -= 5; if (PWMCapture2 <= 5) //Duty Cycle Min = 5% { PWMCapture2 = 5; } printf("CH2 Duty Cycle = %02d%%rn", PWMCapture2); } } __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, PWMCapture2); break; default: MoterMode = 0; break; } } |
|
|
|
只有小组成员才能发言,加入小组>>
2435 浏览 0 评论
9100 浏览 4 评论
36773 浏览 19 评论
5026 浏览 0 评论
24745 浏览 34 评论
1528浏览 2评论
1743浏览 1评论
2193浏览 1评论
1553浏览 0评论
524浏览 0评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 19:19 , Processed in 1.378302 second(s), Total 76, Slave 60 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号