简单的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部分,比较简单

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

2.串口通讯部分

bsp_usart.c

#include "bsp_usart.h"

static void USART3_NVIC_Config(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void USART3_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);   //使能GPIO时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);	//使能USART3时钟
	
	/*TX_GPIO*/
	GPIO_InitStructure.GPIO_Pin = USART3_GPIO_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(USART3_GPIO_TX_PORT, &GPIO_InitStructure);
	
	/*RX_GPIO*/
	GPIO_InitStructure.GPIO_Pin = USART3_GPIO_RX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(USART3_GPIO_RX_PORT, &GPIO_InitStructure);
	
	/*USART3*/
	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(USART3, &USART_InitStructure);

  USART3_NVIC_Config();  
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);   //使能串口接收终断
	USART_Cmd(USART3, ENABLE);  //使能串口
}

bsp_usart.h

#ifndef __BSP_USART_H
#define __BSP_USART_H

#include "stm32f10x.h"

/*TX的GPIO端口定义*/
#define USART3_GPIO_TX_PIN					 GPIO_Pin_10
#define USART3_GPIO_TX_PORT					 GPIOB
#define USART3_GPIO_TX_CLK					 RCC_APB2Periph_GPIOB

/*RX的GPIO端口定义*/
#define USART3_GPIO_RX_PIN					 GPIO_Pin_11
#define USART3_GPIO_RX_PORT					 GPIOB
#define USART3_GPIO_RX_CLK					 RCC_APB2Periph_GPIOB

/*USART3定义*/
#define DEBUG_USARTx_CLK						 RCC_APB1Periph_USART3


void USART3_Config(void);
void USART3_Prg(void);


#endif /*__BSP_USART_H*/

3.TB6612电机驱动模块部分

bsp_tb6612.c

#include "bsp_tb6612.h"

