当前位置:   article > 正文

简单的STM32蓝牙遥控小车完整项目及资料分享,超全_蓝牙小车

蓝牙小车

自学新手的第一个项目,做的时也在论坛查了许多大佬的文章,但还是有许多疑问,我就从一个小白的角度出发来和大家分享,应该对许多自学不久的萌新来说比较友好易懂。欢迎大家交流,大佬轻喷~~

简单的蓝牙小车,目前就只是手机控制小车前进转弯后退刹车这种功能,自己还在扩展···

PS:软件及接线图、原理图、芯片手册等资料都打包在文章末尾的下载链接中!!!

做好了的样子↓,遥控运行的视频:基于stm32的简单蓝牙小车_哔哩哔哩_bilibili真真正正的小白,刚接触这这方面自学不久,分享下第一个简单项目,虽简单但还是很有成就感的,自己找资料前前后后弄了三天吧。大佬勿喷,可以交流分享经验,分享项目。硬件部分:12v点电池DC-DC12V转3.3V模块TB6612模块x2stm32f103c8t6最小单片机系统hc-08蓝牙模块淘宝买的小车本体连带马达的具体代码和硬件资料还有详细的接线图可以私聊我icon-default.png?t=N7T8https://www.bilibili.com/video/BV1Hg411K7md/

1:材料

车体、4个轮子、4个减速马达这一套,淘宝搜智能小车关键字就有卖;

12V锂电池x1;

电源开关x1(有个开关方便点);

STM32F103C8T6最小系统单片机x1;

JLINK烧写器 x1;

TB6612FNG直流电机驱动模块x2(一块能控制2个马达,我做的是四驱,所以要两块);

HC-08蓝牙4.0模块x1(我是苹果手机,只支持4.0,安卓手机没用过,应该HC-05蓝牙2.0也能  用,看自己情况定,代码没区别通用);

手机x1(苹果蓝牙控制app:HackerRemote 要6元买;安卓这类带按键控制界面的蓝牙app比较多,自己找下吧~)

DC-DC 12V转3.3V模块x1;

母-母、公-母、公-公杜邦线若干(我自己接的话消耗量按左到右顺序递减);

作业工具(烙铁重要,胶带双面胶等按自己需要买吧)~~;

下面放上淘宝购物清单(防止被以为打广告,店名不放)↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

2.接线图及单片机原理图(下面2图+芯片的数据手册看就很清楚)

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

PS:下图TB6612模块的STBY脚接线忘记画了,STBY是使能/失能模块工作,可以直接3.3V或者接IO口置高电平就能工作,我自己是直接接的3.3V。“3.TB6612电机驱动模块介绍”的图中有标注。

重点,千万别漏了,好多人漏看了这一句话,车不动,也怪我接线图里忘记画了

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

 PS:软件及接线图、原理图、芯片手册等资料都打包在文章末尾的下载链接中!!!

 3.TB6612电机驱动模块介绍

 真值表:

 四个电机,需要用到2个模块,另一个同理~~

4.HC-08蓝牙4.0模块介绍

 这个模块就不多说了,买到手直接用,不需要做配置,我们只需接中间四个引脚(参考接线图)。有兴趣的自行百度或者看我附件里的手册资料。

 PS:软件及接线图、原理图、芯片手册等资料都打包在文章末尾的下载链接中!!!

5.部分代码(完整代码下载文末附件)

1.main.c部分,比较简单

  1. int main(void)
  2. {
  3. TB6612_FR_Init(); //初始化TB6612模块1和模块2
  4. MotorAllOFF(); //单片机上电默认先把4个电机关闭
  5. USART3_Config(); //初始化蓝牙模块的串口
  6. while(1)
  7. {
  8. RUN_Prg(); //小车蓝牙控制功能
  9. }
  10. }

2.串口通讯部分

