单片机设计 智能小车(STM32、esp8266)
一、电路设计
此电路由STM32F1和ESP8266WIFI模块组成。
1.手机APP:
APP部分我们采用常用的APP wifi调试软件 ITEAD WIFI,安装完成后打开
如下界面,点击Set up进入设置:
进入后会自动获取到手机的IP,注意手机是连接到一个路由器下的,支持局域网内调试,先设置端口,一般是 8080
2.ESP8266模块介绍:
书呆子
Wifi模块
|
Stm32核心板
|
说明
—|—|—
GND
|
GND
|
UTXD
|
PB11(RXD)
|
串口发送IO
CH_PD
|
VCC
|
3.3V,wifi使能位
URXD
|
PB10(TXD)
|
串口接收IO
VCC
|
3.3V
|
3.3V
其他
|
|
悬空
二、部分代码
#include "stm32f10x.h"
#include "interface.h"
#include "LCD1602.h"
#include "IRCtrol.h"
#include "motor.h"
#include "uart.h"
#include "ESP8266.h"
#include "serialportAPI.h"
#include
#include
//全局变量定义
unsigned int speed_count=0;//占空比计数器 50次一周期
char front_left_speed_duty=SPEED_DUTY;
char front_right_speed_duty=SPEED_DUTY;
char behind_left_speed_duty=SPEED_DUTY;
char behind_right_speed_duty=SPEED_DUTY;
unsigned char tick_5ms = 0;//5ms计数器,作为主函数的基本周期
unsigned char tick_1ms = 0;//1ms计数器,作为电机的基本计数器
unsigned char tick_200ms = 0;//刷新显示
unsigned int tick_3s = 0;//隔3s断线检测
char ctrl_comm = COMM_STOP;//控制指令
unsigned char continue_time=0;
unsigned char wifi_rec_flag=0;//蓝牙控制标志位
#define HOST_NAME "192.168.31.195" //主机IP
#define HOST_PORT 8080 //主机端口
#define SSID "Hantech" //路由器名
#define PSD "hantechinternet" //密码
#define RECVBUF_SIZE 16
unsigned char buffer[RECVBUF_SIZE] = {0};
int main(void)
{
USART1Conf(9600);
DBG("system begin");
delay_init();
GPIOCLKInit();
UserLEDInit();
LCD1602Init();
TIM2_Init();
MotorInit();
ServoInit();
// USART3Conf(9600);
while(1)
{
DBG("chang baud");
UartBegin(115200,&USART3Conf;,&PutChar;);//每次设置完波特率后需要做一点延时
delay(500);
SetBaud(19200);
UartBegin(19200,&USART3Conf;,&PutChar;);//更改波特率到19200
delay(500);
if(0 != SetBaud(19200))//在新波特率下检查是否成功
{
break;//成功就退出
}
}
while(0 == WifiInit(SSID,PSD,HOST_NAME,HOST_PORT));
while(1)
{
if(SerialAvailable() > 5)//接收到至少5个字符
{
int len;
len = recv(buffer, RECVBUF_SIZE, 100);
if (len > 0)
{
char inChar;
inChar = buffer[0];
// send(buffer, len);
if (ctrl_comm != inChar || continue_time == 1)
{
wifi_rec_flag = 1;
ctrl_comm = inChar;
}
continue_time = 60;//更新持续时间
tick_3s = 0;//心跳包周期
}
}
if(tick_5ms >= 5)
{
tick_5ms = 0;
tick_200ms++;
if(tick_200ms >= 40)
{
tick_200ms = 0;
tick_3s++;
LEDToggle(LED_PIN);
if(tick_3s >= 15)//15*200=3000
{
tick_3s = 0;
//定时检查连接是否正常,如果不正常,wifi复位重新连接
if (getSystemStatus() != STATUS_GETLINK)
{
DBG("TCP unlink");
while(!WifiInit(SSID,PSD,HOST_NAME,HOST_PORT));
}else
{
DBG("TCP link tick");
}
}
}
//continue_time--;//200ms 无接收指令就停车
if(continue_time == 0)
{
continue_time = 1;
CarStop();
}
//do something
if(wifi_rec_flag == 1)//接收到红外信号
{
wifi_rec_flag = 0;
switch(ctrl_comm)
{
case COMM_UP: CarGo();break;
case COMM_DOWN: CarBack();break;
case COMM_LEFT: CarLeft();break;
case COMM_RIGHT: CarRight();break;
case COMM_STOP: CarStop();break;
default : break;
}
LCD1602WriteCommand(ctrl_comm);
DBG("recv COMM");
}
}
}
}