stm32串口接收
具体协议看之前的01
typedef struct
{
long int x_cm;
long int y_cm;
int openmv;
}OPMV_info;
extern OPMV_info User_openmv1;
extern OPMV_info User_openmv2;
void Openmv1_GetOneByte(uint8_t data);
void Openmv2_GetOneByte(uint8_t data);
OPMV_info User_openmv1;
OPMV_info User_openmv2;
extern signed temp1, temp2;
signed temp1= 0, temp2= 0;
void Openmv1_GetOneByte(uint8_t data)
{
static signed RxBuffer2[10];
static uint8_t rxstate1 =0;
static uint8_t Rxcounter2 =0;
if(rxstate1 == 0 && data == 0x2c)
{
rxstate1 = 1;
RxBuffer2[Rxcounter2++] = data;
}
else if(rxstate1 == 1 && data == 0x12)
{
rxstate1 = 2;
RxBuffer2[Rxcounter2++] = data;
}
else if (rxstate1 == 2)
{
RxBuffer2[Rxcounter2++] = data;
if(data == 0x5B)
{
rxstate1= 3;
}
}
else
{
rxstate1 = 0;
Rxcounter2 = 0;
}
if (rxstate1 == 3)
{
if(RxBuffer2[Rxcounter2-1]==0x5B)
{
temp1 = RxBuffer2[3]<<8|RxBuffer2[2];
temp2 = RxBuffer2[5]<<8|RxBuffer2[4];
if(temp1>1000)
{
temp1 =-(65536 - temp1);
}
if(temp2>1000)
{
temp2 =-(65536 -temp2);
}
if(temp1 > -80 && temp1 < 80)
{
User_openmv1.x_cm = temp1;
if(User_openmv1.x_cm>1000)
{
User_openmv1.x_cm =-(65536 - User_openmv1.x_cm);
}
}
if(temp2 > -80 && temp2 < 0)
{
User_openmv1.y_cm = temp2;
if(User_openmv1.y_cm>1000)
{
User_openmv1.y_cm =-(65536 - User_openmv1.y_cm);
}
}
rxstate1 = 0;
Rxcounter2 = 0;
User_openmv1.openmv= 1;
}
else
{
rxstate1 = 0;
Rxcounter2 = 0;
}
}
}
这里是串口中断服务函数 进入某个人串口的中断的时候会自动调用,前提是你打开了中断。
HAL_UART_Receive_IT收到数据之后就会存到Buffer_1中,也就是会存入上面写的那个结构体里面,具体会存什么数据自由定义。
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart1)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
Openmv1_GetOneByte(Buffer_1[pt_w1]);
HAL_UART_AbortReceive_IT(&huart1);
HAL_UART_Receive_IT(&huart1,&Buffer_1[++pt_w1],1);
if(pt_w1>10)
{
pt_w1 = 0;
}
}
if(huart == &huart2)
{
Openmv2_GetOneByte(Buffer_2[pt_w2]);
HAL_UART_AbortReceive_IT(&huart2);
HAL_UART_Receive_IT(&huart2,&Buffer_2[++pt_w2],1);
if(pt_w2>10)
{
pt_w2 = 0;
}
}
if(huart == &huart3)
{
HAL_UART_AbortReceive_IT(&huart3);
HAL_UART_Receive_IT(&huart3,&Buffer_3[++pt_w1],1);
if(pt_w1>7)
{
pt_w1 = 0;
}
}
}