完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
A4950电机驱动模块特性
这里我们使用TIM4的1、2、3、4通道 timer.h #ifndef __TIMER_H #define __TIMER_H #include "sys.h" void pwm_init(u16 arr,u16 psc); #endif timer.c #include "timer.h" void pwm_init(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_Initure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM4_CHxHandler;//定时器4通道x句柄 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_Initure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;//配置IO GPIO_Initure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Initure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_Initure); GPIO_Initure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9; GPIO_Init(GPIOB,&GPIO_Initure); TIM_TimeBaseStructure.TIM_Period = arr;//配置定时器 TIM_TimeBaseStructure.TIM_Prescaler =psc; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM4_CHxHandler.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2 TIM4_CHxHandler.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 TIM4_CHxHandler.TIM_OCPolarity = TIM_OCPolarity_High; //输出比较极性高 TIM_OC1Init(TIM4, &TIM4_CHxHandler); //配置pwm通道 TIM_OC2Init(TIM4, &TIM4_CHxHandler); TIM_OC3Init(TIM4, &TIM4_CHxHandler); TIM_OC4Init(TIM4, &TIM4_CHxHandler); TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);//预装载使能 TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable); TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable); TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable); TIM_Cmd(TIM4,ENABLE); //使能定时器 } motor.h #ifndef __MOTOR_H #define __MOTOR_H #include "sys.h" void motor_forth(void); void motor_back(void); void motor_left(void); void motor_right(void); void motor_stop(void); #endif motor.c #include "motor.h" #include "delay.h" //TIM_SetCompare1,2对应左边电机,TIM_SetCompare3,4对应右边电机 void motor_forth()//前行 { TIM_SetCompare1(TIM4,599); TIM_SetCompare2(TIM4,899); TIM_SetCompare3(TIM4,593); TIM_SetCompare4(TIM4,899); } void motor_back()//后退 { TIM_SetCompare1(TIM4,899); TIM_SetCompare2(TIM4,700); TIM_SetCompare3(TIM4,899); TIM_SetCompare4(TIM4,700); } void motor_left()//左转 { TIM_SetCompare1(TIM4,899); TIM_SetCompare2(TIM4,700); TIM_SetCompare3(TIM4,750); TIM_SetCompare4(TIM4,899); delay_ms(565); motor_stop(); } void motor_right()//右转 { TIM_SetCompare1(TIM4,750); TIM_SetCompare2(TIM4,899); TIM_SetCompare3(TIM4,899); TIM_SetCompare4(TIM4,700); delay_ms(593); motor_stop(); } void motor_stop() { TIM_SetCompare1(TIM4,899); TIM_SetCompare2(TIM4,899); TIM_SetCompare3(TIM4,899); TIM_SetCompare4(TIM4,899); } 下面是裸机跑的主函数————(私奔?裸奔??) main.c #include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" #include "motor.h" #include "timer.h" /************************************************************************/ int main(void) { SystemInit(); delay_init(); //延时函数初始化 nms<=1864 LED_Init(); //初始化与LED连接的硬件接口 pwm_init(899,0); motor_stop(); delay_ms(1800); delay_ms(1800); while(1) { LED=0; motor_forth(); delay_ms(1500); LED=1; motor_stop(); delay_ms(1500); LED=0; motor_left(); delay_ms(900); LED=1; motor_stop(); delay_ms(1500); LED=0; motor_forth(); delay_ms(1000); LED=1; motor_stop(); delay_ms(1500); LED=0; motor_right(); delay_ms(900); LED=1; motor_stop(); delay_ms(1500); } } |
|
|
|
只有小组成员才能发言,加入小组>>
3318 浏览 9 评论
2995 浏览 16 评论
3494 浏览 1 评论
9063 浏览 16 评论
4088 浏览 18 评论
1185浏览 3评论
609浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
601浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2337浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1897浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-25 10:37 , Processed in 1.555204 second(s), Total 81, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号