#include<reg51.h>
#include<absacc.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define PA XBYTE[0x0000]
#define PB XBYTE[0x0001]
#define PC XBYTE[0x0002]
#define COM XBYTE[0x0003] //PA、PB、PC端口及命令端口地址定义
#define motor1 PA
#define motor2 PB
sbit RS = P3^5; //液晶引脚定义
sbit RW = P3^3;
sbit EN = P3^4;
sbit Xqj=P2^0; //电机正转,X轴前进按钮
sbit Yqj=P2^1; //电机正转,Y轴前进按钮
sbit Xht=P2^2; //电机反转,X轴后退按钮
sbit Yht=P2^3; //电机反转,Y轴后退按钮
sbit jiting=P2^4;//急停按钮
sbit dula=P2^6;
sbit wela=P2^7;
bit flag1=0; //x轴正反转标志位
bit flag2=0; //Y轴正反转标志位
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf8,0x80,0x90};//共阳极数码管编码
uchar code a1[]={0xf2,0xfa,0xf8,0xf9,0xf1,0xf5,0xf4,0xf6}; //八拍正转
uchar code a2[]={0xf6,0xf4,0xf5,0xf1,0xf9,0xf8,0xfa,0xf2}; //八拍反转
uchar temp1=0000,temp2=0000,temp3=0000,temp4=0000;
void delayms(uchar x)//延时程序
{
uchar c,j;
for(c=x;c>0;c--)
for(j=123;j>0;j--);
}
/***********LED写入命令函数***********/
void write_com(uchar com)
{
//while(LCD_Check_Busy()); //忙则等待
RS=0;
P1=com;
_nop_();
RW=0;
EN=1;
_nop_();
EN=0;
_nop_();
}
/**********LED写入数据函数**********/
void write_dat(uchar chr)
{
//while(LCD_Check_Busy()); //忙则等待
RS=1;
P1= chr;
_nop_();
RW=0;
EN=1;
_nop_();
EN=0;
_nop_();
}
void write_shuzi(uchar dat)
{
uchar t;
t=dat+'0';
//while(LCD_Check_Busy()); //忙则等待
RS=1;
RW=0;
P1=t;
_nop_();
EN=1;
_nop_();
EN=0;
}
void GotoXY(uchar x,uchar y)
{
if(y==0) //第一行
write_com(0x80|x); //写地址
if(y==1) //第二行
write_com(0x80|(x+0x40));
}
/*****LED初始化函数******/
void LCD_Init()
{
RW=0;
dula=0; //关闭数码管显示
wela=0; //关闭数码管显示
write_com(0x38); /*显示模式设置*/
delayms(5);
write_com(0x06);/*显示光标移动设置*/
delayms(5);
write_com(0x0C); /*显示开及光标设置*/
write_com(0x01); /*显示清屏*/
}
void dispose()
{
uchar a,b,bai1,shi1,ge1,bai2,shi2,ge2;
a=(temp1-temp3)/8*75/36*5;
b=(temp2-temp4)/8*75/36*5;
bai1=a%1000/100;
shi1=a%100/10;
ge1=a%10;
bai2=b%1000/100;
shi2=b%100/10;
ge2=b%10;
write_shuzi(bai1);
GotoXY(7,1);
write_shuzi(shi1);
GotoXY(8,1);
write_shuzi(ge1);
GotoXY(9,1);
write_shuzi(bai2);
GotoXY(7,2);
write_shuzi(shi2);
GotoXY(8,2);
write_shuzi(ge2);
GotoXY(9,2);
write_dat('X');
GotoXY(4,1);
write_dat(':');
GotoXY(5,1);
write_dat('Y');
GotoXY(4,2);
write_dat(':');
GotoXY(5,2);
if(a<0)
{
write_dat('-');
GotoXY(6,1);
}
if(b<0)
{
write_dat('-');
GotoXY(6,2);
}
}
void key()
{
if(Xqj==0)
{
delayms(5);
if(Xqj==0)
{
flag1=1;
while(Xqj==0);
}
}
if(Xht==0)
{
delayms(5);
if(Xht==0)
{
flag1=~flag1;
while(Xht==0);
}
}
if(Yqj==0)
{
delayms(5);
if(Yqj==0)
{
flag2=1;
while(Yqj==0);
}
}
if(Yht==0)
{
delayms(5);
if(Yht==0)
{
flag2=~flag2;
while(Yht==0);
}
}
}
void qudong()
{
if(flag1==1)
{
uchar i;
for(i=0;i<8;i++)
{
if(jiting==0)
{break;}
PA=a1[i];
temp1++;
}
i=0;
}
if(flag2==1)
{
uchar j;
for(j=0;j<8;j++)
{
if(jiting==0)
{break;}
PB=a1[j];
temp2++;
}
j=0; //八拍正转子程序
}
if(flag1==0)
{
uchar k;
for(k=0;k<8;k++)
{
if(jiting==0)
{break;}
if(Xht==0)
{PA=a2[k];}
temp3++;
}
k=0;
}
if(flag2==0)
{
uchar u;
for(u=0;u<8;u++)
{
if(jiting==1)
{break;}
if(Yht==1)
{PB=a2[u];}
temp4++;
} //八拍反转转子程序
u=0;
}
}
void main()
{
COM=0x80;//方式0,PA口与PB口均为输出
Xqj=1;
Yqj=1;
Xht=1;
Yht=1;
jiting=1;
while(1)
{
key();
dispose();
qudong();
}
}
这是程序 |