欢迎来到天天文库
浏览记录
ID:39119749
大小:26.50 KB
页数:3页
时间:2019-06-25
《单片机控制步进电机正、反转、步数等程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#defineuintunsignedint#defineucharunsignedcharsbitk1=P3^0;sbitk2=P3^1;sbitk3=P3^2;sbitk4=P3^3;ucharFFW[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};ucharREV[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};uchark4_flag=0,k1_flag=0,k2_flag=0,k3_flag=0;uchars
2、tep_num=3,a;//=============================voidshezhi();voidDelayMS(uintms);//延时函数voidzhengzhuan(ucharn);//正转voidfanzhuan(ucharn);//反转voidanjian(void);//====================================================voidDelayMS(uintms)//延时函数{uchari;while(ms--){for(i=0;i<120;i++)
3、;}}//====================================================voidzhengzhuan(ucharn)//正转{uchari,j;for(i=0;i4、(k3==0)break;P1=REV[j];DelayMS(25);}}}//=============================voidanjian(void){if(k1==0)k1_flag=1;if(k2==0)k2_flag=1;if(k3==0)k3_flag=1;if(k4==0){k4_flag++;shezhi();}DelayMS(25);}//=============================voidshezhi(){if(a==0)k1_flag=1;if(a==1)k2_flag=1;sw5、itch(k4_flag){case1:step_num=3;break;case2:step_num=6;break;case3:step_num=9;break;case4:step_num=12;break;case5:step_num=15;break;case6:step_num=18;break;case7:step_num=21;break;case8:step_num=24;break;case9:step_num=27;break;}}//=============================voidmain6、(){while(1){anjian();while(k1_flag==1){a=0;zhengzhuan(step_num);P0=0xfe;if(P3!=0xff){k1_flag=0;break;}}while(k2_flag==1){a=1;fanzhuan(step_num);P0=0xfd;if(P3!=0xff){k2_flag=0;break;}}if(k3_flag==1){P0=0xfb;if(P3!=0xff)k3_flag=0;}}
4、(k3==0)break;P1=REV[j];DelayMS(25);}}}//=============================voidanjian(void){if(k1==0)k1_flag=1;if(k2==0)k2_flag=1;if(k3==0)k3_flag=1;if(k4==0){k4_flag++;shezhi();}DelayMS(25);}//=============================voidshezhi(){if(a==0)k1_flag=1;if(a==1)k2_flag=1;sw
5、itch(k4_flag){case1:step_num=3;break;case2:step_num=6;break;case3:step_num=9;break;case4:step_num=12;break;case5:step_num=15;break;case6:step_num=18;break;case7:step_num=21;break;case8:step_num=24;break;case9:step_num=27;break;}}//=============================voidmain
6、(){while(1){anjian();while(k1_flag==1){a=0;zhengzhuan(step_num);P0=0xfe;if(P3!=0xff){k1_flag=0;break;}}while(k2_flag==1){a=1;fanzhuan(step_num);P0=0xfd;if(P3!=0xff){k2_flag=0;break;}}if(k3_flag==1){P0=0xfb;if(P3!=0xff)k3_flag=0;}}
此文档下载收益归作者所有