#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; //電機正轉(zhuǎn),X軸前進按鈕
sbit Yqj=P2^1; //電機正轉(zhuǎn),Y軸前進按鈕
sbit Xht=P2^2; //電機反轉(zhuǎn),X軸后退按鈕
sbit Yht=P2^3; //電機反轉(zhuǎn),Y軸后退按鈕
sbit jiting=P2^4;//急停按鈕
sbit dula=P2^6;
sbit wela=P2^7;
bit flag1=0; //x軸正反轉(zhuǎn)標志位
bit flag2=0; //Y軸正反轉(zhuǎn)標志位
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf8,0x80,0x90};//共陽極數(shù)碼管編碼
uchar code a1[]={0xf2,0xfa,0xf8,0xf9,0xf1,0xf5,0xf4,0xf6}; //八拍正轉(zhuǎn)
uchar code a2[]={0xf6,0xf4,0xf5,0xf1,0xf9,0xf8,0xfa,0xf2}; //八拍反轉(zhuǎn)
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寫入命令函數(shù)***********/
void write_com(uchar com)
{
//while(LCD_Check_Busy()); //忙則等待
RS=0;
P1=com;
_nop_();
RW=0;
EN=1;
_nop_();
EN=0;
_nop_();
}
/**********LED寫入數(shù)據(jù)函數(shù)**********/
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初始化函數(shù)******/
void LCD_Init()
{
RW=0;
dula=0; //關閉數(shù)碼管顯示
wela=0; //關閉數(shù)碼管顯示
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; //八拍正轉(zhuǎn)子程序
}
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++;
} //八拍反轉(zhuǎn)轉(zhuǎn)子程序
u=0;
}
}
void main()
{
COM=0x80;//方式0,PA口與PB口均為輸出
Xqj=1;
Yqj=1;
Xht=1;
Yht=1;
jiting=1;
while(1)
{
key();
dispose();
qudong();
}
}
這是程序 |