Gitee

https://gitee.com/WangXi_Chn/RttOs_ModuleLib

开发环境

CubeMX
MDK5 IDE
STM32F407IGH6TR 芯片
HC06蓝牙通信模块
Android SPP蓝牙串口助手(下载见文末)
大疆开发板 C型
RT-Thread操作系统
面向对象模块接口设计

大疆M3508直流无刷电机(遥控对象)
大疆C620无刷电机调速器(遥控对象)

硬件条件

  • HC06蓝牙通信模块供电5V
  • STM32与HC06串口通信

    添加驱动

    CubeMX

  • 打开连接蓝牙模块的串口引脚

image.png

  • 注意是否需要引脚重映射,即右侧引脚使能是否正确

    Kconfig

    1. menuconfig BSP_USING_UART
    2. bool "Enable UART"
    3. default y
    4. select RT_USING_SERIAL
    5. if BSP_USING_UART
    6. config BSP_USING_UART1
    7. bool "Enable UART1"
    8. default y
    9. config BSP_USING_UART6
    10. bool "Enable UART6"
    11. default y
    12. config BSP_UART6_RX_USING_DMA
    13. bool "Enable UART6 RX DMA"
    14. depends on BSP_USING_UART6 && RT_SERIAL_USING_DMA
    15. default y
    16. endif
  • UART1为RTT操作系统命令行串口

  • 这里添加了UART6,并且使能DMA

    Env

    image.pngimage.png