bsp_usart.c

  1. #include "bsp_usart.h"
  2. static void USART3_NVIC_Config(void)
  3. {
  4. NVIC_InitTypeDef NVIC_InitStructure;
  5. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  6. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  7. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  8. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  9. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  10. NVIC_Init(&NVIC_InitStructure);
  11. }
  12. void USART3_Config(void)
  13. {
  14. GPIO_InitTypeDef GPIO_InitStructure;
  15. USART_InitTypeDef USART_InitStructure;
  16. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIO时钟
  17. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3时钟
  18. /*TX_GPIO*/
  19. GPIO_InitStructure.GPIO_Pin = USART3_GPIO_TX_PIN;
  20. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  21. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  22. GPIO_Init(USART3_GPIO_TX_PORT, &GPIO_InitStructure);
  23. /*RX_GPIO*/
  24. GPIO_InitStructure.GPIO_Pin = USART3_GPIO_RX_PIN;
  25. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  26. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27. GPIO_Init(USART3_GPIO_RX_PORT, &GPIO_InitStructure);
  28. /*USART3*/
  29. USART_InitStructure.USART_BaudRate = 9600;
  30. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  31. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  32. USART_InitStructure.USART_Parity = USART_Parity_No;
  33. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  34. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  35. USART_Init(USART3, &USART_InitStructure);
  36. USART3_NVIC_Config();
  37. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //使能串口接收终断
  38. USART_Cmd(USART3, ENABLE); //使能串口
  39. }

bsp_usart.h

  1. #ifndef __BSP_USART_H
  2. #define __BSP_USART_H
  3. #include "stm32f10x.h"
  4. /*TX的GPIO端口定义*/
  5. #define USART3_GPIO_TX_PIN GPIO_Pin_10
  6. #define USART3_GPIO_TX_PORT GPIOB
  7. #define USART3_GPIO_TX_CLK RCC_APB2Periph_GPIOB
  8. /*RX的GPIO端口定义*/
  9. #define USART3_GPIO_RX_PIN GPIO_Pin_11
  10. #define USART3_GPIO_RX_PORT GPIOB
  11. #define USART3_GPIO_RX_CLK RCC_APB2Periph_GPIOB
  12. /*USART3定义*/
  13. #define DEBUG_USARTx_CLK RCC_APB1Periph_USART3
  14. void USART3_Config(void);
  15. void USART3_Prg(void);
  16. #endif /*__BSP_USART_H*/

3.TB6612电机驱动模块部分

