// GY-25
// 使用单片机:STC89C52
// 晶振: 11.0592 M
// 液晶屏:LCD1602
// 波特率:9600
#include<REG52.H>
#include<math.h> //Keil library
#include<stdio.h> //Keil library
#include<INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
//****************电机定义部分******************
sbit IN1=P1^0;//左电机红线
sbit IN2=P1^1;//左电机蓝线
sbit ENA=P1^2;//左电机使能端
sbit wela=P2^7;
sbit dula=P2^6;
sbit lcden=P3^4;
sbit lcdrs=P3^5;
//********定义变量*****************************
uchar BUF[10]; //数据缓存区
uchar cnt=0; //接收数据的累计值
uchar sign=0;
int YPR[3];
float a;
float yuzhi0=0.0;
float yuzhi1=45.0;
float yuzhi2=90.0;
float yuzhi3=135.0;
float yuzhi4=180.0;
uchar s[18];
//*********************************************
//串口中断
void serial_serve(void) interrupt 4
{
if(RI==1){ //中断标志
RI=0;
BUF[cnt]=SBUF;
cnt++; //接收计数
if(cnt==8) //
{
cnt=0; //
sign=1;
}
}
}
//*********************************************
//串口初始化
//9600 bps @ 11.059 MHz
void init_uart()
{
TMOD=0x21;
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
PS=1; //串口中断设为高优先级别
TR0=1; //启动定时器
TR1=1;
ET0=1; //打开定时器0中断
ES=1;
// EA=1;
}
//**********延时函数***************
void delay(uint z)//z毫秒延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//写指令
void writecom(uchar com)
{
lcdrs=0;
P0=com;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
//写数据
void writedata(uchar date)
{
lcdrs=1;//高电平
P0=date;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
//1602液晶初始化
void init()
{
dula=0;
wela=0;
lcden=0;
writecom(0x38);
writecom(0x0c);
writecom(0x06);
writecom(0x01);
}
//*********串口数据发送******************
void SeriPushSend(uchar send_data)
{
SBUF=send_data;
while(!TI);TI=0;
}
//*************************************
void dis_play(int num)
{
if(num<0)
{
num=-num;
SeriPushSend('-');
}
else
SeriPushSend('+');
SeriPushSend(0x30|(num/10000));
SeriPushSend(0x30|(num%10000/1000));
SeriPushSend(0x30|(num%1000/100));
SeriPushSend(0x2e);
SeriPushSend(0x30|(num%100/10));
SeriPushSend(0x30|(num%10));
SeriPushSend(',');
}
void dispose(float jiaodu)
{
if((abs(jiaodu)>yuzhi0)&&(abs(jiaodu)<yuzhi1))
{
IN1=1;IN2=0;
ENA=0;
delay(1);
ENA=1;
delay(90);
}
if((abs(jiaodu)>yuzhi1)&&(abs(jiaodu)<yuzhi2))
{
IN1=0;IN2=1;
ENA=0;
delay(35);
ENA=1;
delay(55);
}
if((abs(jiaodu)>yuzhi2)&&(abs(jiaodu)<yuzhi3))
{
IN1=1;IN2=0;
ENA=0;
delay(55);
ENA=1;
delay(35);
}
if((abs(jiaodu)>yuzhi3)&&(abs(jiaodu)<yuzhi4))
{
IN1=0;IN2=1;
ENA=0;
delay(90);
ENA=1;
delay(0);
}
}
void display()
{
if(sign)
{
sign=0;
if(BUF[0]==0XAA&&BUF[7]==0X55)
{ //帧头判断
YPR[0]=(BUF[1]<<8|BUF[2]);
YPR[1]=(BUF[3]<<8|BUF[4]);
YPR[2]=(BUF[5]<<8|BUF[6]);
SeriPushSend('#');
SeriPushSend('Y');
SeriPushSend('P');
SeriPushSend('R');
SeriPushSend('=');
dis_play(YPR[0]);
dis_play(YPR[1]);
dis_play(YPR[2]);
SeriPushSend(0X0d);
SeriPushSend(0X0a);
}
}
else
{
SeriPushSend('N');
}
}
void yejing(int num,int num1,int num2)
{
uchar i;
if(num<0)
{
num=-num;
s[0]=0x30+(num/10000);
s[1]=0x30+(num%10000/1000);
s[2]=0x30+(num%1000/100);
s[3]=0x30+(num%100/10);
s[4]=0x30+(num%10);
writecom(0x80);
writedata('-');
for(i=0;i<=2;i++)
{
writedata(s[i]);
}
writedata('.');
for(i=3;i<=4;i++)
{
writedata(s[i]);
}
}
else
{
s[0]=0x30+(num/10000);
s[1]=0x30+(num%10000/1000);
s[2]=0x30+(num%1000/100);
s[3]=0x30+(num%100/10);
s[4]=0x30+(num%10);
writecom(0x80);
writedata('+');
for(i=0;i<=2;i++)
{
writedata(s[i]);
}
writedata('.');
for(i=3;i<=4;i++)
{
writedata(s[i]);
}
}
if(num1<0)
{
num1=-num1;
s[5]=0x30+(num1/10000);
s[6]=0x30+(num1%10000/1000);
s[7]=0x30+(num1%1000/100);
s[8]=0x30+(num1%100/10);
s[9]=0x30+(num1%10);
writecom(0x88);
writedata('-');
for(i=5;i<=7;i++)
{
writedata(s[i]);
}
writedata('.');
for(i=8;i<=9;i++)
{
writedata(s[i]);
}
}
else
{
s[5]=0x30+(num1/10000);
s[6]=0x30+(num1%10000/1000);
s[7]=0x30+(num1%1000/100);
s[8]=0x30+(num1%100/10);
s[9]=0x30+(num1%10);
writecom(0x88);
writedata('+');
for(i=5;i<=7;i++)
{
writedata(s[i]);
}
writedata('.');
for(i=8;i<=9;i++)
{
writedata(s[i]);
}
}
if(num2<0)
{
num2=-num2;
s[10]=0x30+(num2/10000);
s[11]=0x30+(num2%10000/1000);
s[12]=0x30+(num2%1000/100);
s[13]=0x30+(num2%100/10);
s[14]=0x30+(num2%10);
writecom(0x80+0x40);
writedata('-');
for(i=10;i<=12;i++)
{
writedata(s[i]);
}
writedata('.');
for(i=13;i<=14;i++)
{
writedata(s[i]);
}
}
else
{
s[10]=0x30+(num2/10000);
s[11]=0x30+(num2%10000/1000);
s[12]=0x30+(num2%1000/100);
s[13]=0x30+(num2%100/10);
s[14]=0x30+(num2%10);
writecom(0x80+0x40);
writedata('+');
for(i=10;i<=12;i++)
{
writedata(s[i]);
}
writedata('.');
for(i=13;i<=14;i++)
{
writedata(s[i]);
}
}
}
//主程序
void main()
{
init_uart();
init(); //初始化串口
EA=1; //开总中断
cnt=0;
SeriPushSend(0XA5);
SeriPushSend(0X52); //发送1帧读取命令,连续读取
while(1)
{
ENA=0;
display();
a=(YPR[2]/100.0);
dispose(a);
yejing(YPR[0],YPR[1],YPR[2]);
} //end while
}
|