标题:
单片机蓝牙控制小车前后左右+寻迹,到底哪里出了问题 ,小车通电之后完全不动
[打印本页]
作者:
brad1111
时间:
2020-5-6 09:06
标题:
单片机蓝牙控制小车前后左右+寻迹,到底哪里出了问题 ,小车通电之后完全不动
#include "reg52.h"
#include <intrins.h>
sbit IN1=P1^0; //定义驱动模块IN1为P1.0 左上电机 M2 OUT1 OUT2
sbit IN2=P1^1; //定义驱动模块IN2为P1.1
sbit IN3=P1^2; //定义驱动模块IN3为P1.2 左下电机 M3 OUT3 OUT4
sbit IN4=P1^3; //定义驱动模块IN4为P1.3
sbit IN5=P1^4; //定义驱动模块IN5为P1.4 右上电机 M1 OUT5 OUT6
sbit IN6=P1^5; //定义驱动模块IN6为P1.5
sbit IN7=P1^6; //定义驱动模块IN7为P1.6 右下电机 M4 OUT7 OUT8
sbit IN8=P1^7; //定义驱动模块IN8为P1.7
//寻迹探头
sbit left_led=P2^5; //定义左路寻迹为P2.5
sbit middle_led=P2^6; //定义中路寻迹为P2.6
sbit right_led=P2^7; //定义右路寻迹为P2.7
#define Left_moto_go {IN1=1,IN2=0,IN3=1,IN4=0;} //左边两个电机向前走
#define Left_moto_back {IN1=0,IN2=1,IN3=0,IN4=1;} //左边两个电机向后转
#define Left_moto_Stop {IN1=0,IN2=0,IN3=0,IN4=0;} //左边两个电机停转
#define Right_moto_go {IN5=1,IN6=0,IN7=1,IN8=0;} //右边两个电机向前走
#define Right_moto_back {IN5=0,IN6=1,IN7=0,IN8=1;} //右边两个电机向后转
#define Right_moto_Stop {IN5=0,IN6=0,IN7=0,IN8=0;} //右边两个电机停转
#define M2_moto_pwm IN2 //PWM信号端 左上电机M2
#define M3_moto_pwm IN4 // 左下电机M3
#define M1_moto_pwm IN6 // 右上电机M1
#define M4_moto_pwm IN8 // 右下电机M4
//PWM 寻迹中调速
unsigned char pwm_val_left =0;//变量定义
unsigned char push_val_left =0;//左电机占空比N/10
unsigned char pwm_val_right =0;//变量定义
unsigned char push_val_right=0;//右电机占空比N/10
bit Right_moto_stop=1; //在右电机调速时,如果右电机停止,PWM小于PUSH,PWM置1,否则为0;若PWM≥10,PWM关断
bit Left_moto_stop =1; //在左电机调速时,如果右电机停止,PWM小于PUSH,PWM置1,否则为0;若PWM≥10,PWM关断
unsigned int timer1=0;
// sfr T2CON = 0xC8;
sfr T2MOD = 0xC9;
// sfr RCAP2L = 0xCA;
// sfr RCAP2H = 0xCB;
// sfr TL2 = 0xCC;
// sfr TH2 = 0xCD;
//蓝牙数据发送
#define left 'C' //左转发送数据C
#define right 'D' //右转发送数据D
#define up 'A' //前进发送数据A
#define down 'B' //后退发送数据B
#define stop 'F' //停止发送数据F
#define Car '1' //手机软件1号键 表示寻迹按钮
char code str[] = "收到指令,向前!\n";
char code str1[] = "收到指令,向后!\n";
char code str2[] = "收到指令,向左!\n";
char code str3[] = "收到指令,向右!\n";
char code str4[] = "收到指令,停止!\n";
unsigned char i=0;
unsigned char dat=0;
unsigned char buff[5]=0; //接收缓冲字节
// 程序功能
bit flag_REC=0; //串行接收标去位
bit flag =0; //
bit flag_xj =0; //寻迹标志
//延时函数
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
//前进函数 加入PWM
void run_pwm(void)
{
push_val_left=9; //速度调节变量 0-9。。。9最小,0最大
push_val_right=9; //速度调节变量 0-9。。。9最小,0最大
Left_moto_go ; //左电机往前走
Right_moto_go ; //右电机往前走
}
//后退函数 加入PWM
void backrun_pwm(void)
{
push_val_left=9; //速度调节变量 0-9。。。9最小,0最大
push_val_right=9; //速度调节变量 0-9。。。9最小,0最大
Left_moto_back ; //左电机往后走
Right_moto_back ; //右电机往后走
}
//左转函数 加入PWM
void leftrun_pwm(void)
{
push_val_left=9; //速度调节变量 0-9。。。9最小,0最大
push_val_right=9; //速度调节变量 0-9。。。9最小,0最大
Left_moto_Stop ; //左电机往前走
Right_moto_go ; //右电机往后走
}
//右转函数 加入PWM
void rightrun_pwm(void)
{
push_val_left=9; //减低一半速度 速度调节变量 0-9。。。9最小,0最大
push_val_right=9; //减低一半速度 速度调节变量 0-9。。。9最小,0最大
Left_moto_go ; //左电机往前走
Right_moto_Stop ; //右电机往后走
}
//小车停止
void keepstop(void)
{
Left_moto_Stop ; //左电机往前走
Right_moto_Stop ; //右电机往前走
}
//小车前进 全速
void run(void)
{
Left_moto_go ; //左电机往前走
Right_moto_go ; //右电机往前走
}
//小车后退 全速
void backrun(void)
{
Left_moto_back ; //左电机往后走
Right_moto_back ; //右电机往后走
}
//左转函数 全速
void leftrun(void)
{
Left_moto_Stop ; //左电机往前走
Right_moto_go ; //右电机往后走
}
//右转函数 全速
void rightrun(void)
{
Left_moto_go ; //左电机往前走
Right_moto_Stop ; //右电机往后走
}
//左电机调速
void pwm_out_left_moto(void)
{
if(Left_moto_stop)
{
if(pwm_val_left<=push_val_left)
{
M2_moto_pwm=1; //M2的PWM
M3_moto_pwm=1; //M3的PWM
}
else
{
M2_moto_pwm=0;
M3_moto_pwm=0;
}
if(pwm_val_left>=10)
pwm_val_left=0;
}
else
{
M2_moto_pwm=0;
M3_moto_pwm=0;
}
}
//右电机调速
void pwm_out_right_moto(void)
{
if(Right_moto_stop)
{
if(pwm_val_right<=push_val_right)
{
M1_moto_pwm=1; //M1
M4_moto_pwm=1; //M4
}
else
{
M1_moto_pwm=0;
M4_moto_pwm=0;
}
if(pwm_val_right>=10)
pwm_val_right=0;
}
else
{
M1_moto_pwm=0;
M4_moto_pwm=0;
}
}
void Time1(void) interrupt 3 //3 为定时器1的中断号 1 定时器0的中断号 0 外部中断1 2 外部中断2 4 串口中断
{
TH1 = 0xFF;
TL1 = 0x9C;
timer1++;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}
//中断号为4 串口中断
void sint() interrupt 4 //中断接收3个字节
{
if(RI) //是否接收中断
{
RI=0;
dat=SBUF;
if(dat=='O'&&(i==0)) //接收数据第一帧
{
buff[i]=dat;
flag=1; //开始接收数据
}
else
if(flag==1)
{
i++;
buff[i]=dat;
if(i>=2)
{i=0;flag=0;flag_REC=1 ;} // 停止接收
}
}
}
//主函数
void mian(void)
{ // T1 T0
TMOD=0x11; // 0001 0001 工作方式3 两个独立8位计数器
TH1=0xFF; //100uS定时 高8位
TL1=0x9c; // 低8位
TH0=0;
TL0=0;
T2CON=0x34; //8位寄存器0011 0100 串行发送时钟 TCLK=1, T2工作于波特率发生器方式
T2MOD=0x00; // 串行接收时钟 RCLK=1,T2工作于波特率发生器方式
RCAP2H=0xFF; // 为1时外部事件计数,为0时T2为定时器
RCAP2L=0xDC ; //波特率位4800 12/[32*(65536-x)]
SCON=0x50;
PCON=0x00;
PS=1;
TR1=1; // 定时器1工作
ET1=1; // 打开定时器1中断
ES=1; // 串行口中断
EA=1; // 总中断允许
while(flag_xj) //循线标志位
{
if(flag_REC==1)
{
break;
}
//有信号为0 没有信号为1
if(right_led==1&&middle_led==1&&left_led==1) //三路检测到黑线
{
Left_moto_stop=1;
Right_moto_stop=1;
run_pwm();
}
else
{
if(right_led==1&&middle_led==0&&left_led==0) //右边检测到黑线
{
Left_moto_stop=1;
Right_moto_stop=1;
rightrun_pwm();
}
if(right_led==0&&middle_led==0&&left_led==1) //左边检测到黑线
{
Right_moto_stop=1;
Left_moto_stop=1;
leftrun_pwm();
}
}
}
if(flag_REC==1) //
{
flag_REC=0;
if(buff[0]=='O'&&buff[1]=='N') //第一个字节为O,第二个字节为N,第三个字节为控制码
switch(buff[2])
{
case up : // 前进
// send_str( );
flag_xj=0; //寻迹置0
Left_moto_stop=0;
Right_moto_stop=0;
run();
break;
case down: // 后退
// send_str1( );
flag_xj=0;
Left_moto_stop=0;
Right_moto_stop=0;
backrun();
break;
case left: // 左转
// send_str2( );
flag_xj=0;
leftrun();
break;
case right: // 右转
// send_str3( );
flag_xj=0;
Left_moto_stop=0;
Right_moto_stop=0;
rightrun();
break;
case stop: // 停止
// send_str4( );
flag_xj=0;
Left_moto_stop=0;
Right_moto_stop=0;
keepstop();
break;
case Car : //
flag_xj=1;
break;
}
}
}
复制代码
作者:
f556
时间:
2020-5-6 11:27
程序是copy别人的吗?如果是自己写的,检查硬件、程序
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1