bsp_tb6612.c

  1. #include "bsp_tb6612.h"
  2. /*TB6612模块1,驱动前轮 F*/
  3. void TB6612_F_GPIO_Config(void)
  4. {
  5. GPIO_InitTypeDef GPIO_InitStructure;
  6. RCC_APB2PeriphClockCmd(TB6612_F_GPIO_AIN1_CLK | TB6612_F_GPIO_AIN2_CLK | \
  7. TB6612_F_GPIO_BIN1_CLK | TB6612_F_GPIO_BIN2_CLK , ENABLE);
  8. GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_AIN1_PIN;
  9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(TB6612_F_GPIO_AIN1_PORT, &GPIO_InitStructure);
  12. GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_AIN2_PIN;
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  14. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  15. GPIO_Init(TB6612_F_GPIO_AIN2_PORT, &GPIO_InitStructure);
  16. GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_BIN1_PIN;
  17. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  18. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  19. GPIO_Init(TB6612_F_GPIO_BIN1_PORT, &GPIO_InitStructure);
  20. GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_BIN2_PIN;
  21. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  22. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  23. GPIO_Init(TB6612_F_GPIO_BIN2_PORT, &GPIO_InitStructure);
  24. }
  25. void TB6612_F_PWM_GPIO_Config(void)
  26. {
  27. GPIO_InitTypeDef GPIO_InitStructure;
  28. RCC_APB2PeriphClockCmd(TB6612_F_GPIO_PWMA_CH1_CLK, ENABLE);
  29. GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_PWMA_CH1_PIN;
  30. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  31. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  32. GPIO_Init(TB6612_F_GPIO_PWMA_CH1_PORT,&GPIO_InitStructure);
  33. RCC_APB2PeriphClockCmd(TB6612_F_GPIO_PWMB_CH2_CLK, ENABLE);
  34. GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_PWMB_CH2_PIN;
  35. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  36. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  37. GPIO_Init(TB6612_F_GPIO_PWMB_CH2_PORT,&GPIO_InitStructure);
  38. }
  39. /*TB6612模块2,驱动后轮 R*/
  40. void TB6612_R_GPIO_Config(void)
  41. {
  42. GPIO_InitTypeDef GPIO_InitStructure;
  43. RCC_APB2PeriphClockCmd(TB6612_R_GPIO_AIN1_CLK | TB6612_R_GPIO_AIN2_CLK | \
  44. TB6612_R_GPIO_BIN1_CLK | TB6612_R_GPIO_BIN2_CLK , ENABLE);
  45. GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_AIN1_PIN;
  46. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  47. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  48. GPIO_Init(TB6612_R_GPIO_AIN1_PORT, &GPIO_InitStructure);
  49. GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_AIN2_PIN;
  50. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  51. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  52. GPIO_Init(TB6612_R_GPIO_AIN2_PORT, &GPIO_InitStructure);
  53. GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_BIN1_PIN;
  54. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  55. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  56. GPIO_Init(TB6612_R_GPIO_BIN1_PORT, &GPIO_InitStructure);
  57. GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_BIN2_PIN;
  58. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  59. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  60. GPIO_Init(TB6612_R_GPIO_BIN2_PORT, &GPIO_InitStructure);
  61. }
  62. void TB6612_R_PWM_GPIO_Config(void)
  63. {
  64. GPIO_InitTypeDef GPIO_InitStructure;
  65. RCC_APB2PeriphClockCmd(TB6612_R_GPIO_PWMA_CH3_CLK, ENABLE);
  66. GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_PWMA_CH3_PIN;
  67. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  68. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  69. GPIO_Init(TB6612_R_GPIO_PWMA_CH3_PORT,&GPIO_InitStructure);
  70. RCC_APB2PeriphClockCmd(TB6612_R_GPIO_PWMB_CH4_CLK, ENABLE);
  71. GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_PWMB_CH4_PIN;
  72. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  73. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  74. GPIO_Init(TB6612_R_GPIO_PWMB_CH4_PORT,&GPIO_InitStructure);
  75. }
  76. static void ADVANCE_TIM1_Mode_Config(void)
  77. {
  78. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  79. TIM_OCInitTypeDef TIM_OCInitStructure;
  80. RCC_APB2PeriphClockCmd(ADVANCE_TIM_CLK , ENABLE);
  81. TIM_TimeBaseInitStructure.TIM_Prescaler = (72-1);
  82. TIM_TimeBaseInitStructure.TIM_Period = (100-1);
  83. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  84. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  85. TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  86. TIM_TimeBaseInit(ADVANCE_TIM, &TIM_TimeBaseInitStructure);
  87. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  88. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  89. TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  90. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  91. TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  92. TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  93. TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  94. TIM_OCInitStructure.TIM_Pulse = 0;
  95. TIM_OC1Init(ADVANCE_TIM, &TIM_OCInitStructure);
  96. TIM_OC1PreloadConfig(ADVANCE_TIM,TIM_OCPreload_Enable);
  97. TIM_OCInitStructure.TIM_Pulse = 0;
  98. TIM_OC2Init(ADVANCE_TIM, &TIM_OCInitStructure);
  99. TIM_OC2PreloadConfig(ADVANCE_TIM,TIM_OCPreload_Enable);
  100. TIM_OCInitStructure.TIM_Pulse = 0;
  101. TIM_OC3Init(ADVANCE_TIM, &TIM_OCInitStructure);
  102. TIM_OC3PreloadConfig(ADVANCE_TIM,TIM_OCPreload_Enable);
  103. TIM_OCInitStructure.TIM_Pulse = 0;
  104. TIM_OC4Init(ADVANCE_TIM, &TIM_OCInitStructure);
  105. TIM_OC4PreloadConfig(ADVANCE_TIM,TIM_OCPreload_Enable);
  106. TIM_Cmd(ADVANCE_TIM, ENABLE);
  107. }
  108. //void TB6612_F_Init(void) //控制前面2轮的电机驱动模块初始化
  109. //{
  110. // TB6612_F_GPIO_Config();
  111. // TB6612_F_PWM_GPIO_Config();
  112. // ADVANCE_TIM1_Mode_Config();
  113. //}
  114. //void TB6612_R_Init(void) //控制后面2轮的电机驱动模块初始化
  115. //{
  116. // TB6612_R_GPIO_Config();
  117. // TB6612_R_PWM_GPIO_Config();
  118. // ADVANCE_TIM1_Mode_Config();
  119. //}
  120. void TB6612_FR_Init(void) //控制前和后2轮的电机驱动模块初始化
  121. {
  122. TB6612_F_GPIO_Config();
  123. TB6612_F_PWM_GPIO_Config();
  124. TB6612_R_GPIO_Config();
  125. TB6612_R_PWM_GPIO_Config();
  126. ADVANCE_TIM1_Mode_Config();
  127. }

