这两周的实训课程,制作一个蓝牙控制的STM32智能小车。代码是给的。
硬件:
1.STM32板卡
2.电机驱动板L298N
3.小车套件(轮子,板材,电机等)
4.蓝牙JDY-30
5.18650锂电池
(巡线,超声波选配,代码有)
先测试电机是否能够正常运行(直接用STM32上的供电),组装小车,连接蓝牙,初始化配置蓝牙(AT指令),
最后烧录小车程序,测试小车能否正常运行。
需要学习这方面的知识:
蓝牙、串口、中断、GPIO、定时器、PWM。(过,不会的自己百度,看书,CSDN都一堆)
接线:
驱动电机与L298N:这还要我教?电机接对应的output,不对就反过来。
JDY-30与STM32连接 :
JDY-30 STM32
VCC <----------------> +5V
GND <----------------> GND
TXD <----------------> U1_Rx(PD2)
RXD <----------------> U1_Tx(PC12)
L298N与STM32连接 :
L298N STM32
GND <----------------> GND
+5V <----------------> +5V
IN1 <----------------> PB4
IN2 <----------------> PB3
IN3 <----------------> PB5
IN4 <----------------> PC11
L298N 与18650锂电池连接:
L298N 18650锂电池
GND <----------------> GND (黑色线)
+12V <----------------> VCC (红色线)
注意事项
电源线和地线切勿接反!!(如果反了可能烧掉MCU)。在烧录代码的时候,请不要外部锂电池给板卡供电!!!
主函数main.c
#include "stm32f10x.h"
#include "drv_rgb.h"
#include "drv_uart.h"
#include "SysTick.h"
#include <stdio.h>
#include "misc.h"
#include "Front_hc04.h"
#include "drv_time.h"
#include "drv_car.h"
#include "drv_serchLine.h"
float retDate = 0;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SysTick_init(72);
RGB_Configuration();
Uart1_Configuration();
Uart5_Configuration();
TIM_InitConfig();
Hc_Sr04_Init();
TIM3_Configuration();
TIM_SetSpeed(0, 0);
SERCHLINE_Configuration();
Hc_Sr04_Start();
while(1)
{
if (AutoDriver_IsAutoDriverFlag())
{
// ×Ô¶¯Ñ°¼£
SERCHLINE_AutoRun();
// ×Ô¶¯±ÜÕÏ
if (HCSR04_IsGetDistantFlag())
{
HCSR04_ClearIsGetDistantFlag();
retDate = HC_SR04_Get_Distance();
printf("retDate = %f\n",retDate);
if (retDate < 3)
TIM_SetSpeed(0, 0);
delay_ms(50);
Hc_Sr04_Start();
}
}
}
}
中断函数stm32f10x_it.c:(手机APP自定义命令,16位那个,a的那个填是0A,b的是0B)
/**
******************************************************************************
* @file GPIO/IOToggle/stm32f10x_it.c
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and peripherals
* interrupt service routine.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "stm32f10x_gpio.h"
#include "drv_uart.h"
#include "Front_hc04.h"
#include "SysTick.h"
#include "math.h"
#include "stm32f10x_exti.h"
#include "stdio.h"
#include "drv_time.h"
#include "drv_rgb.h"
/** @addtogroup STM32F10x_StdPeriph_Examples
* @{
*/
/** @addtogroup GPIO_IOToggle
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro --------------------------------