快捷搜索: 王者荣耀 脱发

STM32实现电机的正反转

2019年7月22日 今天是做小车过程中的第二天,今天写一下单纯的实现电机的正反转。(但是还是不会实现PWM调速)代码如下: /***********************************************/ .H 代码 #ifndef __MOTOR1_H #define __MOTOR1_H #include “stm32f10x.h” //#include “stm32f10x_gpio.h” #define High 1 //宏定义 高为1 #define Low 0 //宏定义 低为0 #define IN1(a) if (a) GPIO_SetBits(GPIOA,GPIO_Pin_4); //如果真 IN1 为高电平(PA4) else GPIO_ResetBits(GPIOA,GPIO_Pin_4) // 否则为低电平 (PA4) #define IN2(a) if (a) GPIO_SetBits(GPIOA,GPIO_Pin_5); else GPIO_ResetBits(GPIOA,GPIO_Pin_5) otor_1_Config(void); void Motor_1_STOP(void); //停止 void Motor_1_PRun(void); //正转 void Motor_1_NRun(void); //反转 #endif 下面是.c代码 #include “motor.h” #include “stm32f10x.h” #include “delay.h” void Motor_1_Config(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//用GPIOA时钟 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出 GPIO_InitStruct.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_ResetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5); } void Motor_1_STOP(void) { IN1(High); IN2(High); } void Motor_1_PRun(void) { IN1(Low); IN2(High); } void Motor_1_NRun(void) { IN1(High); IN2(Low); } 下面是主函数 int main(void) { Motor_1_Config(); delay_init(); while(1) { Motor_1_PRun(); //正转 delay_ms(2000); //延迟2秒 Motor_1_NRun(); //反转 delay_ms(2000); //延迟2秒 Motor_1_STOP(); //停止 delay_ms(2000); } }

经验分享 程序员 微信小程序 职场和发展