|
此设计包括了直流步进机的源程序代码,可以直接使用,
是用KEIL软件编的程序
程序如下:
#include<AT89X52.h>
#include<intrins.h>
#include<main.h>
bit moto_dir=0;
uchar irq_count ;
uchar irq_count_t;
uchar PWM_TIME_H;
uchar PWM_TIME_L;
bit i=0;
void shortdelay()
{
unsigned char k,j;
for(k=0;k>20;k--)
for(j=248;j>0;j--);
}
void run()
{
if(FZ_KEY)
{ shortdelay();
if(FZ_KEY)
{ moto_dir=1;
PWM3=0;PWM4=1;
}
while(FZ_KEY);
}
if(ZZ_KEY)
{
shortdelay();
if(ZZ_KEY)
{
moto_dir=0;
PWM3=1;PWM4=0; }
while(ZZ_KEY);
}
if(UP_KEY)
{ shortdelay();
if(UP_KEY)
{
if(PWM_TIME_H>10)
{
PWM_TIME_H--;
PWM_TIME_L=100-PWM_TIME_H;
}
}
while(UP_KEY);
}
if(DOWN_KEY)
{ shortdelay();
if(DOWN_KEY){
if(PWM_TIME_H<90)
{
PWM_TIME_H++;
PWM_TIME_L=100-PWM_TIME_H;
}
}
while(DOWN_KEY);
}
}
void main()
{ PWM1=1;PWM2=1;PWM3=1;PWM4=1;
irq_count=0;
i=0;
PWM_TIME_H=80;
PWM_TIME_L=20;
ET1=1;
TMOD=0X11;
TH1=0XFC;
TL1=0X18;
TR1=1;
EA=1;
while(1)
run();
}
void timer1() interrupt 3 using 1
{
TH1=0XFC;
TL1=0X18;
irq_count++;
if(irq_count>=irq_count_t)
{
irq_count=0;
i=~i;
if(i==1)
irq_count_t= PWM_TIME_H;
else
irq_count_t=PWM_TIME_L;
if( moto_dir==1)
{PWM2=~PWM2;PWM1=1;}
else
{PWM2=1;PWM1=~PWM1;}
}
}
|
|