Rd03:具体思路
这个模块测试要上墙,我做了一个上墙的杆子
测试的视频
WeChat_20240427151935
Rd03d的测试思路
硬件连接
软件思路
我这个蓝牙的波特率最大是115200,而Rd03d波特率最低是256000,所以要开两个USART,一个以256000的波特率进行接受数据,另一个以115200的波特率连接蓝牙把接收到的数据发送给电脑端
代码如下
"main.c"
#include "stm32f10x.h"
#include "Serial.h"
//目标1的信息
int16_t Target1_XPos=0;
int16_t Target1_YPos=0;
int16_t Target1_Speed=0;
int16_t Target1_Rate=0;
//目标2的信息
int16_t Target2_XPos=0;
int16_t Target2_YPos=0;
int16_t Target2_Speed=0;
int16_t Target2_Rate=0;
//目标3的信息
int16_t Target3_XPos=0;
int16_t Target3_YPos=0;
int16_t Target3_Speed=0;
int16_t Target3_Rate=0;
#pragma pack (1)
typedef struct{
short x;
short y;
short v;
short d;
}PersonInfo;
typedef struct{
union{
unsigned char data[4];
unsigned int data4;
}head;
PersonInfo person[3];
// unsigned char tail[2];
} Info;
#pragma pack()
//将数组Buffer里的数据解析成十进制数据
void Translat_Rd05RegDate()
{
if(BuffFullFlag==1)
{
Info * pInfo = (Info*)Buffer;
Target1_XPos =pInfo->person[0].x;
Target1_YPos =pInfo->person[0].y;
Target1_Speed=pInfo->person[0].v;
Target1_Rate =pInfo->person[0].d;
Target2_XPos =pInfo->person[1].x;
Target2_YPos =pInfo->person[1].y;
Target2_Speed=pInfo->person[1].v;
Target2_Rate =pInfo->person[1].d;
Target3_XPos =pInfo->person[2].x;
Target3_YPos =pInfo->person[2].y;
Target3_Speed=pInfo->person[2].v;
Target3_Rate =pInfo->person[2].d;
}
}
void Show_Rd03d_Data()
{
Translat_Rd05RegDate();
if (BuffFullFlag == 1)//判断数组是否满了
{
Serial_Printf("***************Rd05 Data***************:\n");
Serial_Printf("Target1_XPos=%d\n",Target1_XPos);
Serial_Printf("Target1_YPos=%d\n",Target1_YPos);
Serial_Printf("Target1_Speed=%d\n",Target1_Speed);
Serial_Printf("Target1_Rate=%d\n",Target1_Rate);
Serial_Printf("Target2_XPos=%d\n",Target2_XPos);
Serial_Printf("Target2_YPos=%d\n",Target2_YPos);
Serial_Printf("Target2_Speed=%d\n",Target2_Speed);
Serial_Printf("Target2_Rate=%d\n",Target2_Rate);
Serial_Printf("Target3_XPos=%d\n",Target3_XPos);
Serial_Printf("Target3_YPos=%d\n",Target3_YPos);
Serial_Printf("Target3_Speed=%d\n",Target3_Speed);
Serial_Printf("Target3_Rate=%d\n",Target3_Rate);
Serial_Printf("**************Buffer Data*************:\n");
for(int i=0;i<30;i++)
{
Serial_Printf("Buffer[%d]=%x",i,Buffer[i]);
Serial_Printf("\n");
}
Serial_Printf("\n\n");
BuffFullFlag = 0;
}
}
void main()
{
OLED_Init(); //OLED初始化
Serial_Init(); //串口初始化
while (1)
{
Show_Rd03d_Data();
}
}
"Serial.c"
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
uint8_t BuffFullFlag=0;
uint8_t pData=0;
uint8_t Buffer[30];//包头+数据+包尾
/**
* 函 数:串口初始化
* 参 数:无
* 返 回 值:无
*/
void Serial_Init(void)
{
/*开启时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //开启USART1的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启GPIOA的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//USART1 GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA9引脚初始化为复用推挽输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA10引脚初始化为上拉输入
//USART2 GPIO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA9引脚初始化为复用推挽输出
/*USART1初始化*/
USART_InitTypeDef USART_InitStructure1; //定义结构体变量
USART_InitStructure1.USART_BaudRate = 256000; //波特率
USART_InitStructure1.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制,不需要
USART_InitStructure1.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //模式,发送模式和接收模式均选择
USART_InitStructure1.USART_Parity = USART_Parity_No; //奇偶校验,不需要
USART_InitStructure1.USART_StopBits = USART_StopBits_1; //停止位,选择1位
USART_InitStructure1.USART_WordLength = USART_WordLength_8b; //字长,选择8位
USART_Init(USART1, &USART_InitStructure1); //将结构体变量交给USART_Init,配置USART1
/*USART2初始化*/
USART_InitTypeDef USART_InitStructure2; //定义结构体变量
USART_InitStructure2.USART_BaudRate = 115200; //波特率
USART_InitStructure2.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制,不需要
USART_InitStructure2.USART_Mode = USART_Mode_Tx; //模式
USART_InitStructure2.USART_Parity = USART_Parity_No; //奇偶校验,不需要
USART_InitStructure2.USART_StopBits = USART_StopBits_1; //停止位,选择1位
USART_InitStructure2.USART_WordLength = USART_WordLength_8b; //字长,选择8位
USART_Init(USART2, &USART_InitStructure2); //将结构体变量交给USART_Init,配置USART1
/*中断输出配置*/
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启串口接收数据的中断
/*NVIC中断分组*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //配置NVIC为分组2
/*NVIC配置*/
NVIC_InitTypeDef NVIC_InitStructure1; //定义结构体变量
NVIC_InitStructure1.NVIC_IRQChannel = USART1_IRQn; //选择配置NVIC的USART1线
NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE; //指定NVIC线路使能
NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority = 1; //指定NVIC线路的抢占优先级为1
NVIC_InitStructure1.NVIC_IRQChannelSubPriority = 1; //指定NVIC线路的响应优先级为1
NVIC_Init(&NVIC_InitStructure1); //将结构体变量交给NVIC_Init,配置NVIC外设
/*USART使能*/
USART_Cmd(USART1, ENABLE); //使能USART1,串口开始运行
USART_Cmd(USART2, ENABLE); //使能USART1,串口开始运行
}
void Serial_SendByte(uint8_t Byte)
{
USART_SendData(USART2, Byte); //将字节数据写入数据寄存器,写入后USART自动生成时序波形
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); //等待发送完成
/*下次写入数据寄存器会自动清除发送完成标志位,故此循环后,无需清除标志位*/
}
void Serial_SendString(char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i ++)//遍历字符数组(字符串),遇到字符串结束标志位后停止
{
Serial_SendByte(String[i]); //依次调用Serial_SendByte发送每个字节数据
}
}
int fputc(int ch, FILE *f)
{
Serial_SendByte(ch); //将printf的底层重定向到自己的发送字节函数
return ch;
}
void Serial_Printf(char *format, ...)
{
char String[100]; //定义字符数组
va_list arg; //定义可变参数列表数据类型的变量arg
va_start(arg, format); //从format开始,接收参数列表到arg变量
vsprintf(String, format, arg); //使用vsprintf打印格式化字符串和参数列表到字符数组中
va_end(arg); //结束变量arg
Serial_SendString(String); //串口发送字符数组(字符串)
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //判断是否是USART1的接收事件触发的中断
{
Buffer[pData]= USART_ReceiveData(USART1); //读取数据寄存器,存放在接收的数据变量
pData++;
if(pData==30&&BuffFullFlag==0)
{
pData=0;
BuffFullFlag=1;
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除USART1的RXNE标志位
//读取数据寄存器会自动清除此标志位 //如果已经读取了数据寄存器,也可以不执行此代码
}
}
"Serial.h"
#ifndef __SERIAL_H
#define __SERIAL_H
#include <stdio.h>
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendString(char *String);
void Serial_Printf(char *format, ...);
extern uint8_t pData;
extern uint8_t BuffFullFlag;
extern uint8_t Buffer[30];
#endif
最后解析的数据