|
#include<reg52.h>
#include<math.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar Buffer =0; //從串口接收的數(shù)據(jù)
uint URTAReceivedCount=0,n=1;
uchar data Tempdatatable[5],CommandDatatable[5];//數(shù)據(jù)包
/********************************************************************
* 名稱 : Delay_1ms()
* 功能 : 延時子程序,延時時間為 1ms * x
* 輸入 : x (延時一毫秒的個數(shù))
* 輸出 : 無
***********************************************************************/
void Delay_1ms(uint i)//1ms延時
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
/********************************************************************
* 名稱 : Send_Data()
* 功能 : 向上位機傳送字符
* 輸入 : 無
* 輸出 : 無
***********************************************************************/
void Send_Data(uchar type,uchar cmd,uchar dat)
{
uchar data Buffer[5];//構(gòu)建數(shù)據(jù)包
uchar *p;
uint Send_Count=0;
p=Buffer;
Buffer[0]=0XFF;
Buffer[1]=type;
Buffer[2]=cmd;
Buffer[3]=dat;
Buffer[4]=0XFF;
while(1)
{
if(*p==0XFF)
{
Send_Count++; //0XFF標志統(tǒng)計位
}
SBUF = *p; //發(fā)送
while(!TI) //如果發(fā)送完畢,硬件會置位TI,等待發(fā)送完畢
{
_nop_();
}
p++;
TI = 0;
if(Send_Count == 2) //當統(tǒng)計到兩次出現(xiàn)0XFF,則認為一個數(shù)據(jù)包發(fā)送完畢,跳出循環(huán)
{
TI = 0;
break;
}
}
}
/********************************************************************
協(xié)議規(guī)定:
包頭 類型位 數(shù)據(jù)位 數(shù)據(jù)位 結(jié)束位
0XFF 0X** OX** 0X** 0XFF
各命令說明:
類型位 數(shù)據(jù)位 數(shù)據(jù)位 功能
0X00 0X02 0X00 前進
0X00 0X01 0X00 后退
0X00 0X03 0X00 左轉(zhuǎn)
0X00 0X04 0X00 右轉(zhuǎn)
0X00 0X00 0X00 停止
0X01 0X01 角度 舵機1
0X01 0X02 . 舵機2
0X01 0X01 . 舵機3
0X01 0X02 . 舵機4
0X01 0X01 . 舵機5
0X01 0X02 . 舵機6
0X01 0X01 . 舵機7
0X01 0X02 數(shù)據(jù) 舵機8
0X02 0X01 車燈亮
0X02 0X02 車燈滅
剩下的私聊發(fā)。
|
|