image.pngimage.png

  • SCON命令生成MDK5工程

    接口函数

    见官方使用手册
    https://www.rt-thread.org/document/site/programming-manual/device/uart/uart/

    官方例程

  • 不定长帧接收,直到收到指定的结束码 ```c /*

    • 程序清单:这是一个串口设备接收不定长数据的示例代码
    • 例程导出了 uart_dma_sample 命令到控制终端
    • 命令调用格式:uart_dma_sample uart2
    • 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
    • 程序功能:通过串口 uart2 输出字符串”hello RT-Thread!”,并通过串口 uart2 输入一串字符(不定长),再通过数据解析后,使用控制台显示有效数据。 */

include

define SAMPLE_UART_NAME “uart2”

define DATA_CMD_END ‘\r’ / 结束位设置为 \r,即回车符 /

define ONE_DATA_MAXLEN 20 / 不定长数据的最大长度 /

/ 用于接收消息的信号量 / static struct rt_semaphore rx_sem; static rt_device_t serial;

/ 接收数据回调函数 / static rt_err_t uart_rx_ind(rt_device_t dev, rt_size_t size) { / 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 / if (size > 0) { rt_sem_release(&rx_sem); } return RT_EOK; }

static char uart_sample_get_char(void) { char ch;

  1. while (rt_device_read(serial, 0, &ch, 1) == 0)
  2. {
  3. rt_sem_control(&rx_sem, RT_IPC_CMD_RESET, RT_NULL);
  4. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  5. }
  6. return ch;

}

/ 数据解析线程 / static void data_parsing(void) { char ch; char data[ONE_DATA_MAXLEN]; static char i = 0;

  1. while (1)
  2. {
  3. ch = uart_sample_get_char();
  4. rt_device_write(serial, 0, &ch, 1);
  5. if(ch == DATA_CMD_END)
  6. {
  7. data[i++] = '\0';
  8. rt_kprintf("data=%s\r\n",data);
  9. i = 0;
  10. continue;
  11. }
  12. i = (i >= ONE_DATA_MAXLEN-1) ? ONE_DATA_MAXLEN-1 : i;
  13. data[i++] = ch;
  14. }

}

static int uart_data_sample(int argc, char *argv[]) { rt_err_t ret = RT_EOK; char uart_name[RT_NAME_MAX]; char str[] = “hello RT-Thread!\r\n”;

  1. if (argc == 2)
  2. {
  3. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  4. }
  5. else
  6. {
  7. rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
  8. }
  9. /* 查找系统中的串口设备 */
  10. serial = rt_device_find(uart_name);
  11. if (!serial)
  12. {
  13. rt_kprintf("find %s failed!\n", uart_name);
  14. return RT_ERROR;
  15. }
  16. /* 初始化信号量 */
  17. rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  18. /* 以中断接收及轮询发送模式打开串口设备 */
  19. rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  20. /* 设置接收回调函数 */
  21. rt_device_set_rx_indicate(serial, uart_rx_ind);
  22. /* 发送字符串 */
  23. rt_device_write(serial, 0, str, (sizeof(str) - 1));
  24. /* 创建 serial 线程 */
  25. rt_thread_t thread = rt_thread_create("serial", (void (*)(void *parameter))data_parsing, RT_NULL, 1024, 25, 10);
  26. /* 创建成功则启动线程 */
  27. if (thread != RT_NULL)
  28. {
  29. rt_thread_startup(thread);
  30. }
  31. else
  32. {
  33. ret = RT_ERROR;
  34. }
  35. return ret;

}

/ 导出到 msh 命令列表中 / MSH_CMD_EXPORT(uart_data_sample, uart device sample);

  1. <a name="E8ys4"></a>
  2. # 适配模块接口
  3. <a name="vMj9O"></a>
  4. ## Module_BlueToothHC06.h
  5. ```c
  6. /*
  7. * Copyright (c) 2020 - ~, HIT_HERO Team
  8. *
  9. * BLUETOOTH HC06 MODULE HEAD FILE
  10. * Used in RT-Thread Operate System
  11. *
  12. * Change Logs:
  13. * Date Author Notes Mail
  14. * 2020-08-09 WangXi first version WangXi_chn@foxmail.com
  15. */
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. #include <board.h>
  19. #ifndef _MODULE_BLUETOOTHHC06_H_
  20. #define _MODULE_BLUETOOTHHC06_H_
  21. typedef enum funcFlag
  22. {
  23. LED_BlUE_1 = 0xE1,
  24. LED_GREEN_1,
  25. LED_RED_1,
  26. LED_YELLOW_1,
  27. LED_BlUE_2,
  28. LED_GREEN_2,
  29. LED_RED_2,
  30. LED_YELLOW_2,
  31. PARAM_SHOW_1 = 0xC1,
  32. PARAM_SHOW_2,
  33. PARAM_SHOW_3,
  34. PARAM_SHOW_4,
  35. PARAM_SHOW_5,
  36. PARAM_SHOW_6,
  37. WAVE_SHOW = 0x01,
  38. }FuncFlag;
  39. enum ConnectStatusFlag
  40. {
  41. DISCONNECTED = PIN_LOW,
  42. CONNECTED = PIN_HIGH,
  43. };
  44. struct _MODULE_BLUETOOTHHC06
  45. {
  46. /* Property */
  47. char * Property_UartDevName;
  48. rt_base_t ConnectStatusPin;
  49. rt_uint8_t ConnectCheckSwitch;
  50. /* Value */
  51. rt_device_t Value_uart_dev;
  52. rt_uint8_t Value_sendData[8];
  53. rt_uint8_t Value_feedData[8];
  54. rt_uint32_t ConnectStatus;
  55. rt_uint16_t Value_keyMask;
  56. /* Method */
  57. void (*Method_Init)(struct _MODULE_BLUETOOTHHC06 *module);
  58. void (*Method_Send)(struct _MODULE_BLUETOOTHHC06 *module,FuncFlag funcflag,rt_int16_t param);
  59. void (*Method_Feed)(struct _MODULE_BLUETOOTHHC06 *module);
  60. void (*Method_Code)(struct _MODULE_BLUETOOTHHC06 *module,FuncFlag funcflag,rt_int16_t param);
  61. void (*Method_Encode)(struct _MODULE_BLUETOOTHHC06 *module);
  62. rt_err_t (*Method_Handle)(rt_device_t dev, rt_size_t size);
  63. };
  64. typedef struct _MODULE_BLUETOOTHHC06 MODULE_BLUETOOTHHC06;
  65. rt_err_t Module_BlueToothHC06_Config(MODULE_BLUETOOTHHC06 *module);
  66. #endif
  67. /************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/

Module_BlueToothHC06.c

  1. /*
  2. * Copyright (c) 2020 - ~, HIT_HERO Team
  3. *
  4. * BLUETOOTH HC06 MODULE SOUCE FILE
  5. * Used in RT-Thread Operate System
  6. *
  7. * Change Logs:
  8. * Date Author Notes Mail
  9. * 2020-08-09 WangXi first version WangXi_chn@foxmail.com
  10. */
  11. #include "Module_BlueToothHC06.h"
  12. static void Module_BlueToothHC06Init(MODULE_BLUETOOTHHC06 *module);
  13. static void Module_BlueToothHC06Send(MODULE_BLUETOOTHHC06 *module,FuncFlag funcflag,rt_int16_t param);
  14. static void Module_BlueToothHC06Feed(MODULE_BLUETOOTHHC06 *module);
  15. static void Module_BlueToothHC06Code(MODULE_BLUETOOTHHC06 *module,FuncFlag funcflag,rt_int16_t param);
  16. static void Module_BlueToothHC06Encode(MODULE_BLUETOOTHHC06 *module);
  17. static rt_err_t Module_BlueToothHC06Handle(rt_device_t dev, rt_size_t size);
  18. static struct rt_semaphore uartBluetoothHC06_sem;
  19. /* Global Method */
  20. rt_err_t Module_BlueToothHC06_Config(MODULE_BLUETOOTHHC06 *module)
  21. {
  22. if( module->Method_Init ==NULL &&
  23. module->Method_Handle ==NULL &&
  24. module->Method_Send ==NULL &&
  25. module->Method_Feed ==NULL &&
  26. module->Method_Code ==NULL &&
  27. module->Method_Encode ==NULL
  28. ){
  29. /* Link the Method */
  30. module->Method_Init = Module_BlueToothHC06Init;
  31. module->Method_Handle = Module_BlueToothHC06Handle;
  32. module->Method_Send = Module_BlueToothHC06Send;
  33. module->Method_Feed = Module_BlueToothHC06Feed;
  34. module->Method_Code = Module_BlueToothHC06Code;
  35. module->Method_Encode = Module_BlueToothHC06Encode;
  36. }
  37. else{
  38. rt_kprintf("Warning: Module BlueTooth HC06 is Configed twice\n");
  39. return RT_ERROR;
  40. }
  41. /* Device Init */
  42. module->Method_Init(module);
  43. return RT_EOK;
  44. }
  45. static void Module_BlueToothHC06Init(MODULE_BLUETOOTHHC06 *module)
  46. {
  47. module->Value_uart_dev = rt_device_find(module->Property_UartDevName);
  48. if (!module->Value_uart_dev)
  49. {
  50. rt_kprintf("find %s failed!\n", module->Value_uart_dev);
  51. return;
  52. }
  53. rt_sem_init(&uartBluetoothHC06_sem, "uartBluetoothHC06_sem", 0, RT_IPC_FLAG_FIFO);
  54. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */
  55. /* step2:修改串口配置参数 */
  56. config.baud_rate = BAUD_RATE_9600; //修改波特率为 9600
  57. config.data_bits = DATA_BITS_8; //数据位 8
  58. config.stop_bits = STOP_BITS_1; //停止位 1
  59. config.bufsz = 128; //修改缓冲区 buff size 为 128
  60. config.parity = PARITY_NONE; //无奇偶校验位
  61. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  62. rt_device_control(module->Value_uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
  63. rt_device_open(module->Value_uart_dev, RT_DEVICE_FLAG_DMA_RX);
  64. rt_device_set_rx_indicate(module->Value_uart_dev, module->Method_Handle);
  65. /* Enable Connect Status GPIO */
  66. rt_pin_mode(module->ConnectStatusPin, PIN_MODE_INPUT_PULLDOWN);
  67. }
  68. static void Module_BlueToothHC06Send(MODULE_BLUETOOTHHC06 *module,FuncFlag funcflag,rt_int16_t param)
  69. {
  70. module->ConnectStatus = rt_pin_read(module->ConnectStatusPin);
  71. module->Method_Code(module,funcflag,param);
  72. rt_device_write( module->Value_uart_dev, 0,
  73. &(module->Value_sendData),
  74. sizeof(module->Value_sendData));
  75. }
  76. static void Module_BlueToothHC06Feed(MODULE_BLUETOOTHHC06 *module)
  77. {
  78. char ch;
  79. static char i = 0;
  80. while (1)
  81. {
  82. while (rt_device_read(module->Value_uart_dev, 0, &ch, 1) == 0)
  83. {
  84. rt_sem_control(&uartBluetoothHC06_sem, RT_IPC_CMD_RESET, RT_NULL);
  85. rt_sem_take(&uartBluetoothHC06_sem, RT_WAITING_FOREVER);
  86. }
  87. if((rt_uint8_t)ch == 0xAA)
  88. {
  89. /* get the frame end flag and start encode the frame */
  90. module->Method_Encode(module);
  91. i = 0;
  92. continue;
  93. }
  94. i = (i >= 8-1) ? 8-1 : i;
  95. module->Value_feedData[i++] = ch;
  96. }
  97. }
  98. static rt_err_t Module_BlueToothHC06Handle(rt_device_t dev, rt_size_t size)
  99. {
  100. rt_sem_release(&uartBluetoothHC06_sem);
  101. return RT_EOK;
  102. }
  103. static void Module_BlueToothHC06Code(MODULE_BLUETOOTHHC06 *module,FuncFlag funcflag,rt_int16_t param)
  104. {
  105. module->Value_sendData[0] = 0xA5;
  106. module->Value_sendData[1] = 0x5A;
  107. switch (funcflag>>4)
  108. {
  109. case 0x0E: //LED Show
  110. module->Value_sendData[2] = 0x05;
  111. module->Value_sendData[3] = funcflag;
  112. module->Value_sendData[4] = param;
  113. module->Value_sendData[5] = (0x05 + funcflag + param)&0xFF;
  114. module->Value_sendData[6] = 0xAA;
  115. break;
  116. case 0x0C: //Param Show
  117. module->Value_sendData[2] = 0x06;
  118. module->Value_sendData[3] = funcflag;
  119. module->Value_sendData[4] = param >> 8;
  120. module->Value_sendData[5] = param & 0xFF;
  121. module->Value_sendData[6] = (0x06 + funcflag + (param>>8) + (param&0xFF))&0xFF;
  122. module->Value_sendData[7] = 0xAA;
  123. break;
  124. case 0x00: //Wave Show
  125. module->Value_sendData[2] = 0x06;
  126. module->Value_sendData[3] = funcflag;
  127. module->Value_sendData[4] = param >> 8;
  128. module->Value_sendData[5] = param & 0xFF;
  129. module->Value_sendData[6] = (0x06 + funcflag + (param>>8) + (param&0xFF))&0xFF;
  130. module->Value_sendData[7] = 0xAA;
  131. break;
  132. }
  133. }
  134. static void Module_BlueToothHC06Encode(MODULE_BLUETOOTHHC06 *module)
  135. {
  136. if((rt_uint8_t)(module->Value_feedData[0])==0xA5 && (rt_uint8_t)(module->Value_feedData[1])==0x5A)
  137. {
  138. rt_uint8_t funcFlag = (rt_uint8_t)(module->Value_feedData[3])>>4;
  139. rt_uint8_t numFlag = (rt_uint8_t)(module->Value_feedData[3])&0x0F;
  140. switch(funcFlag)
  141. {
  142. case 0x0B:
  143. module->Value_keyMask |= (0x01 << (numFlag-1));break;
  144. }
  145. }
  146. else
  147. {
  148. return;
  149. }
  150. }
  151. /************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/

使用

  1. /*
  2. * Copyright (c) 2020 - ~, HIT_HERO Team
  3. *
  4. * MAIN SOUCE FILE
  5. * Used in RT-Thread Operate System
  6. *
  7. * Change Logs:
  8. * Date Author Notes Mail
  9. * 2020-08-08 WangXi first version WangXi_chn@foxmail.com
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <board.h>
  14. #include "Module_3508MotorGroup.h"
  15. #include "Module_BlueToothHC06.h"
  16. /* Module param list ---------------------------------------------------------------------------------------- */
  17. MODULE_BLUETOOTHHC06 dev_communicate =
  18. {
  19. .Property_UartDevName = "uart6",
  20. };
  21. MODULE_MOTOR3508GROUP dev_motor3508group =
  22. {
  23. .Property_CanDevName = "can1",
  24. .Value_module_motor3508[MOTOR3508ID_1] =
  25. {
  26. .Enable = 1,
  27. .Mode = MOTOR3508MODE_SPEED,
  28. .PID_Speed =
  29. {
  30. .Property_Kp = 6,
  31. .Property_Ki = 2,
  32. .Property_Kd = 0,
  33. .Property_dt = 0.005,
  34. .Property_integralMax = 500,
  35. .Property_AimMax = 9000,
  36. .Property_OutputMax =16300
  37. }
  38. },
  39. .Value_module_motor3508[MOTOR3508ID_2] =
  40. {
  41. .Enable = 1,
  42. .Mode = MOTOR3508MODE_SPEED,
  43. .PID_Speed =
  44. {
  45. .Property_Kp = 6,
  46. .Property_Ki = 2,
  47. .Property_Kd = 0,
  48. .Property_dt = 0.005,
  49. .Property_integralMax = 500,
  50. .Property_AimMax = 9000,
  51. .Property_OutputMax =16300
  52. }
  53. },
  54. .Value_module_motor3508[MOTOR3508ID_3] =
  55. {
  56. .Enable = 1,
  57. .Mode = MOTOR3508MODE_ANGLE,
  58. .PID_Speed =
  59. {
  60. .Property_Kp = 6,
  61. .Property_Ki = 2,
  62. .Property_Kd = 0,
  63. .Property_dt = 0.005,
  64. .Property_integralMax = 500,
  65. .Property_AimMax = 9000,
  66. .Property_OutputMax =10000
  67. },
  68. .PID_Angle =
  69. {
  70. .Property_Kp = 4,
  71. .Property_Ki = 2,
  72. .Property_Kd = 0,
  73. .Property_dt = 0.005,
  74. .Property_integralMax = 500,
  75. .Property_AimMax = 7200,
  76. .Property_OutputMax =300
  77. },
  78. },
  79. };
  80. /* thread entry list ---------------------------------------------------------------------------------------- */
  81. static void motor_entry(void *parameter)
  82. {
  83. while (1)
  84. {
  85. /* Communicate with motor group */
  86. dev_motor3508group.Method_Feed(&dev_motor3508group);
  87. rt_thread_mdelay(1);
  88. dev_motor3508group.Method_Send(&dev_motor3508group);
  89. }
  90. }
  91. static void bluetupdata_entry(void *parameter)
  92. {
  93. dev_communicate.Method_Feed(&dev_communicate);
  94. }
  95. static void bluetcontrol_entry(void *parameter)
  96. {
  97. static rt_uint32_t time_1000m = 0;
  98. while(1)
  99. {
  100. rt_thread_mdelay(1);
  101. time_1000m++;
  102. switch(dev_communicate.Value_keyMask)
  103. {
  104. case (0x0001<<0): //speed up the friction wheel
  105. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_1].Value_motor_AimRPM -= 200;
  106. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_2].Value_motor_AimRPM += 200;
  107. break;
  108. case (0x0001<<1): //start up the friction wheel
  109. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_1].Value_motor_AimRPM = -1400;
  110. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_2].Value_motor_AimRPM = 1400;
  111. break;
  112. case (0x0001<<2): //slow down the friction wheel
  113. if(dev_motor3508group.Value_module_motor3508[MOTOR3508ID_2].Value_motor_AimRPM >= 200)
  114. {
  115. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_1].Value_motor_AimRPM += 200;
  116. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_2].Value_motor_AimRPM -= 200;
  117. }
  118. break;
  119. case (0x0001<<3): //raise up the paw
  120. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_Kp = 4;
  121. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_Ki = 2;
  122. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_Kd = 0;
  123. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_dt = 0.005;
  124. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_integralMax = 500;
  125. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_AimMax = 7200;
  126. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_OutputMax = 300;
  127. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Speed.Property_OutputMax = 10000;
  128. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].Value_motor_AimAngle = -720;
  129. break;
  130. case (0x0001<<4): //stop the friction wheel
  131. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_1].Value_motor_AimRPM = 0;
  132. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_2].Value_motor_AimRPM = 0;
  133. break;
  134. case (0x0001<<5): //put down the paw
  135. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_Kp = 0.05;
  136. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_Ki = 0.0001;
  137. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_Kd = 0;
  138. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_dt = 0.005;
  139. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_integralMax = 500;
  140. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_AimMax = 7200;
  141. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Angle.Property_OutputMax = 100;
  142. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].PID_Speed.Property_OutputMax = 100;
  143. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_3].Value_motor_AimAngle = 0;
  144. break;
  145. case (0x0001<<6):
  146. break;
  147. case (0x0001<<7):
  148. break;
  149. case (0x0001<<8):
  150. break;
  151. case (0x0001<<9):
  152. break;
  153. case (0x0001<<10):
  154. break;
  155. case (0x0001<<11):
  156. break;
  157. }
  158. dev_communicate.Value_keyMask = 0x0000;
  159. if(time_1000m >= 1000)
  160. {
  161. time_1000m = 0;
  162. dev_communicate.Method_Send(&dev_communicate,PARAM_SHOW_1,
  163. dev_motor3508group.Value_module_motor3508[MOTOR3508ID_2].Value_motor_AimRPM/200);
  164. }
  165. }
  166. }
  167. int main(void)
  168. {
  169. /* Config module ---------------------------------------------------------------------------------------- */
  170. Module_Motor3508Group_Config(&dev_motor3508group);
  171. Module_BlueToothHC06_Config(&dev_communicate);
  172. /* Enable task thread ---------------------------------------------------------------------------------------- */
  173. /* motor control thread */
  174. rt_thread_t motor_thread = rt_thread_create("motor", motor_entry, RT_NULL,
  175. 1024, RT_THREAD_PRIORITY_MAX - 2, 20);
  176. if (motor_thread != RT_NULL){
  177. rt_thread_startup(motor_thread);
  178. }
  179. /* blue tooth updata thread */
  180. rt_thread_t bluetupdata_thread = rt_thread_create("bluetupdata", bluetupdata_entry, RT_NULL,
  181. 512, RT_THREAD_PRIORITY_MAX - 5, 20);
  182. if (bluetupdata_thread != RT_NULL){
  183. rt_thread_startup(bluetupdata_thread);
  184. }
  185. /* blue tooth updata thread */
  186. rt_thread_t bluetcontrol_thread = rt_thread_create("bluetcontrol", bluetcontrol_entry, RT_NULL,
  187. 512, RT_THREAD_PRIORITY_MAX - 5, 20);
  188. if (bluetcontrol_thread != RT_NULL){
  189. rt_thread_startup(bluetcontrol_thread);
  190. }
  191. }
  192. /************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/

说明

  • 配合Android手机蓝牙串口助手SPP使用
  • 模块内部已集成遵循软件协议的编码和解码
  • 使用示例中,已实现通过按键调节电机转速,并每一秒一次返回电机转速
  • 按键的使能中也参考了掩码的思想,通过读取一个16位数据的每一位,判断哪个按键被按下
  • 最终留出的函数接口是三个
    • 初始化模块config(全局函数)
    • 发送指令Send(成员函数,即方法)
    • 读取指令Feed(成员函数)
  • 发送指令的参数有三个
    • 模块自身指针
    • 发送指令的功能(用枚举限定,对应手机端的LED、参数、示波)
    • 发送参数(LED仅1和0,其他功能填写想呈现的数据)
  • 现已可实现波形显示功能 2020-8-12
  • 修复了参数回显功能 2020-8-12

    手机SPP下载

    BlueToothSerier.rar

  • 尽可能不要修改手机端默认协议以保持一致