bsp_tb6612.h

  1. #ifndef __BSP_TB6612_H
  2. #define __BSP_TB6612_H
  3. #include "stm32f10x.h"
  4. /*TB6612模块1*/
  5. /*定义TB6612模块1(驱动前轮(_F))的AIN1、AIN2、BIN1、BIN2的GPIO端口*/
  6. #define TB6612_F_GPIO_AIN1_PIN GPIO_Pin_12
  7. #define TB6612_F_GPIO_AIN1_PORT GPIOB
  8. #define TB6612_F_GPIO_AIN1_CLK RCC_APB2Periph_GPIOB
  9. #define TB6612_F_GPIO_AIN2_PIN GPIO_Pin_13
  10. #define TB6612_F_GPIO_AIN2_PORT GPIOB
  11. #define TB6612_F_GPIO_AIN2_CLK RCC_APB2Periph_GPIOB
  12. #define TB6612_F_GPIO_BIN1_PIN GPIO_Pin_14
  13. #define TB6612_F_GPIO_BIN1_PORT GPIOB
  14. #define TB6612_F_GPIO_BIN1_CLK RCC_APB2Periph_GPIOB
  15. #define TB6612_F_GPIO_BIN2_PIN GPIO_Pin_15
  16. #define TB6612_F_GPIO_BIN2_PORT GPIOB
  17. #define TB6612_F_GPIO_BIN2_CLK RCC_APB2Periph_GPIOB
  18. /*定义TB6612模块1(驱动前轮(_F))的PWMA、PWMB的GPIO端口*/
  19. #define TB6612_F_GPIO_PWMA_CH1_PIN GPIO_Pin_8
  20. #define TB6612_F_GPIO_PWMA_CH1_PORT GPIOA
  21. #define TB6612_F_GPIO_PWMA_CH1_CLK RCC_APB2Periph_GPIOA
  22. #define TB6612_F_GPIO_PWMB_CH2_PIN GPIO_Pin_9
  23. #define TB6612_F_GPIO_PWMB_CH2_PORT GPIOA
  24. #define TB6612_F_GPIO_PWMB_CH2_CLK RCC_APB2Periph_GPIOA
  25. /*TB6612模块2*/
  26. /*定义TB6612模块2(驱动后轮(_R))的AIN1、AIN2、BIN1、BIN2的GPIO端口*/
  27. #define TB6612_R_GPIO_AIN1_PIN GPIO_Pin_6
  28. #define TB6612_R_GPIO_AIN1_PORT GPIOB
  29. #define TB6612_R_GPIO_AIN1_CLK RCC_APB2Periph_GPIOB
  30. #define TB6612_R_GPIO_AIN2_PIN GPIO_Pin_7
  31. #define TB6612_R_GPIO_AIN2_PORT GPIOB
  32. #define TB6612_R_GPIO_AIN2_CLK RCC_APB2Periph_GPIOB
  33. #define TB6612_R_GPIO_BIN1_PIN GPIO_Pin_8
  34. #define TB6612_R_GPIO_BIN1_PORT GPIOB
  35. #define TB6612_R_GPIO_BIN1_CLK RCC_APB2Periph_GPIOB
  36. #define TB6612_R_GPIO_BIN2_PIN GPIO_Pin_9
  37. #define TB6612_R_GPIO_BIN2_PORT GPIOB
  38. #define TB6612_R_GPIO_BIN2_CLK RCC_APB2Periph_GPIOB
  39. /*定义TB6612模块2(驱动后轮(_R))的PWMA、PWMB的GPIO端口*/
  40. #define TB6612_R_GPIO_PWMA_CH3_PIN GPIO_Pin_10
  41. #define TB6612_R_GPIO_PWMA_CH3_PORT GPIOA
  42. #define TB6612_R_GPIO_PWMA_CH3_CLK RCC_APB2Periph_GPIOA
  43. #define TB6612_R_GPIO_PWMB_CH4_PIN GPIO_Pin_11
  44. #define TB6612_R_GPIO_PWMB_CH4_PORT GPIOA
  45. #define TB6612_R_GPIO_PWMB_CH4_CLK RCC_APB2Periph_GPIOA
  46. /*高级定时器TIM1参数定义*/
  47. #define ADVANCE_TIM TIM1
  48. #define ADVANCE_TIM_CLK RCC_APB2Periph_TIM1
  49. void TB6612_F_GPIO_Config(void);
  50. void TB6612_F_PWM_GPIO_Config(void);
  51. void TB6612_R_GPIO_Config(void);
  52. void TB6612_R_PWM_GPIO_Config(void);
  53. //void TB6612_F_Init(void);
  54. //void TB6612_R_Init(void);
  55. void TB6612_FR_Init(void);
  56. #endif /*BSP_TB6612_H*/

