我現在激光測距模塊里的通訊協議是通過16進制碼命令控制激光的打開,但16進制代碼怎么輸進去呢?#include <reg52.h>
#include"lcd.h"
#define uchar unsigned char
#define uint unsigned int
sbit k3=P3^2;
uint Sysec;
uchar ErrorCode;
uchar stringLenth;//串口收到的字符串長度,單次測量收到28字節 連續測量38個字節
#define maxSbufLenth 38 //根據測量模式設置緩存大小
uchar aciiCount; //收到的字符數計算
uchar multipleTestShift; //連續測量要減去一個偏移量10字節
char uartSbuf[11];//第20位是距離的十位數 21是個位數,后面是小數
uchar xdata laserOn[12] ={"$0003260130&"};
char xdata singleTest[4]={0x80,0x06,0x02,0x77};
uchar xdata multipleTest[10] ={"$00022426&"};
void Timer0Init(void) //50毫秒@6.000MHz
{
AUXR &= 0x7F; //定時器時鐘12T模式
TMOD &= 0xF0; //設置定時器模式
TMOD |= 0x01; //設置定時器模式
TL0 = 0x58; //設置定時初值
TH0 = 0x9E; //設置定時初值
TF0 = 0; //清除TF0標志
//TR0 = 1; //定時器0開始計時
ET0=0;
EA=1;
}
void UartInit(void) //115200bps@11.0592MHz
{
SCON=0X50; //設置為工作方式1
TMOD=0X20; //設置計數器工作方式2
PCON=0X00; //波特率加倍
TH1=0XFD; //計數器初始值設置,注意波特率是9600的
TL1=0XFD;
ES=1; //打開接收中斷
EA=1; //打開總中斷
TR1=1; //打開計數器
}
/*********** ***發送字符(ASCII)函數*** **********/
void sendAscii(char a[4])
{ int i;
ES = 0; //關串口中斷
for (i=0;i<4;i++)
{
SBUF = a[i];
while (TI!=1); //等待發送完成
TI = 0; //清除發送中斷標志位
}
ES = 1; //開串口中斷
}
void clearUartSbuf()
{
uchar i;
for (i=0;i<11;i++)
{
uartSbuf[i]=0x00;
}
}
/*void main()
{
uchar test;
UartInit();
LcdInit();
sendAscii({0x80,0x06,0x02,0x78});//打開激光
for(i=0;i<11;i++)
{
LcdWriteData(Disp[i]);
}
while(1);
}
*/
void uartRec()interrupt 4{
ES=0;//關閉中斷
if (RI)
{
RI=0;
aciiCount=0;
uartSbuf[aciiCount]=SBUF;
aciiCount++;
if (aciiCount>11) //根據各個模式截取合適長度的字符串
{
aciiCount=0;
}
}
if (TI)
{
TI=0;
}
ES=1;
}
typedef unsigned int u16; //對數據類型進行聲明定義
typedef unsigned char u8;
u8 Disp[11];
u16 f ;
/*******************************************************************************
* 函 數 名 : main
* 函數功能 : 主函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void main(void)
{
u8 i;
uchar test;
UartInit();
LcdInit();
sendAscii(singleTest);//打開激光
for(i=0;i<11;i++)
{
LcdWriteData(uartSbuf[i]);
}
while(1);
}
|