求单片机c语言控制步进电机调速程序

2024-11-22 18:09:27
推荐回答(2个)
回答(1):

几相的步进电机啊?用不用驱动芯片?用的话选什么型号的驱动芯片?你要说具体点啊!
先给你三相的,个不用驱动芯片的步进电机程序。(驱动电路自己做)
#include
#define uchar unsigned char
#define uint unsigned int
uint time

void timer1(void) interrupt 1 using 1{ //定时器0中断,定时1ms//
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
time++;
}

void delay(uint n){ //一般延时
while(--n);
}

void delay(uint dtime){ //延时
TR0=1;
while(time<=dtime);
time=0;
TR0=0;
}

void moto(bit cf,uchar n,uint dtime){ //电机驱动子程序,
uchar idata zhpai[4]={0x78,0xb8,0xd8,0xe8};
uchar idata fpai[4]={0xe8,0xd8,0xb8,0x78};
uchar i,l=0;
if(cf==0) //正转,A-B-C-A
for(i=0;iP1=zhpai[l];//P1.7~1.4为输出口,分别为A、B、C、D
l++;
delay(dtime);
if(l==4)l=0;
}
else{ //反转,C-B-A-C
for(i=0;iP1=fpai[l];
l++;
delay(dtime);
if(l==4)l=0;
}
}
}

void main(){
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
while(1){
moto(0,4,70);//正转,转四相,每相间隔70ms
delay(10000);
moto(1,4,70);//反转,转四相,每相间隔70ms
delay(10000);
}
}

回答(2):

  • #include #define uchar unsigned char #define uint unsigned int sbit KEY1 = P3^2; //步进电机逆时针方向转 sbit KEY2 = P3^3; //步进电机顺时针方向转 sbit KEY3 = P3^4; //步进电机调速 uchar step = 0; bit AB_flag = 0; unsigned char co...