4.马达动作部分

bsp_motor.c

  1. #include "bsp_motor.h"
  2. //马达控制函数,第一个参数为选定哪个马达,第二个参数是选定马达状态
  3. void MotorCtrl(uint32_t motornum,uint32_t state)
  4. {
  5. if(motornum == LEFT_FRONT) //左前马达1
  6. {
  7. switch(state)
  8. {
  9. case OFF:
  10. F_AIN1(LOW);
  11. F_AIN2(LOW);
  12. break;
  13. case FORWARD:
  14. F_AIN1(HIGH);
  15. F_AIN2(LOW);
  16. break;
  17. case REVERSE:
  18. F_AIN1(LOW);
  19. F_AIN2(HIGH);
  20. break;
  21. case BRAKE:
  22. F_AIN1(HIGH);
  23. F_AIN2(HIGH);
  24. break;
  25. default:
  26. break;
  27. }
  28. }
  29. else if(motornum == RIGHT_FRONT) //右前马达2
  30. {
  31. switch(state)
  32. {
  33. case OFF:
  34. F_BIN1(LOW);
  35. F_BIN2(LOW);
  36. break;
  37. case FORWARD:
  38. F_BIN1(HIGH);
  39. F_BIN2(LOW);
  40. break;
  41. case REVERSE:
  42. F_BIN1(LOW);
  43. F_BIN2(HIGH);
  44. break;
  45. case BRAKE:
  46. F_BIN1(HIGH);
  47. F_BIN2(HIGH);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. else if(motornum == LEFT_REAR) //左后马达3
  54. {
  55. switch(state)
  56. {
  57. case OFF:
  58. R_AIN1(LOW);
  59. R_AIN2(LOW);
  60. break;
  61. case FORWARD:
  62. R_AIN1(HIGH);
  63. R_AIN2(LOW);
  64. break;
  65. case REVERSE:
  66. R_AIN1(LOW);
  67. R_AIN2(HIGH);
  68. break;
  69. case BRAKE:
  70. R_AIN1(HIGH);
  71. R_AIN2(HIGH);
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. else if(motornum == RIGHT_REAR) //右后马达4
  78. {
  79. switch(state)
  80. {
  81. case OFF:
  82. R_BIN1(LOW);
  83. R_BIN2(LOW);
  84. break;
  85. case FORWARD:
  86. R_BIN1(HIGH);
  87. R_BIN2(LOW);
  88. break;
  89. case REVERSE:
  90. R_BIN1(LOW);
  91. R_BIN2(HIGH);
  92. break;
  93. case BRAKE:
  94. R_BIN1(HIGH);
  95. R_BIN2(HIGH);
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. }
  102. /*马达全部关闭函数*/
  103. void MotorAllOFF(void)
  104. {
  105. MotorCtrl(LEFT_FRONT,OFF);
  106. MotorCtrl(LEFT_REAR,OFF);
  107. MotorCtrl(RIGHT_FRONT,OFF);
  108. MotorCtrl(RIGHT_REAR,OFF);
  109. }

bsp_motor.h

  1. #ifndef __BSP_MOTOR_H
  2. #define __BSP_MOTOR_H
  3. #include "bsp_tb6612.h"
  4. /*H桥两端电平高低控制*/
  5. #define HIGH 1
  6. #define LOW 0
  7. /*TB6612模块1,驱动前轮 F*/
  8. /*AIN1*/
  9. #define F_AIN1(a) if(a) GPIO_SetBits(TB6612_F_GPIO_AIN1_PORT, TB6612_F_GPIO_AIN1_PIN); \
  10. else GPIO_ResetBits(TB6612_F_GPIO_AIN1_PORT, TB6612_F_GPIO_AIN1_PIN)
  11. /*AIN2*/
  12. #define F_AIN2(a) if(a) GPIO_SetBits(TB6612_F_GPIO_AIN2_PORT, TB6612_F_GPIO_AIN2_PIN); \
  13. else GPIO_ResetBits(TB6612_F_GPIO_AIN2_PORT, TB6612_F_GPIO_AIN2_PIN)
  14. /*BIN1*/
  15. #define F_BIN1(a) if(a) GPIO_SetBits(TB6612_F_GPIO_BIN1_PORT, TB6612_F_GPIO_BIN1_PIN); \
  16. else GPIO_ResetBits(TB6612_F_GPIO_BIN1_PORT, TB6612_F_GPIO_BIN1_PIN)
  17. /*BIN2*/
  18. #define F_BIN2(a) if(a) GPIO_SetBits(TB6612_F_GPIO_BIN2_PORT, TB6612_F_GPIO_BIN2_PIN); \
  19. else GPIO_ResetBits(TB6612_F_GPIO_BIN2_PORT, TB6612_F_GPIO_BIN2_PIN)
  20. /*TB6612模块2,驱动后轮 R*/
  21. /*AIN1*/
  22. #define R_AIN1(a) if(a) GPIO_SetBits(TB6612_R_GPIO_AIN1_PORT, TB6612_R_GPIO_AIN1_PIN); \
  23. else GPIO_ResetBits(TB6612_R_GPIO_AIN1_PORT, TB6612_R_GPIO_AIN1_PIN)
  24. /*AIN2*/
  25. #define R_AIN2(a) if(a) GPIO_SetBits(TB6612_R_GPIO_AIN2_PORT, TB6612_R_GPIO_AIN2_PIN); \
  26. else GPIO_ResetBits(TB6612_R_GPIO_AIN2_PORT, TB6612_R_GPIO_AIN2_PIN)
  27. /*BIN1*/
  28. #define R_BIN1(a) if(a) GPIO_SetBits(TB6612_R_GPIO_BIN1_PORT, TB6612_R_GPIO_BIN1_PIN); \
  29. else GPIO_ResetBits(TB6612_R_GPIO_BIN1_PORT, TB6612_R_GPIO_BIN1_PIN)
  30. /*BIN2*/
  31. #define R_BIN2(a) if(a) GPIO_SetBits(TB6612_R_GPIO_BIN2_PORT, TB6612_R_GPIO_BIN2_PIN); \
  32. else GPIO_ResetBits(TB6612_R_GPIO_BIN2_PORT, TB6612_R_GPIO_BIN2_PIN)
  33. /*MotorCtrl(uint32_t motornum,uint32_t state)函数的参数宏定义*/
  34. #define LEFT_FRONT 1 //马达1(AIN、左前轮)
  35. #define RIGHT_FRONT 2 //马达2(BIN、右前轮)
  36. #define LEFT_REAR 3 //马达3(AIN、左后轮)
  37. #define RIGHT_REAR 4 //马达4(BIN、右后轮)
  38. #define OFF 0 //停止转动
  39. #define FORWARD 1 //正转
  40. #define REVERSE 2 //反转
  41. #define BRAKE 3 //制动
  42. void MotorCtrl(uint32_t motornum,uint32_t state);
  43. void MotorAllOFF(void);
  44. #endif /*__BSP_MOTOR_H*/

5.接收中断部分

stm32f10x_it.h

  1. #include "bsp_usart.h"
  2. uint8_t rx_data;
  3. uint32_t rx_end;
  4. void USART3_IRQHandler(void)
  5. {
  6. if(USART_GetITStatus(USART3,USART_IT_RXNE) != RESET)
  7. {
  8. rx_data = USART_ReceiveData(USART3); //接收到的数据放入rx_data
  9. rx_end = 1;
  10. }
  11. USART_ClearITPendingBit(USART3,USART_IT_RXNE); //清除中断待处理位
  12. }

6.最后是遥控控制小车部分

car_ctrl.c

  1. #include "car_ctrl.h"
  2. #include "bsp_usart.h"
  3. #include "bsp_motor.h"
  4. extern uint8_t rx_end;
  5. extern uint8_t rx_data;
  6. void RUN_Prg(void)
  7. {
  8. if(rx_end)
  9. {
  10. rx_end = 0;
  11. if(rx_data == 'U') //前进 (左前、左后轮反转, 右前、右后轮子正转)
  12. {
  13. MotorCtrl(LEFT_FRONT,REVERSE);
  14. MotorCtrl(LEFT_REAR,REVERSE);
  15. MotorCtrl(RIGHT_FRONT,FORWARD);
  16. MotorCtrl(RIGHT_REAR,FORWARD);
  17. }
  18. else if(rx_data == 'L') //左转 (左前、左后轮正转, 右前、右后轮子正转)
  19. {
  20. MotorCtrl(LEFT_FRONT,FORWARD);
  21. MotorCtrl(LEFT_REAR,FORWARD);
  22. MotorCtrl(RIGHT_FRONT,FORWARD);
  23. MotorCtrl(RIGHT_REAR,FORWARD);
  24. }
  25. else if(rx_data == 'R') //右转 (左前、左后轮反转, 右前、右后轮子反转)
  26. {
  27. MotorCtrl(LEFT_FRONT,REVERSE);
  28. MotorCtrl(LEFT_REAR,REVERSE);
  29. MotorCtrl(RIGHT_FRONT,REVERSE);
  30. MotorCtrl(RIGHT_REAR,REVERSE);
  31. }
  32. else if(rx_data == 'D') //后退 (左前、左后轮正转, 右前、右后轮子反转)
  33. {
  34. MotorCtrl(LEFT_FRONT,FORWARD);
  35. MotorCtrl(LEFT_REAR,FORWARD);
  36. MotorCtrl(RIGHT_FRONT,REVERSE);
  37. MotorCtrl(RIGHT_REAR,REVERSE);
  38. }
  39. else if(rx_data == 'B') //刹车
  40. {
  41. MotorCtrl(LEFT_FRONT,BRAKE);
  42. MotorCtrl(LEFT_REAR,BRAKE);
  43. MotorCtrl(RIGHT_FRONT,BRAKE);
  44. MotorCtrl(RIGHT_REAR,BRAKE);
  45. }
  46. else if(rx_data == 'u'|'l'|'r'|'d'|'S'|'s'|'b')//前进|左转|右转|后退|关闭电机按下/松掉|刹车键松掉后都关闭电机
  47. {
  48. MotorCtrl(LEFT_FRONT,OFF);
  49. MotorCtrl(LEFT_REAR,OFF);
  50. MotorCtrl(RIGHT_FRONT,OFF);
  51. MotorCtrl(RIGHT_REAR,OFF);
  52. }
  53. }
  54. }

PS:软件及接线图、原理图、芯片手册等资料都打包在文章末尾的下载链接中!!! 

6.APP通信协议介绍

软件说明↓↓↓↓↓↓↓红框内容就是很简单的通信协议了

IOS端APP:HackerRemote

 刷新列表,找到你蓝牙模块的名字,点进去如下图

把Service UUID、TX UUID、RX UUID改成下图框里的默认值,点击连接就好了

连上之后蓝牙模块蓝灯长亮,app界面如下图。好了,可以愉快的遥控小车了~~~

7.完整程序和资源下载地址:

链接:https://pan.baidu.com/s/1FkaoSZ0fNEJ3TbaNO1fp5A 
提取码:wsmx

※好多朋友私信我说工程报错,下图这2个文件删了就行

(本来是准备加一个超声波模块的,后面没搞,文件忘记删了)

8.有问题欢迎评论留言交流,每天都会看一下帖子及时解答~ 

9.分享一个OLED+天气显示的小电视 

                                   

视频地址:OLED+ESP8266 时钟天气显示_哔哩哔哩_bilibili

教学视频地址:OELD+ESP8266 时钟天气显示小电视 制作教程!!(超细致)(太极创客开源项目)_哔哩哔哩_bilibili

外观结构改版已在立创开源社区开源:https://oshwhub.com/yz961114/oled-esp8266-tian-qi-shi-zhong-xian-shi 

10.分享一个11万转速的暴力小风扇    

                         

视频地址:11w转暴力小风扇_哔哩哔哩_bilibili

接线图及物料清单地址:11w转暴力小风扇 超详细购买清单说明以及接线图_哔哩哔哩_bilibili

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/500996?site
推荐阅读
相关标签
  

闽ICP备14008679号