-
stm32来按键控制步进电机,发现电机一得电就一直转
我使用stm32来按键控制步进电机,本意是想按一次键,电机就整步n次,但是一按键,这个角度就一直转,变化幅度挺慢的,观察角度变化,有种在零点几反复跳的感觉,但是整体是增加的。以下是主函数和步进电机的封装函数###NextPage[title=主函数]###u8KeyNum;intmain(){KEY_Init();KEY_GetNum();Motor_Init();while(1){KeyNum=KEY_GetNum();if(KEY_GetNum()==8){Motor_RotateByStep(Foreward,1000,5);Motor_Stop();}}###NextPage[title=步进电机]###uint8_tSTEP;//用于存储电机正在走过的整步编号voidMotor_Init(void){//选择PB7,PB6,PB5,PB4分别为相A,B,C,D的输出GPIO_InitTypeDefGPIO_InitStruct;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStruct.GPIO_Pin=Motor_A1|Motor_B1|Motor_C1|Motor_D1;GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStruct);GPIO_ResetBits(GPIOB,Motor_A1|Motor_B1|Motor_C1|Motor_D1);}voidMotor_SingleStep(uint8_tStepNum,uint16_tDelay_Time_xms){switch(StepNum){case0://AGPIO_WriteBit(GPIOB,Motor_A1,Bit_SET);GPIO_WriteBit(GPIOB,Motor_B1|Motor_C1|Motor_D1,Bit_RESET);break;case1://BGPIO_WriteBit(GPIOB,Motor_B1,Bit_SET);GPIO_WriteBit(GPIOB,Motor_A1|Motor_C1|Motor_D1,Bit_RESET);break;case2://CGPIO_WriteBit(GPIOB,Motor_C1,Bit_SET);GPIO_WriteBit(GPIOB,Motor_A1|Motor_B1|Motor_D1,Bit_RESET);break;case3://DGPIO_WriteBit(GPIOB,Motor_D1,Bit_SET);GPIO_WriteBit(GPIOB,Motor_A1|Motor_B1|Motor_C1,Bit_RESET);break;default:break;}delay_ms(Delay_Time_xms);//延时,控制电机速度//Motor_Stop();//断电,防止电机过热}voidMotor_RotateByStep(RotDirectiondirection,uint32_tstep,uint16_tDelay_Time_xms){for(uint32_ti=0;i<step;i++){if(direction==Foreward)//电
2025-01-14 0