MCU选择的是stm32F103RBT6,步进电机选择4相5线5V步进电机马达减速电机28BYJ-48-5V,驱动选择的是L298N模块,模块供电选择12V供电(5V可能带不起来),模块的OUT1、OUT2接小型步进电机的一个线圈,OUT3、OUT4接另一个线圈,注意L298N模块的GND和MCU的GND相连。*******************************************************************************步进电机头文件Stepmotor.h#ifndef__MOTOR_H#define__MOTOR_HvoidMotor_Init(void);voidMotor_Go(void);#endif源文件Stepmotor.c#includestepmotor.h#includesys.hvoidMotor_Init(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能PB端口时钟GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8;//PB3接IN1PB5接IN2PB7接IN3PB8接IN4GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度为50MHzGPIO_Init(GPIOB,&GPIO_InitStructure);//GPIO_ResetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8);*******************************************************************************PWM调制头文件mypwm.h#ifndef__TIMER_H#define__TIMER_H#includesys.hvoidMYTIMER3_Init(u16arr,u16psc);voidTIM3_PWM_Init(u16arr,u16psc);#endif源文件mypwm.c#includemypwm.h#includestm32f10x.hvoidTIM3_PWM_Init(u16arr,u16psc){TIM_OCInitTypeDefTIM_OCInitTypestucture;TIM_TimeBaseInitTypeDefTIM_TimeBaseInitstucture;GPIO_InitTypeDefGPIO_InitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能定时器时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE);//使能PC端口,复用时钟GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;//PC6接ENAPC7接ENBGPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//推挽复用输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度为50MHzGPIO_Init(GPIOC,&GPIO_InitStructure);//GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);//TIM_TimeBaseInitstucture.TIM_Period=arr;//自动装载值TIM_TimeBaseInitstucture.TIM_Prescaler=psc;//预分频值TIM_TimeBaseInitstucture.TIM_CounterMode=TIM_CounterMode_Up;//计数模式向上TIM_TimeBaseInitstucture.TIM_ClockDivision=0;//TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitstucture);TIM_OCInitTypestucture.TIM_OCMode=TIM_OCMode_PWM1;//模式1TIM_OCInitTypestucture.TIM_OCPolarity=TIM_OCPolarity_High;//高电平TIM_OCInitTypestucture.TIM_Pulse=599;//改变数值调节占空比TIM_OCInitTypestucture.TIM_OutputState=TIM_OutputState_Enable;//使能TIM_OC1Init(TIM3,&TIM_OCInitTypestucture);TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能预装载TIM_OCInitTypestucture.TIM_OCMode=TIM_OCMode_PWM1;//模式1TIM_OCInitTypestucture.TIM_OCPolarity=TIM_OCPolarity_High;//高电平TIM_OCInitTypestucture.TIM_Pulse=599;TIM_OCInitTypestucture.TIM_OutputState=TIM_OutputState_Enable;//使能TIM_OC2Init(TIM3,&TIM_OCInitTypestucture);TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能预装载TIM_Cmd(TIM3,ENABLE);//使能定时器}*******************************************************************************主函数#includestepmotor.h#includestm32f10x.h#includedelay.h#includetimer.hintmain(){delay_init();Motor_Init();TIM3_PWM_Init(999,71);//72M/(71+1)(999+1)=1Khzwhile(1)//正转{GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8);//0111GPIO_ResetBits(GPIOB,GPIO_Pin_3);delay_ms(4);GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_7|GPIO_Pin_8);//1011GPIO_ResetBits(GPIOB,GPIO_Pin_5);delay_ms(4);GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_8);//1101GPIO_ResetBits(GPIOB,GPIO_Pin_7);delay_ms(4);GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_7);//1110GPIO_ResetBits(GPIOB,GPIO_Pin_8);delay_ms(4);}}