#include <reg52.h>
//定义输出端口
sbit INT=P1^0;
sbit INT2=P1^1;
sbit INT3=P1^2;
sbit INT4=P1^3;
sbit EN1 = P1^4;
sbit EN2 = P1^5;
typedef unsigned int u16;
typedef unsigned char u8;
u8 buf; //串口数据缓冲
u8 speed=100; //设置初始速度
u8 pwm; //设置比较参数
//向前走
void CarGo()
{
speed = 50;
INT= 1;
INT2=0;
INT3=1;
INT4=0;
}
//向后走
void CarBack()
{
speed = 40;
INT= 0;
INT2=1;
INT3=0;
INT4=1;
}
//小车左转
void CarTurnLeft()
{
speed = 35;
INT=0;
INT2=0;
INT3=1;
INT4=0;
}
//小车右转
void CarTurnRight()
{
speed = 35;
INT= 1;
INT2=0;
INT3=0;
INT4=0;
}
//小车停止
void CarStop()
{
INT= 0;
INT2=0;
INT3=0;
INT4=0;
speed = 0;
}
//小车加速
void speedup()
{
INT= 1;
INT2=0;
INT3=1;
INT4=0;
speed+=4;
if(speed>=100)
{
speed = 100;
}
}
//小车减速
void speeddown()
{
INT= 1;
INT2=0;
INT3=1;
INT4=0;
speed-=5;
if(speed<=18)
{
speed = 18;
}
}
//初始化定时器0,以用于调速
void timer ()
{
TMOD|=0X01;
TH0 = (65536-1500)/256;
TL0 = (65536-1500)%256;
TR0 = 1;
ET0 = 1;
EA =1;
}
//定时器1中断函数
void init() interrupt 1
{
TH0 = (65536-100)/256;
TL0 = (65536-100)%256;
pwm++;
if(pwm==100)
{pwm = 0;}
if(pwm<=speed)
{
EN1 = 1;
EN2 = 1;
}
if(pwm>speed)
{
EN1 = 0;
EN2 = 0;
}
}
//串口初始化
void irinit()
{
SCON=0x50; //设定串口工作方式
PCON=0x00; //波特率不倍增
EA=1; //打开总中断
TMOD|=0x20;
ES=1; //打开串口中断
TR1=1;
TH1=0xFD; //设置波特率为9600;
TL1=0xFD;
}
void main()
{
irinit();
timer ();
while(1);
}
void serial() interrupt 4
{
TL1=0xFD;
TH1=0xFD;//波特率9600
buf = SBUF;//从串口缓冲区取得数据
//接收手机传给蓝牙的指令以控制小车
while(!RI);
RI = 0;
if(buf=='G')
{
CarGo();
}
if(buf=='B')
{
CarBack();
}
if(buf=='S')
{
CarStop();
}
if(buf=='C')
{
CarTurnLeft();
}
if(buf=='E')
{
CarTurnRight();
}
if(buf=='1')
{
speedup();
}
if(buf=='2')
{
speeddown();
}
}
来源:https://blog.csdn.net/linfengXBB/article/details/116140915
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询