mv用到串口2 从mv代码中找到。 32接收mv发来的数据用0x0a结束 mv.c
#include "openmv_uart.h"
#define END_CODE 0x0a
u8 openmv_rx_data[Rx_Length];
u8 length=0;
u8 rx_flag=0;
void Openmv_Usart_Rx_Irq(u8 data)
{
openmv_rx_data[length]=data;
if(openmv_rx_data[length]==END_CODE)
{
rx_flag=1;
length=0;
}
else
{
length++;
if(length>Rx_Length-1) length=0;
}
}
void Openmv_Send_Cmd(char* cmd)
{
Usart_Printf(&uart2,"%s\n",cmd);
}
void Clear_Openmv_Rxbuff(void)
{
u8 *p,i;
p=openmv_rx_data;
for(i=0;i<Rx_Length;i++)
{
*p++=0;
}
length=0;
rx_flag=0;
}
u8 Judge_Openmv_Response(char* fmt,...)
{
char p[30];
if(!rx_flag) return 0;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)p,fmt,ap);
va_end(ap);
if(strstr((char*)openmv_rx_data,p)==NULL) return 0;
else return 1;
}
u8 Get_Openmv_Materials_XY(materials_blob *blob)
{
s32 x,y,a,cx,cy;
if(!rx_flag) return 0;
sscanf((const char *)openmv_rx_data,"x:%dy:%da:%dx:%dy:%d",&x,&y,&a,&cx,&cy);
blob->x=(float)(x-65);
blob->y=(float)(y-57);
blob->a=a;
blob->cx=(float)(cx-65);
blob->cy=(float)(cy-57);
return 1;
}
u8 Get_Openmv_Materials_Labels_Size(u8 *input_label,s32 *input_size)
{
s32 label_;
s32 size_;
if(!rx_flag) return 0;
sscanf((const char *)openmv_rx_data,"l:%da:%d",&label_,&size_);
*input_label=(u8)label_;
*input_size=(u32)size_;
return 1;
}
u8 Get_Openmv_Lighting(u8 *input_lighting)
{
s32 lighting_;
if(!rx_flag) return 0;
sscanf((const char *)openmv_rx_data,"%d",&lighting_);
*input_lighting=(u8)lighting_;
return 1;
}
u8 Get_Openmv_Angle(u8 input_angle[])
{
s32 angle_[3];
if(!rx_flag) return 0;
sscanf((const char *)openmv_rx_data,"lr:%d,ud:%d,an:%d",&angle_[0],&angle_[1],&angle_[2]);
input_angle[0]=(u8)angle_[0];
input_angle[1]=(u8)angle_[1];
input_angle[2]=(u8)angle_[2];
return 1;
}
mv.h
#ifndef __OPENMV_UART_H
#define __OPENMV_UART_H
#include "my_scc.h"
#define Rx_Length 100
void Openmv_Usart_Rx_Irq(u8 data);
void Openmv_Send_Cmd(char* cmd);
void Clear_Openmv_Rxbuff(void);
u8 Judge_Openmv_Response(char* fmt,...);
u8 Get_Openmv_Materials_XY(materials_blob *blob);
u8 Get_Openmv_Materials_Labels_Size(u8 *input_label,s32 *input_size);
u8 Get_Openmv_Lighting(u8 *input_lighting);
u8 Get_Openmv_Angle(u8 input_angle[]);
#endif
关于变参问题的 https://www.cnblogs.com/coolYuan/p/10181011.html
串口配置文件 usart.c
#include "usart.h"
USART_DATA uart1;
USART_DATA uart2;
USART_DATA uart3;
static void USART_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_8|GPIO_Pin_9;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
static void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
USART_InitStructure.USART_BaudRate =9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART1,USART_IT_TC,ENABLE);
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART2,USART_IT_TC,ENABLE);
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART3,USART_IT_TC,ENABLE);
USART_ClearITPendingBit(USART1,USART_IT_TC);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
USART_ClearFlag(USART1,USART_FLAG_ORE);
USART_ClearITPendingBit(USART2,USART_IT_TC);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
USART_ClearFlag(USART2,USART_FLAG_ORE);
USART_ClearITPendingBit(USART3,USART_IT_TC);
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
USART_ClearFlag(USART3,USART_FLAG_ORE);
USART_Cmd(USART1, ENABLE);
USART_Cmd(USART2, ENABLE);
USART_Cmd(USART3, ENABLE);
}
void Usart_Init(void)
{
USART_GPIO();
USART_Configuration();
uart1.usart_n=USART1;
uart1.tx_busy=0;
uart2.usart_n=USART2;
uart2.tx_busy=0;
uart3.usart_n=USART3;
uart3.tx_busy=0;
}
void Usart_Tx_Start(USART_DATA *uart_data_struct,u8 data[],u8 length)
{
u8 i;
if(length>100) uart_data_struct->tx_length=100;
else uart_data_struct->tx_length=length;
for(i=0;i<uart_data_struct->tx_length;i++)
{
uart_data_struct->tx_buff[i]=data[i];
}
while(uart_data_struct->tx_busy);
uart_data_struct->tx_busy=1;
uart_data_struct->usart_n->DR = (uart_data_struct->tx_buff[0] & (uint16_t)0x01FF);
uart_data_struct->tx_numb=1;
}
void Usart_Printf(USART_DATA *uart_data_struct,char* fmt,...)
{
va_list ap;
va_start(ap,fmt);
vsprintf((char*)uart_data_struct->tx_buff,fmt,ap);
va_end(ap);
uart_data_struct->tx_length=strlen((const char*)uart_data_struct->tx_buff);
while(uart_data_struct->tx_busy);
uart_data_struct->tx_busy=1;
uart_data_struct->usart_n->DR = (uart_data_struct->tx_buff[0] & (uint16_t)0x01FF);
uart_data_struct->tx_numb=1;
}
void Usart_Tx_Irq(USART_DATA *uart_data_struct)
{
if(uart_data_struct->tx_busy)
{
USART_SendData(uart_data_struct->usart_n,uart_data_struct->tx_buff[uart_data_struct->tx_numb]);
uart_data_struct->tx_numb++;
if(uart_data_struct->tx_numb>=uart_data_struct->tx_length) uart_data_struct->tx_busy=0;
}
}
usart.h
#ifndef __USART_H
#define __USART_H
#include "my_scc.h"
typedef struct
{
USART_TypeDef *usart_n;
u8 tx_busy;
u8 tx_length;
u8 tx_numb;
u8 tx_buff[50];
}USART_DATA;
extern USART_DATA uart1;
extern USART_DATA uart2;
extern USART_DATA uart3;
void Usart_Init(void);
void Usart_Tx_Irq(USART_DATA *uart_data_struct);
void Usart_Tx_Start(USART_DATA *uart_data_struct,u8 data[],u8 length);
void Usart_Printf(USART_DATA *uart_data_struct,char* fmt,...);
#endif
具体的使用 Usart_Tx_Start
|