部分指令:
char SingleMea[4]={0x80,0x06,0x02,0x78};
//关机
char Switch_Off[4]={0x80,0x04,0x02,0x7A};
//开机
char Switch_On[8]={0x16,0x03,0x00,0x02,0x00,0x01,0x26,0xED};//
//连续测量
char Cont_Mea[4]={0x80,0x06,0x03,0x77};
//连续测量间隔时间
char Cont_Mea_time[5]={0xFA,0x04,0x05,0x01,0x04};
//设置地址(01)
char Set_addr[5]={0xFA,0x04,0x01,0x80,0x07};
//设置量程(10m)
char Range[5]= {0xFA,0x04,0x09,0x0A,0xEF};
//设置频率(5Hz)
char Set_freq[5]={0xFA,0x04,0x0A,0x05,0x0D};
//设置分辨率(0.1mm)
char Set_accuracy[5]={0xFA,0x04,0x0C,0x02,0xF4};
//上电就测(开启)
char Mea_On[5]={0xFA,0x04,0x0D,0x01,0xF4};
//控制激光开启
char Ctrl_On[5]={0x80,0x06,0x05,0x01,0x74};
//控制激光关闭
char Ctrl_Off[5]={0x80,0x06,0x05,0x00,0x75};
指令发送函数:
void JG_Send_Data(USART_TypeDef* USARTx, char* point, u8 Length)
{
USART_ClearFlag(USARTx,USART_FLAG_TC);
while(Length--)
{
USART_SendData(USARTx,*point++);
while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
}
}
//由于数据中存在0x00,所以必须有length这个参数的存在,否则它会被当作字符串的结束标志‘\0’.
串口接收协议(简易版):
void USART3_IRQHandler(void) //串口3中断服务程序
{
static u8 Rx_buf[11] = {0x00}; //帧数据缓存
static u8 count = 0, sum = 0; //计数标志 校验和
u8 ch;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
ch=USART_ReceiveData(USART3);
if((count == 0) && (ch != 0x80)) return; //判断帧头
if((count == 1) && (ch != 0x06)) return;
if((count == 2) && (ch != 0x83)) return;
if(count<10) sum += ch;
Rx_buf[count++] = ch;
if(count == 11)
{
count = 0;
sum = ~sum +1;//(由于符号位扩展的问题,需要再次赋值)
if(sum != Rx_buf[10]) //数据出错
{
LED0=0;
sum = 0;
}
else
{
LED1=0;
sum = 0;
JG_data_analys(&JG_Dist, Rx_buf); //激光测距数据解析
}
}
}
}
//代码中有许多的错误情况没写,只能说这段代码基本能用
数据解析函数:
void JG_data_analys(JG_Dist_str* JG,u8* rxbuff)
{
JG->HundredBits = 100*hex_to_ascll(rxbuff[3]);//百位
JG->TenBits = 10* hex_to_ascll(rxbuff[4]);//十位
JG->Bits = hex_to_ascll(rxbuff[5]) ;//个位
JG->TenthBits = hex_to_ascll(rxbuff[7])/10.0;//十分位
JG->PercentBits = hex_to_ascll(rxbuff[8])/100.0;//百分位
/*十进制结果 */
JG->JG_result=JG->HundredBits+JG->TenBits+JG->Bits+JG->TenthBits+JG->PercentBits;
}
版权声明:本文为CSDN博主「菜小白xdm」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_61628978/article/details/122865054
暂无评论