标题:
单片机步进电机控制实现直线插补的仿真与程序源码
[打印本页]
作者:
151431
时间:
2018-7-10 14:36
标题:
单片机步进电机控制实现直线插补的仿真与程序源码
单片机控制步进电机实现直线插补仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
0.png
(21.02 KB, 下载次数: 31)
下载附件
2018-7-10 19:13 上传
单片机源程序如下:
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code bux[]={0xf0,0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
uchar code buy[]={0x00,0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90};
uchar code comx[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code comy[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit SX=P3^4;
sbit SY=P3^5;
sbit ST=P3^6;
uint countx=0;
uint county=0;
uint count,flag=0;
uint x=0;
uint y=0;
uint a=0,b=0,k=0,m=0;
uint st=0;
char Fm[20];
void Key_Scan(void);
void Key_Action(void);
void motor();
void stepx();
void stepy();
void star();
void time0(void);
void delayms(uint t)
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<112;j++);
}
void main()
{
while(1)
{
Key_Scan();
time0();
while(!flag);
flag=0;
motor();
if(1==st)
{
star();
st=0;
}
}
}
void Key_Scan(void)
{
if(0==SX)
{
delayms(10);
if(0==SX)
{
countx++;
while(!SX);
}
}
if(0==SY)
{
delayms(10);
if(0==SY)
{
county++;
while(!SY);
}
}
}
void time0(void)
{
TMOD=0x01;
TH0=(65536-9216)/256;
TL0=(65536-9216)%256;
EA=1;
ET0=1;
TR0=1;
}
void TimeRS(void) interrupt 1
{
TH0=(65536-9216)/256;
TL0=(65536-9216)%256;
count++;
if(count>=10)
{
Key_Action();
flag=1;
count=0;
}
}
void Key_Action(void)
{
uint i,j;
if(countx>9)
{
countx=0;
}
if(county>9)
{
county=0;
}
P0=comx[countx];
//delayms(100);
P2=comy[county];
//delayms(100);
}
void motor()
{
uint i,j;
if(0==ST)
{
delayms(10);
if(0==ST)
{
st=1;
while(!ST);
}
}
}
void star()
{
a=countx;
b=county;
m=a+b;
Fm[k]=0;
while(m>0)
{
if(Fm[k]>=0)
{
x++;
m--;
stepx();
k++;
Fm[k]=Fm[k-1]-b;
}
if(Fm[k]<0)
{
y++;
m--;
stepy();
k++;
Fm[k]=Fm[k-1]+a;
}
}
}
void stepx()
{
P3=bux[x];
delayms(1000);
}
void stepy()
{
P1=bux[y];
delayms(1000);
}
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code bux[]={0xf0,0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
uchar code buy[]={0x00,0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90};
uchar code comx[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code comy[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit SX=P3^4;
sbit SY=P3^5;
sbit ST=P3^6;
uint countx=0;
uint county=0;
uint count,flag=0;
uint x=0;
uint y=0;
uint a=0,b=0,k=0,m=0;
uint st=0;
char Fm[20];
void Key_Scan(void);
void Key_Action(void);
void motor();
void stepx();
void stepy();
void star();
void time0(void);
void delayms(uint t)
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<112;j++);
}
void main()
{
while(1)
{
Key_Scan();
time0();
while(!flag);
flag=0;
motor();
if(1==st)
{
star();
st=0;
}
}
}
void Key_Scan(void)
{
if(0==SX)
{
delayms(10);
if(0==SX)
{
countx++;
while(!SX);
}
}
if(0==SY)
{
delayms(10);
if(0==SY)
{
county++;
while(!SY);
}
}
}
void time0(void)
{
TMOD=0x01;
TH0=(65536-9216)/256;
TL0=(65536-9216)%256;
EA=1;
ET0=1;
TR0=1;
}
void TimeRS(void) interrupt 1
{
TH0=(65536-9216)/256;
TL0=(65536-9216)%256;
count++;
if(count>=10)
{
Key_Action();
flag=1;
count=0;
}
}
void Key_Action(void)
{
uint i,j;
if(countx>9)
{
countx=0;
}
if(county>9)
{
county=0;
}
P0=comx[countx];
//delayms(100);
P2=comy[county];
//delayms(100);
}
void motor()
{
uint i,j;
if(0==ST)
{
delayms(10);
if(0==ST)
{
st=1;
while(!ST);
}
}
}
void star()
{
a=countx;
b=county;
m=a+b;
Fm[k]=0;
while(m>0)
{
if(Fm[k]>=0)
{
x++;
m--;
stepx();
k++;
Fm[k]=Fm[k-1]-b;
}
if(Fm[k]<0)
{
y++;
m--;
stepy();
k++;
Fm[k]=Fm[k-1]+a;
}
}
}
void stepx()
{
P3=bux[x];
delayms(1000);
}
void stepy()
{
P1=bux[y];
delayms(1000);
}
复制代码
0.png
(53.37 KB, 下载次数: 37)
下载附件
2018-7-10 19:15 上传
所有资料51hei提供下载:
步进电机.rar
(32.22 KB, 下载次数: 81)
2018-7-10 14:35 上传
点击文件名下载附件
步进电机控制
下载积分: 黑币 -5
作者:
阿拉登666
时间:
2018-12-3 21:30
感谢分享 值得借鉴
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1