蓝牙(Bluetooth)
1.bsp_bluetooth.c 文件里存放的是蓝牙基础驱动文件
2.app_bluetooth.c 文件里存放的是小车应用蓝牙的文件
全局变量
| 全局变量 | 类型 | 注释 |
|---|---|---|
| g_autoup | int | 自动上报标志 |
| manydisplay | char[80] | 上报数据字符串 |
| updata | char[80] | 存储最终发送的数据 |
| lspeed | char[10] | 左电机速度字符串 |
| rspeed | char[10] | 右电机速度字符串 |
| daccel | char[10] | 加速度字符串 |
| dgyro | char[10] | 陀螺仪数据字符串 |
| csb | char[10] | 超声波距离字符串 |
| vi | char[10] | 电量字符串 |
| newLineReceived | u8 | 新行接收标志 |
| num | int | 输入字符串长度 |
| startBit | u8 | 协议开始位标志 |
| int9num | int | 第9个整数值 |
| inputString | u8[80] | 接收到的输入字符串 |
| ProtocolString | u8[80] | 协议字符串 |
| g_newcarstate | enCarState | 小车状态 |
| PID_Original | float[6] | PID初始值数组 |
| piddisplay | char[50] | PID显示字符串 |
| charkp | char[10] | 比例增益KP字符串 |
| charkd | char[10] | 微分增益KD字符串 |
| charksp | char[10] | 速度环KP字符串 |
| charksi | char[10] | 速度环KI字符串 |
| charktp | char[10] | 转向环KP字符串 |
| charktd | char[10] | 转向环KD字符串 |
宏定义
| 宏定义 | 值 | 注释 |
|---|---|---|
| run_car | '1' | 前进命令 |
| back_car | '2' | 后退命令 |
| left_car | '3' | 左转命令 |
| right_car | '4' | 右转命令 |
| stop_car | '0' | 停止命令 |
方法
bluetooth_init(void)
初始化蓝牙模块
| 返回值 | 类型 |
|---|---|
| 无 | void |
bluetooth_send_char(uint8_t ch)
发送一个字符
| 参数 | 类型 | 注释 |
|---|---|---|
| ch | uint8_t | 要发送的字符 |
| 返回值 | 类型 |
|---|---|
| 无 | void |
UART5_Send_ArrayU8(uint8_t *BufferPtr, uint16_t Length)
发送一个字节数组
| 参数 | 类型 | 注释 |
|---|---|---|
| BufferPtr | uint8_t* | 待发送的数据指针 |
| Length | uint16_t | 数据长度 |
| 返回值 | 类型 |
|---|---|
| 无 | void |
USART5_Send_Byte(unsigned char byte)
发送一个字节
| 参数 | 类型 | 注释 |
|---|---|---|
| byte | unsigned char | 要发送的字节 |
| 返回值 | 类型 |
|---|---|
| 无 | void |
bluetooth_send_string(char *s)
发送字符串
| 参数 | 类型 | 注释 |
|---|---|---|
| s | char* | 待发送的字符串 |
| 返回值 | 类型 |
|---|---|
| 无 | void |
Init_PID(void)
初始化PID参数
| 返回值 | 类型 |
|---|---|
| 无 | void |
ResetPID(void)
恢复开启时的PID参数
| 返回值 | 类型 |
|---|---|
| 无 | void |
deal_bluetooth(uint8_t rxbuf)
处理接收到的蓝牙数据
| 参数 | 类型 | 注释 |
|---|---|---|
| rxbuf | uint8_t | 接收到的数据 |
| 返回值 | 类型 |
|---|---|
| 无 | void |
ProtocolCpyData(void)
复制协议数据
| 返回值 | 类型 |
|---|---|
| 无 | void |
Protocol(void)
解析协议数据
| 返回值 | 类型 |
|---|---|
| 无 | void |
StringFind(const char *pSrc, const char *pDst)
查找子字符串
| 参数 | 类型 | 注释 |
|---|---|---|
| pSrc | const char* | 源字符串 |
| pDst | const char* | 目标字符串 |
| 返回值 | 类型 |
|---|---|
| 找到的起始索引 | int |
CalcUpData(void)
计算和更新上报数据
| 返回值 | 类型 |
|---|---|
| 无 | void |
SendAutoUp(void)
定时自动上报数据
| 返回值 | 类型 |
|---|---|
| 无 | void |
ProtocolGetPID(void)
获取PID参数
| 返回值 | 类型 |
|---|---|
| 无 | void |
使用示例
#include "bsp_bluetooth.h"
int main() {
bluetooth_init();
bluetooth_send_string("Hello Yahboom!\n");
while(1);
}
// 当蓝牙接收到消息后会触发该中断
void UART5_IRQHandler(void) {
uint8_t Rx5_Temp;
if (USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
{
Rx5_Temp = USART_ReceiveData(UART5);
bluetooth_send_char(Rx5_Temp);
}
}