初中生
最后登录1970-1-1
在线时间 小时
注册时间2022-6-15
|
楼主 |
发表于 2022-9-9 23:11:31
|
显示全部楼层
我的代码:
/* USER CODE BEGIN 0 */
#define u8 unsigned char
u8 receive[9],send[4];
char str[80]="";
struct
{
uint8_t sign;
uint8_t type;
uint8_t amount;
uint16_t distance;
uint8_t mode;
uint8_t temp;
uint8_t sum;
}GY_56_tx = {0x5A,0x15,0x03,0,0,0,0},*p_gy=&GY_56_tx;
struct
{
uint8_t spads;
uint8_t temp_correction;
uint8_t deviation_correction;
uint8_t win_correction;
uint8_t load_data;
uint8_t measure_mode;
uint8_t output_mode;
uint8_t baud;
uint8_t save_mode;
uint8_t max;
uint8_t min;
} GY_56_rx = {0x0a,0x10,0x21,0x32,0x43,0x54,0x65,0x76,0x87,0x98,0xa9},*p_gy_r=&GY_56_rx;
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)
{
}
if(huart->Instance==USART2)
{
uint8_t receive[20]={0};
}
if(huart->Instance==UART4)
{
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)
{
}
if(huart->Instance==USART2)
{
uint8_t receive[20]={0};
}
if(huart->Instance==UART4)
{
}
}
//void send_com(u8 function,u8 value )
//{
// u8 bytes[4]={0};
// bytes[0]=0xa5;
// bytes[1]=function;//功能指令
// bytes[2]=value;//指令值
// USART_Send(bytes,4);//发送帧头、功能字节、校验和
//}
//void USART_Send(uint8_t *Buffer, uint8_t Length)
//{
// uint8_t i=0;
// while(i<Length)
// {
// if(i<(Length-1))
// Buffer[Length-1]+=Buffer[i];//累加Length-1前的数据
// HAL_UART_Transmit(&huart1, &Buffer[i++],sizeof(Buffer[i++]),10000);
// }
//}
void send_gy(u8 fuction,u8 value)
{
send[0]=0xa5;
send[1]=fuction;
send[2]=value;
send[3]=send[0]+send[1]+send[2];
HAL_UART_Transmit(&huart2,send,sizeof(send),1000);
}
void receive_gy()
{
uint8_t j=0;
char t[20];
for(j=0;j<9;j++)
{
HAL_UART_Receive(&huart2,&receive[j],sizeof(receive[j]),1000);
sprintf(t,"%d\n",receive[j]);
HAL_UART_Transmit(&huart1,t,sizeof(t),10000);
}
if(receive[0]!=p_gy->sign|receive[0]!=p_gy->sign)
{
char f[]="failure\t";
HAL_UART_Transmit(&huart1,f,sizeof(f),10000);
}
else
{
p_gy->distance=(receive[4]<<8)|receive[5];
p_gy->temp=receive[6];
p_gy->temp=receive[7];
p_gy->sum=receive[8];
sprintf(str,"%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t",receive[0], receive[1],receive[2],receive[3],receive[4], receive[5],receive[6],receive[7],receive[8]);
HAL_UART_Transmit(&huart1,str,sizeof(str),10000);
sprintf(str,"dis:%u cm\t mode:%u\t temp:%u\t sum:%u\n",p_gy->distance, p_gy->mode,p_gy->temp,p_gy->sum);
HAL_UART_Transmit(&huart1,str,sizeof(str),10000);
sprintf(str,"dis:%u cm\t mode:%u\t temp:%u\t sum:%u\n",receive[4], receive[5],receive[6],receive[7]);
HAL_UART_Transmit(&huart1,str,sizeof(str),10000);
}
}
/* USER CODE END 0 */
|
|