只为初学者的步进电机正反控制程序#includereg51.h#defineucharunsignedchar#defineuintunsignedint#defineMotorDataP2//步进电机控制接口定义sbitzheng=P3^0;sbitfan=P3^1;sbitstop=P3^2;ucharphasecw[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};//正转ucharphaseccw[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};//反转//ms延时函数voiddelay(uintt){uintk;while(t--){for(k=0;k125;k++);}}voidDelay_xms(uintx){uinti,j;for(i=0;ix;i++)for(j=0;j112;j++);}//顺时针转动voidMotor_work(uintt){uchari,j;switch(t){case0:while(1){if(stop==0)break;for(i=0;i8;i++){MotorData=phasecw[i];delay(50);//转速调节}}break;case1:while(1){if(stop==0)break;for(j=0;j8;j++){MotorData=phaseccw[j];delay(50);//转速调节}}break;}}//停止转动voidMotor_test(void){if(zheng==0){Delay_xms(10);if(zheng==0)Motor_work(0);}if(fan==0){Delay_xms(10);if(fan==0)Motor_work(1);}}//主函数voidmain(void){Delay_xms(10);//等待系统稳定while(1){Motor_test();}}