51單片機—步進電機程序
電路圖如下:
本文引用地址:http://butianyuan.cn/article/201611/323322.htm
c語言源程序
#includesbit p10=P1^0;sbit p11=P1^1;sbit p12=P1^2;sbit p13=P1^3;sbit p32=P3^2;//減速sbit p33=P3^3;//加速sbit p34=P3^4;//正轉(zhuǎn)sbit p35=P3^5;//反轉(zhuǎn)#define A {p10=1;p11=0;p12=0;p13=0;}#define B {p10=0;p11=1;p12=0;p13=0;}#define C {p10=0;p11=0;p12=1;p13=0;}#define D {p10=0;p11=0;p12=0;p13=1;}#define OFF {p10=0;p11=0;p12=0;p13=0;}#define uint unsigned int#define uchar unsigned charuchar HZ;void Delay(uchar t){while(--t);}void ms(uchar t){while(t--){Delay(245);Delay(245);}}void REV()//正轉(zhuǎn){Ams(HZ);Bms(HZ);Cms(HZ);Dms(HZ);}void FWD()//反轉(zhuǎn){Dms(HZ);Cms(HZ);Bms(HZ);Ams(HZ);}void main(){uint ddd;HZ=5;A1:if(!p35)goto A2;ddd=500;while(1){while(ddd){if(!p32){HZ++;if(HZ>20)HZ=20;}if(!p33){HZ--;if(HZ<3)HZ=3;}REV();ddd--;if(!p34||!p35)goto A1;}while(1){OFFif(!p34||!p35)goto A1;}}A2:ddd=1000;while(1){OFFwhile(ddd){if(!p32){HZ++;if(HZ>30)HZ=30;}if(!p33){HZ--;if(HZ<3)HZ=3;}FWD();ddd--;if(!p34||!p35)goto A1;}while(1){if(!p34)goto A1;if(!p35)goto A2;}}}
評論