/*TB6612模块1,驱动前轮  F*/
void TB6612_F_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(TB6612_F_GPIO_AIN1_CLK | TB6612_F_GPIO_AIN2_CLK |              \
												 TB6612_F_GPIO_BIN1_CLK | TB6612_F_GPIO_BIN2_CLK , ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_AIN1_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(TB6612_F_GPIO_AIN1_PORT, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_AIN2_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(TB6612_F_GPIO_AIN2_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_BIN1_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(TB6612_F_GPIO_BIN1_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_BIN2_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(TB6612_F_GPIO_BIN2_PORT, &GPIO_InitStructure);	
}

void TB6612_F_PWM_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(TB6612_F_GPIO_PWMA_CH1_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_PWMA_CH1_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(TB6612_F_GPIO_PWMA_CH1_PORT,&GPIO_InitStructure);
	
	RCC_APB2PeriphClockCmd(TB6612_F_GPIO_PWMB_CH2_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = TB6612_F_GPIO_PWMB_CH2_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(TB6612_F_GPIO_PWMB_CH2_PORT,&GPIO_InitStructure);
}

/*TB6612模块2,驱动后轮  R*/
void TB6612_R_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(TB6612_R_GPIO_AIN1_CLK | TB6612_R_GPIO_AIN2_CLK |              \
												 TB6612_R_GPIO_BIN1_CLK | TB6612_R_GPIO_BIN2_CLK , ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_AIN1_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(TB6612_R_GPIO_AIN1_PORT, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_AIN2_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(TB6612_R_GPIO_AIN2_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_BIN1_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(TB6612_R_GPIO_BIN1_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_BIN2_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(TB6612_R_GPIO_BIN2_PORT, &GPIO_InitStructure);
}

void TB6612_R_PWM_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(TB6612_R_GPIO_PWMA_CH3_CLK, ENABLE);	
	GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_PWMA_CH3_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(TB6612_R_GPIO_PWMA_CH3_PORT,&GPIO_InitStructure);

	RCC_APB2PeriphClockCmd(TB6612_R_GPIO_PWMB_CH4_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = TB6612_R_GPIO_PWMB_CH4_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(TB6612_R_GPIO_PWMB_CH4_PORT,&GPIO_InitStructure);
}

static void ADVANCE_TIM1_Mode_Config(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;
	
	RCC_APB2PeriphClockCmd(ADVANCE_TIM_CLK , ENABLE);
	
	TIM_TimeBaseInitStructure.TIM_Prescaler = (72-1);
	TIM_TimeBaseInitStructure.TIM_Period = (100-1);
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(ADVANCE_TIM, &TIM_TimeBaseInitStructure);
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
	TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
	TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
	
	TIM_OCInitStructure.TIM_Pulse = 0;
	TIM_OC1Init(ADVANCE_TIM, &TIM_OCInitStructure);
	TIM_OC1PreloadConfig(ADVANCE_TIM,TIM_OCPreload_Enable);
	
	TIM_OCInitStructure.TIM_Pulse = 0;
	TIM_OC2Init(ADVANCE_TIM, &TIM_OCInitStructure);
	TIM_OC2PreloadConfig(ADVANCE_TIM,TIM_OCPreload_Enable);
	
	TIM_OCInitStructure.TIM_Pulse = 0;
	TIM_OC3Init(ADVANCE_TIM, &TIM_OCInitStructure);
	TIM_OC3PreloadConfig(ADVANCE_TIM,TIM_OCPreload_Enable);
	
	TIM_OCInitStructure.TIM_Pulse = 0;
	TIM_OC4Init(ADVANCE_TIM, &TIM_OCInitStructure);
	TIM_OC4PreloadConfig(ADVANCE_TIM,TIM_OCPreload_Enable);
	
	TIM_Cmd(ADVANCE_TIM, ENABLE);
}

//void TB6612_F_Init(void)    //控制前面2轮的电机驱动模块初始化
//{
//	TB6612_F_GPIO_Config();
//	TB6612_F_PWM_GPIO_Config();
//	ADVANCE_TIM1_Mode_Config();
//}

//void TB6612_R_Init(void)    //控制后面2轮的电机驱动模块初始化
//{
//	TB6612_R_GPIO_Config();
//	TB6612_R_PWM_GPIO_Config();
//	ADVANCE_TIM1_Mode_Config();
//}

void TB6612_FR_Init(void)		//控制前和后2轮的电机驱动模块初始化
{
	TB6612_F_GPIO_Config();
	TB6612_F_PWM_GPIO_Config();
	TB6612_R_GPIO_Config();
	TB6612_R_PWM_GPIO_Config();
	ADVANCE_TIM1_Mode_Config();
}

bsp_tb6612.h

#ifndef __BSP_TB6612_H
#define __BSP_TB6612_H

#include "stm32f10x.h"

/*TB6612模块1*/
/*定义TB6612模块1(驱动前轮(_F))的AIN1、AIN2、BIN1、BIN2的GPIO端口*/
#define TB6612_F_GPIO_AIN1_PIN  			GPIO_Pin_12
#define TB6612_F_GPIO_AIN1_PORT  			GPIOB
#define TB6612_F_GPIO_AIN1_CLK    		RCC_APB2Periph_GPIOB 

#define TB6612_F_GPIO_AIN2_PIN  			GPIO_Pin_13
#define TB6612_F_GPIO_AIN2_PORT   		GPIOB
#define TB6612_F_GPIO_AIN2_CLK    		RCC_APB2Periph_GPIOB

#define TB6612_F_GPIO_BIN1_PIN  			GPIO_Pin_14
#define TB6612_F_GPIO_BIN1_PORT   		GPIOB
#define TB6612_F_GPIO_BIN1_CLK    		RCC_APB2Periph_GPIOB 

#define TB6612_F_GPIO_BIN2_PIN  			GPIO_Pin_15
#define TB6612_F_GPIO_BIN2_PORT   		GPIOB
#define TB6612_F_GPIO_BIN2_CLK    		RCC_APB2Periph_GPIOB

/*定义TB6612模块1(驱动前轮(_F))的PWMA、PWMB的GPIO端口*/
#define TB6612_F_GPIO_PWMA_CH1_PIN    GPIO_Pin_8
#define TB6612_F_GPIO_PWMA_CH1_PORT 	GPIOA
#define TB6612_F_GPIO_PWMA_CH1_CLK  	RCC_APB2Periph_GPIOA

#define TB6612_F_GPIO_PWMB_CH2_PIN    GPIO_Pin_9
#define TB6612_F_GPIO_PWMB_CH2_PORT 	GPIOA
#define TB6612_F_GPIO_PWMB_CH2_CLK  	RCC_APB2Periph_GPIOA

/*TB6612模块2*/
/*定义TB6612模块2(驱动后轮(_R))的AIN1、AIN2、BIN1、BIN2的GPIO端口*/
#define TB6612_R_GPIO_AIN1_PIN  			GPIO_Pin_6
#define TB6612_R_GPIO_AIN1_PORT  			GPIOB
#define TB6612_R_GPIO_AIN1_CLK    		RCC_APB2Periph_GPIOB 

#define TB6612_R_GPIO_AIN2_PIN  			GPIO_Pin_7
#define TB6612_R_GPIO_AIN2_PORT   		GPIOB
#define TB6612_R_GPIO_AIN2_CLK    		RCC_APB2Periph_GPIOB

#define TB6612_R_GPIO_BIN1_PIN  			GPIO_Pin_8
#define TB6612_R_GPIO_BIN1_PORT   		GPIOB
#define TB6612_R_GPIO_BIN1_CLK    		RCC_APB2Periph_GPIOB 

#define TB6612_R_GPIO_BIN2_PIN  			GPIO_Pin_9
#define TB6612_R_GPIO_BIN2_PORT   		GPIOB
#define TB6612_R_GPIO_BIN2_CLK    		RCC_APB2Periph_GPIOB

/*定义TB6612模块2(驱动后轮(_R))的PWMA、PWMB的GPIO端口*/
#define TB6612_R_GPIO_PWMA_CH3_PIN    GPIO_Pin_10
#define TB6612_R_GPIO_PWMA_CH3_PORT 	GPIOA
#define TB6612_R_GPIO_PWMA_CH3_CLK  	RCC_APB2Periph_GPIOA

#define TB6612_R_GPIO_PWMB_CH4_PIN    GPIO_Pin_11
#define TB6612_R_GPIO_PWMB_CH4_PORT 	GPIOA
#define TB6612_R_GPIO_PWMB_CH4_CLK  	RCC_APB2Periph_GPIOA



/*高级定时器TIM1参数定义*/ 
#define ADVANCE_TIM                 TIM1
#define ADVANCE_TIM_CLK             RCC_APB2Periph_TIM1


void TB6612_F_GPIO_Config(void);
void TB6612_F_PWM_GPIO_Config(void);
void TB6612_R_GPIO_Config(void);
void TB6612_R_PWM_GPIO_Config(void);
//void TB6612_F_Init(void);
//void TB6612_R_Init(void);
void TB6612_FR_Init(void);


#endif /*BSP_TB6612_H*/

4.马达动作部分

bsp_motor.c

#include "bsp_motor.h"

//马达控制函数,第一个参数为选定哪个马达,第二个参数是选定马达状态
void MotorCtrl(uint32_t motornum,uint32_t state)
{
	if(motornum == LEFT_FRONT)   //左前马达1
	{
		switch(state)
		{
			case OFF:
				F_AIN1(LOW);
				F_AIN2(LOW);
				break;
			case FORWARD:
				F_AIN1(HIGH);
				F_AIN2(LOW);
				break;			
			case REVERSE:
				F_AIN1(LOW);
				F_AIN2(HIGH);							
				break;
			case BRAKE:
				F_AIN1(HIGH);
				F_AIN2(HIGH);
				break;
			default:
				break;
		}
	}
	else if(motornum == RIGHT_FRONT)  //右前马达2
	{
		switch(state)
		{
			case OFF:
				F_BIN1(LOW);
				F_BIN2(LOW);
				break;
			case FORWARD:
				F_BIN1(HIGH);
				F_BIN2(LOW);
				break;			
			case REVERSE:
				F_BIN1(LOW);
				F_BIN2(HIGH);
				break;
			case BRAKE:
				F_BIN1(HIGH);
				F_BIN2(HIGH);
				break;
			default:
				break;
		}
	}
	else if(motornum == LEFT_REAR)   //左后马达3
	{
		switch(state)
		{
			case OFF:
				R_AIN1(LOW);
				R_AIN2(LOW);
				break;
			case FORWARD:
				R_AIN1(HIGH);
				R_AIN2(LOW);
				break;			
			case REVERSE:
				R_AIN1(LOW);
				R_AIN2(HIGH);							
				break;
			case BRAKE:
				R_AIN1(HIGH);
				R_AIN2(HIGH);
				break;
			default:
				break;
		}
	}
	else if(motornum == RIGHT_REAR)    //右后马达4
	{
		switch(state)
		{
			case OFF:
				R_BIN1(LOW);
				R_BIN2(LOW);
				break;
			case FORWARD:
				R_BIN1(HIGH);
				R_BIN2(LOW);
				break;			
			case REVERSE:
				R_BIN1(LOW);
				R_BIN2(HIGH);
				break;
			case BRAKE:
				R_BIN1(HIGH);
				R_BIN2(HIGH);
				break;
			default:
				break;
		}
	}
}

/*马达全部关闭函数*/
void MotorAllOFF(void)
{
	MotorCtrl(LEFT_FRONT,OFF);
	MotorCtrl(LEFT_REAR,OFF);
	MotorCtrl(RIGHT_FRONT,OFF);
	MotorCtrl(RIGHT_REAR,OFF);
}

bsp_motor.h

#ifndef __BSP_MOTOR_H
#define __BSP_MOTOR_H

#include "bsp_tb6612.h"

/*H桥两端电平高低控制*/
#define HIGH     	 1
#define LOW   		 0

/*TB6612模块1,驱动前轮  F*/
/*AIN1*/
#define F_AIN1(a)				if(a) GPIO_SetBits(TB6612_F_GPIO_AIN1_PORT, TB6612_F_GPIO_AIN1_PIN); \
												else  GPIO_ResetBits(TB6612_F_GPIO_AIN1_PORT, TB6612_F_GPIO_AIN1_PIN)
/*AIN2*/
#define F_AIN2(a)				if(a)	GPIO_SetBits(TB6612_F_GPIO_AIN2_PORT, TB6612_F_GPIO_AIN2_PIN); \
												else  GPIO_ResetBits(TB6612_F_GPIO_AIN2_PORT, TB6612_F_GPIO_AIN2_PIN)
/*BIN1*/
#define F_BIN1(a)				if(a) GPIO_SetBits(TB6612_F_GPIO_BIN1_PORT, TB6612_F_GPIO_BIN1_PIN); \
												else  GPIO_ResetBits(TB6612_F_GPIO_BIN1_PORT, TB6612_F_GPIO_BIN1_PIN)
/*BIN2*/
#define F_BIN2(a)				if(a) GPIO_SetBits(TB6612_F_GPIO_BIN2_PORT, TB6612_F_GPIO_BIN2_PIN); \
											  else  GPIO_ResetBits(TB6612_F_GPIO_BIN2_PORT, TB6612_F_GPIO_BIN2_PIN)

/*TB6612模块2,驱动后轮  R*/											
/*AIN1*/
#define R_AIN1(a)				if(a) GPIO_SetBits(TB6612_R_GPIO_AIN1_PORT, TB6612_R_GPIO_AIN1_PIN); \
												else  GPIO_ResetBits(TB6612_R_GPIO_AIN1_PORT, TB6612_R_GPIO_AIN1_PIN)
/*AIN2*/
#define R_AIN2(a)				if(a)	GPIO_SetBits(TB6612_R_GPIO_AIN2_PORT, TB6612_R_GPIO_AIN2_PIN); \
												else  GPIO_ResetBits(TB6612_R_GPIO_AIN2_PORT, TB6612_R_GPIO_AIN2_PIN)
/*BIN1*/
#define R_BIN1(a)				if(a) GPIO_SetBits(TB6612_R_GPIO_BIN1_PORT, TB6612_R_GPIO_BIN1_PIN); \
												else  GPIO_ResetBits(TB6612_R_GPIO_BIN1_PORT, TB6612_R_GPIO_BIN1_PIN)
/*BIN2*/
#define R_BIN2(a)				if(a) GPIO_SetBits(TB6612_R_GPIO_BIN2_PORT, TB6612_R_GPIO_BIN2_PIN); \
											  else  GPIO_ResetBits(TB6612_R_GPIO_BIN2_PORT, TB6612_R_GPIO_BIN2_PIN)

/*MotorCtrl(uint32_t motornum,uint32_t state)函数的参数宏定义*/												
#define LEFT_FRONT  1      //马达1(AIN、左前轮)
#define RIGHT_FRONT 2      //马达2(BIN、右前轮)	
#define LEFT_REAR   3      //马达3(AIN、左后轮)
#define RIGHT_REAR  4      //马达4(BIN、右后轮)															

#define OFF					0			 //停止转动
#define FORWARD 		1      //正转
#define REVERSE     2			 //反转
#define BRAKE				3			 //制动


void MotorCtrl(uint32_t motornum,uint32_t state);
void MotorAllOFF(void);

#endif /*__BSP_MOTOR_H*/

5.接收中断部分

stm32f10x_it.h

#include "bsp_usart.h"

uint8_t rx_data;
uint32_t rx_end;

void USART3_IRQHandler(void)
{	
	if(USART_GetITStatus(USART3,USART_IT_RXNE) != RESET)
	{
		rx_data = USART_ReceiveData(USART3);   //接收到的数据放入rx_data
		rx_end = 1;
	}
	USART_ClearITPendingBit(USART3,USART_IT_RXNE);   //清除中断待处理位
}

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

car_ctrl.c

#include "car_ctrl.h"
#include "bsp_usart.h"
#include "bsp_motor.h"

extern uint8_t rx_end;
extern uint8_t rx_data;

void RUN_Prg(void)
{
	if(rx_end)
	{
		rx_end = 0;
		if(rx_data == 'U')  			 //前进     (左前、左后轮反转, 右前、右后轮子正转)
		{
			MotorCtrl(LEFT_FRONT,REVERSE);
			MotorCtrl(LEFT_REAR,REVERSE);
			MotorCtrl(RIGHT_FRONT,FORWARD);
			MotorCtrl(RIGHT_REAR,FORWARD);			
		}
		else if(rx_data == 'L')    //左转  (左前、左后轮正转, 右前、右后轮子正转)
		{
			MotorCtrl(LEFT_FRONT,FORWARD);
			MotorCtrl(LEFT_REAR,FORWARD);
			MotorCtrl(RIGHT_FRONT,FORWARD);
			MotorCtrl(RIGHT_REAR,FORWARD);
		}		
		else if(rx_data == 'R')    //右转  (左前、左后轮反转, 右前、右后轮子反转)
		{
			MotorCtrl(LEFT_FRONT,REVERSE);
			MotorCtrl(LEFT_REAR,REVERSE);
			MotorCtrl(RIGHT_FRONT,REVERSE);
			MotorCtrl(RIGHT_REAR,REVERSE);			
		}
		else if(rx_data == 'D')    //后退  (左前、左后轮正转, 右前、右后轮子反转)
		{
			MotorCtrl(LEFT_FRONT,FORWARD);
			MotorCtrl(LEFT_REAR,FORWARD);
			MotorCtrl(RIGHT_FRONT,REVERSE);
			MotorCtrl(RIGHT_REAR,REVERSE);		
		}
		else if(rx_data == 'B')		 //刹车
		{
			MotorCtrl(LEFT_FRONT,BRAKE);
			MotorCtrl(LEFT_REAR,BRAKE);
			MotorCtrl(RIGHT_FRONT,BRAKE);
			MotorCtrl(RIGHT_REAR,BRAKE);			
		}	
		else if(rx_data == 'u'|'l'|'r'|'d'|'S'|'s'|'b')//前进|左转|右转|后退|关闭电机按下/松掉|刹车键松掉后都关闭电机
		{
			MotorCtrl(LEFT_FRONT,OFF);
			MotorCtrl(LEFT_REAR,OFF);
			MotorCtrl(RIGHT_FRONT,OFF);
			MotorCtrl(RIGHT_REAR,OFF);			
		}
	}
}

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

评论 212
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值