刚刚研究单片机小车一段时间,在某宝上买个小车,随车附赠了WIFI控制小车运转的代码,然后想自己改改代码完成我想要的功能。但问题是,在switch……case这个语句中,我都没有给小车发送指令,小车自己就动了,我不太清楚是哪个地方出的问题,所以想请教各位大神,我该怎么改,小车才能在接到我的指令后,才动。(我在程序里面编了一个gg事件,用于证明程序是没有经过判断就直接运行case中的事件,但结果确实是我没有发送指令,他就动了......)代码如下:
/*////////////////接线图//////////////////////////////////////////
MLA-----------------P1.0 L---left 左
MLB-----------------P1.1
MRA-----------------P1.2 R---right 右
MRB-----------------P1.3
////////////////////////////////////////////////////////////////*/
#include<AT89x51.H>
#define uchar unsigned char
uchar count;
//HJ-4WD小车驱动接线定义
#define Left_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;} //左边两个电机向前走
#define Left_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;} //左边两个电机向后转
#define Left_moto_Stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;} //左边两个电机停转
#define Right_moto_go {P1_0=1,P1_1=0,P1_2=1,P1_3=0;} //右边两个电机向前走
#define Right_moto_back {P1_0=0,P1_1=1,P1_2=0,P1_3=1;} //右边两个电机向后走
#define Right_moto_Stop {P1_0=0,P1_1=0,P1_2=0,P1_3=0;} //右边两个电机停转
bit M3_start_flag=0;
bit M4_start_flag=0;
bit M3_forward_flag=0;
bit M4_forward_flag=0;
//uchar SEH_PWM_count=1;
//uchar SEV_PWM_count=1;
uchar M3A_count=0;
uchar M3B_count=0;
uchar M4A_count=0;
uchar M4B_count=0;
uchar SEH_count=15;
uchar SEV_count=14;
uchar command=0;
sbit BEEP=P2^6;
sbit LEMP=P2^0;
sbit M3A=P0^0;
sbit M3B=P0^1;
sbit M4A=P0^2;
sbit M4B=P0^3;
//sbit SEH_PWM=P2^6;
//sbit SEV_PWM=P2^7;
int gg=0;
void delay(int z)
{
int i;
for(i=z;i>0;i--);
}
int aa(int g)
{
g++;
return g;
}
void Com_Init()
{
//IE = 0x00; //停止所有中断
TMOD= 0x22; //设置定时器0为方式1(16位);定时器1为方式2(8位自动重装)
SCON= 0x50; //设置串口控制寄存器,串口方式1,10位uart,波特率可调;允许接收
TH1 = TL1 = 0xfd; //为定时器1赋初值,晶振为11.0592MHz时波特率为9600bps
IE = 0x92; //开定时器0中断,串口中断,外中断0和总中断
//EA,_ _ ES,ET1,EX1,ET0,EX0
TR1 = 1;
}
void Time0_Init() //定时器初始化
{
//TMOD=0x01; //使用定时器T0的模式1
TH0=TL0=0xa3;
TR0=1;
}
void Time0_Int() interrupt 1
{
count=count++;
if(count>=200)
{
count=0;
//if(SEH_PWM_count>=40000)SEH_PWM_count=0;
//if(SEV_PWM_count>=40000)SEV_PWM_count=0;
}
// if(SEH_PWM_count)
// {
// if(count<SEH_count) //判断0.5ms次数是否小于角度标识
// SEH_PWM=1; //确实小于,pwm输出高电平
// else
// SEH_PWM=0; //大于则输出低电平
//SEH_PWM_count++;
//}
// if(SEV_PWM_count)
// {
// if(count<SEV_count) //判断0.5ms次数是否小于角度标识
// SEV_PWM=1; //确实小于,pwm输出高电平
// else
// SEV_PWM=0; //大于则输出低电平
// SEV_PWM_count++;
// }
}
void main()
{
int i=0;
count=0;
P0=0xef;
P1=0x00;
P2=0xff;
P3=0xff;
Time0_Init();
Com_Init();
while(1)
{ i++;
if(i>=200)i=0;
if(gg>=100||gg<=15)
{ Left_moto_go ; //左电机往前走
Right_moto_back ;}
//////////////////////////////////////////
if(M3_start_flag)
{
if(M3_forward_flag)
{
M3A=0;
if(i<M3B_count)M3B=1;
else M3B=0;
}
else
{
M3B=0;
if(i<M3A_count)M3A=1;
else M3A=0;
}
}
else
{
M3A=0;
M3B=0;
}
/////////////////////////////////////////////
if(M4_start_flag)
{
if(M4_forward_flag)
{
M4B=0;
if(i<M4A_count)M4A=1;
else M4A=0;
}
else
{
M4A=0;
if(i<M4B_count)M4B=1;
else M4B=0;
}
}
else
{
M4A=0;
M4B=0;
}
if(i>=100)i=0;
}
}
* 1. void ser() interrupt 4
* 1. {
* 1. if(RI==0) return;
* 1. ES=0; //关闭口中断
* 1. RI=0; //清除口接收标志位
* 1. command=SBUF; //读取字符
* 1. switch(command)
* 1. {
* 1. case 'a':{ //向前:左右轮同时正传
* 1. // Left_moto_go ; //左电机往前走
* 1. // Right_moto_go ; //右电机往前走
* 1. // gg++;
* 1. break;
* 1. }
* 1. case 'b':{
* 1. //向后:左右轮同时反传
* 1. Left_moto_back ; //左电机往后走
* 1. Right_moto_back ; //右电机往后走
* 1. gg=aa(gg);
* 1. break;
* 1. }
case 'c':{ //向左:左轮后转,右轮正传
Left_moto_back ; //左电机往后走
Right_moto_go ; //右电机往前走
break;
}
case 'd':{ //向右:右轮后转,左轮正传
Left_moto_go ; //左电机往前走
Right_moto_back ; //右电机后走
break;
}
case 'e':{ //停转
Left_moto_Stop ; //左电机往前走
Right_moto_Stop ; //右电机往前走
break;
}
case '1':{ //云台向下移动
SEV_count++;
// SEV_PWM_count=1;
if(SEV_count>=23)SEV_count=23; //已经是180度,则保持
break;
}
case '2':{ //云台向上移动
SEV_count--;
// SEV_PWM_count=1;
if(SEV_count<=4)SEV_count=5; //已经是180度,则保持
break;
}
case '3':{ //左后转大弯
SEH_count++;
// SEH_PWM_count=1;
if(SEH_count>=23)SEH_count=23; //已经是180度,则保持
break;
}
case '4':{ //云台向右移动
SEH_count--;
// SEH_PWM_count=1;
if(SEH_count<=4)SEH_count=5; //已经是180度,则保持
break;
}
case '5':{ //云台回正
SEH_count=15;
SEV_count=14;
break;
}
case 'f':{ //开摄像头灯
LEMP=1;
break;
}
case 'g':{ //关摄像头灯
LEMP=0;
break;
}
case 'h':{ //开摄像头灯
BEEP=0;
break;
}
default:
{
Left_moto_Stop;
Right_moto_Stop;
BEEP=1;
break;
}
count=0; //20ms周期从新开始
}
ES=1; //允许串口中断
}
第一次发帖求助,我不知道这么发有没有什么错误的地方,如有请指正,我尽快修改。谢谢~