4相5线的步进电机,在51hei单片机开发板上用uln2003驱动,此程序可实现正反转和加减速功能。
电路图如下:
#include <reg52.h> sbit 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;//正转 sbit p35=P3^5;//反转 #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 char uchar HZ; void Delay(uchar t) { while(--t); } void ms(uchar t) { while(t--) { Delay(245); Delay(245); } } void REV()//正转 { A ms(HZ); B ms(HZ); C ms(HZ); D ms(HZ); } void FWD()//反转 { D ms(HZ); C ms(HZ); B ms(HZ); A ms(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) { OFF if(!p34||!p35)goto A1; } } A2: ddd=1000; while(1) { OFF while(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; } } }