|
#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()
* 功能 : 延時(shí)子程序,延時(shí)時(shí)間為 1ms * x
* 輸入 : x (延時(shí)一毫秒的個(gè)數(shù))
* 輸出 : 無(wú)
***********************************************************************/
void Delay_1ms(uint i)//1ms延時(shí)
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
/********************************************************************
* 名稱 : Send_Data()
* 功能 : 向上位機(jī)傳送字符
* 輸入 : 無(wú)
* 輸出 : 無(wú)
***********************************************************************/
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標(biāo)志統(tǒng)計(jì)位
}
SBUF = *p; //發(fā)送
while(!TI) //如果發(fā)送完畢,硬件會(huì)置位TI,等待發(fā)送完畢
{
_nop_();
}
p++;
TI = 0;
if(Send_Count == 2) //當(dāng)統(tǒng)計(jì)到兩次出現(xiàn)0XFF,則認(rèn)為一個(gè)數(shù)據(jù)包發(fā)送完畢,跳出循環(huán)
{
TI = 0;
break;
}
}
}
/********************************************************************
協(xié)議規(guī)定:
包頭 類型位 數(shù)據(jù)位 數(shù)據(jù)位 結(jié)束位
0XFF 0X** OX** 0X** 0XFF
各命令說(shuō)明:
類型位 數(shù)據(jù)位 數(shù)據(jù)位 功能
0X00 0X02 0X00 前進(jìn)
0X00 0X01 0X00 后退
0X00 0X03 0X00 左轉(zhuǎn)
0X00 0X04 0X00 右轉(zhuǎn)
0X00 0X00 0X00 停止
0X01 0X01 角度 舵機(jī)1
0X01 0X02 . 舵機(jī)2
0X01 0X01 . 舵機(jī)3
0X01 0X02 . 舵機(jī)4
0X01 0X01 . 舵機(jī)5
0X01 0X02 . 舵機(jī)6
0X01 0X01 . 舵機(jī)7
0X01 0X02 數(shù)據(jù) 舵機(jī)8
0X02 0X01 車燈亮
0X02 0X02 車燈滅
剩下的私聊發(fā)。
|
|