#define forward P2_0
#define backward P2_1
#define bujinjia P2_2
#define bujinjian P2_3
#define jiasu P2_6
#define jiansu P2_5
#define stop P2_4
static unsigned int k=0;
static unsigned int j=0;
code char fward[4]={0x33,0x99,0xcc,0x66};
code char bward[4]={0x99,0x33,0x66,0xcc};
unsigned char timer0_tick=0,i=0,times=66,status=1;
void delay()
{
unsigned char i;
for (i=500;i>0;i--);
}
//步进加速
jinjia()
{
int m;
if (P2_2==0)
delay();
if(P2_2==1)
{
m=m+1;
k++;
}
return (k);
}
//步进减速
jinjian()
{
int n;
if (P2_3==0)
delay();
if(P2_3==1)
{
n=n+1;
j++;
}
return (j);
}
/////////外部中断///////
static void timer0_isr(void) interrupt TF0_VECTOR using 1
{
TR0=0;
TL0=(time0count & 0x00ff);
TH0=(time0count>>8);
TR0=1;
timer0_tick++;
if(timer0_tick==times)
{
i++;
if(i==4) i=0;
timer0_tick=0;
if(status==1) P1=fward[i];
if(status==0) P1=bward[i];
if(status==2) P1=0x00;
}
}
//时间中断
static void time0_initialize(void)
{
EA=0;
timer0_tick=0;
TR0=0;
TMOD=0x01;
TL0=(time0count & 0x00ff);
TH0=(time0count>>8);
PT0=0;
ET0=1;
TR0=1;
EA=1;
}
///主函数
void main(void)
{
time0_initialize();
while(1)
{
if(forward==0) status=1;
if(backward==0) status=0;
if(stop==0) status=2;
jinjia();
if(k==1) times=61;
if(k==2) times=56;
if(k==3) times=51;
if(k==4) times=46;
if(k==5) times=41;
if(k==6) times=36;
if(jiasu==0) times=32;
jinjian();
if(j==1) times=71;
if(j==2) times=76;
if(j==3) times=81;
if(j==4) times=86;
if(j==5) times=91;
if(j==6) times=96;
if(jiansu==0) times=100;
}
}
以上便是4相步进电机的程序,下面是仿真图
上一篇:C51成功读写AT24C32(EEPROM)
下一篇:PS2键盘的设计---C51程序
推荐阅读最新更新时间:2024-03-16 15:20