发表于:2005-11-09 22:08:00
楼主
我想用AT89S51单片机同时控制两台一样的步进电动机(要求两台步机的转速可以不同,一台正转一台反转):用C语言编程,其中步机1的四相分别连在P1.0-P1.3,步机2的四相分别连在P2.0-P2.3==>故有:步机1正转{0x03,0x09,0x0c,0x06};步机2反转{0x03,0x06,0x0c,0x09};然后用中断控制延时,大略的程序如下:
#include <reg51.h>
#define Num_run 192
#define m_T 100
unsigned char idata cw1[5]={0x03,0x09,0x0c,0x06,0x00};
unsigned char idata ccw1[5]={0x03,0x06,0x0c,0x09,0x00};
unsigned char idata cw2[5]={0x03,0x09,0x0c,0x06,0x00};
unsigned char idata ccw2[5]={0x03,0x06,0x0c,0x09,0x00};
unsigned char idata *p1,*p2;//分别用于再P1,P2口输出步机1,步机2的正转字
bit intt1=0;//两个中断的标志,等于1为中断
bit intt2=0;
void control(bit direct1,unsigned int num_run1,bit direct2,unsigned int num_run2)//同时控制两个步机的函数
{
unsigned int n1,n2;
if(direct1==1) p1=cw1;
else p1=ccw1;
if(direct2==1) p2=cw2;
else p2=ccw2;
TMOD=0x11;//同时用T/C0及T/C1工作在模式1:16位
TH0=(65536-m_T*500)/256;
TL0=(65536-m_T*500)%256;
TH1=(65536-m_T*250)/256;
TL1=(65536-m_T*250)%256;
ET0=1;
ET1=1;
TR0=1;
TR1=1;;//启动T/C0及T/C1
EA=1;
..............
}
void delay_time1(void)interrupt 1 using 1
{
P1=*p1++;//P1口输出步机1正转字
if(*p1==0) p1-=4;
TH0=(65536-m_T*500)/256;
TL0=(65536-m_T*500)%256;
intt1=1;//中断标志
}
void delay_time2(void)interrupt 2 using 2
{
P2=*p2++;//P2口输出步机2反转转字
if(*p2==0) p2-=4;
TH1=(65536-m_T*250)/256;
TL1=(65536-m_T*250)%256;
intt2=1;
}
void main()
{
control(1,Num_run,0,Num_run);
}
..............部分的循环语句怎弄?还是整个程序有问题?
请伸出你热情的手帮小弟一把,不胜感激!
开始我用于控制一台步机时用:
for(n1=0;n1<num_run1;n1++)
{
while(intt1==0);
intt1=0;
}
倒时可以运行,但同时控制两台不知道怎弄