智能小车蓝牙控制以及pwm调速,红外避障程序在里面没反应,单独用红外模块有用
用用#include<reg52.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedints...
用
用#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
sbit ENA=P1^4;
sbit ENB=P1^5;
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
uchar left_pwm=35,right_pwm=35,i,j;//占空比标志
int speed;
uchar tmp,flag;
void delay(uint z)
{
uchar i,j;
for(j=z;j>0;j--)
for(i=0;i<121;i++);
}
void init()
{
TMOD=0x20;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
TH1=TL1=0xFD;
SCON=0x50;
PCON=0x00;
EA=1;
ET0=1;
TR0=1;
TR1=1;
ES=1;
ENA=1;
ENB=1;
}
void timer0(void)interrupt 1
{
i++;
j++;
if(i<=right_pwm){ENA=1;}
else ENA=0;
if(i==40){ENA=~ENA;i=0;}
if(j<=left_pwm){ENB=1;}
else ENB=0;
if(j==40){ENB=~ENB;j=0;}
TH0=(65536-100)/256;
TL0=(65536-100)%256;
}
void straight()
{
left_pwm=22+speed;
right_pwm=20+speed;
IN1=0;
IN2=1;
IN3=0;
IN4=1;
}
void left()
{
left_pwm=20;
right_pwm=10;
IN1=0;
IN2=1;
IN3=0;
IN4=0;
}
void right()
{
right_pwm=20;
left_pwm=10;
IN1=0;
IN2=0;
IN3=0;
IN4=1;
}
void back()
{
right_pwm=20+speed;
left_pwm=22+speed;
IN1=1;
IN2=0;
IN3=1;
IN4=0;
}
void stop()
{
right_pwm=0;
left_pwm=0;
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
void bizhang()
{
flag=1;
while(flag==1)
{
if(RI==1)
{
RI=0;
tmp=SBUF;
}
if(tmp==9)
{
flag=0;
}
if(P0^0==1)
{
straight();
}
if(P0^0==0)
{
back();
}
}
}
void work()
{
switch(tmp)
{
case 8:
bizhang();
break;
case 6:
speed=5;
break;
case 7:
speed=-5;
break;
case 1:
straight();
break;
case 2:
back();
break;
case 3:
left();
break;
case 4:
right();
break;
case 5:
stop();
break;
default:
break;
}
}
void main()
{
init();
while(1)
{
if(RI==1)
{
RI=0;
tmp=SBUF;
}
work();
}
}
看看代码哪里有问题 展开
用#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
sbit ENA=P1^4;
sbit ENB=P1^5;
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
uchar left_pwm=35,right_pwm=35,i,j;//占空比标志
int speed;
uchar tmp,flag;
void delay(uint z)
{
uchar i,j;
for(j=z;j>0;j--)
for(i=0;i<121;i++);
}
void init()
{
TMOD=0x20;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
TH1=TL1=0xFD;
SCON=0x50;
PCON=0x00;
EA=1;
ET0=1;
TR0=1;
TR1=1;
ES=1;
ENA=1;
ENB=1;
}
void timer0(void)interrupt 1
{
i++;
j++;
if(i<=right_pwm){ENA=1;}
else ENA=0;
if(i==40){ENA=~ENA;i=0;}
if(j<=left_pwm){ENB=1;}
else ENB=0;
if(j==40){ENB=~ENB;j=0;}
TH0=(65536-100)/256;
TL0=(65536-100)%256;
}
void straight()
{
left_pwm=22+speed;
right_pwm=20+speed;
IN1=0;
IN2=1;
IN3=0;
IN4=1;
}
void left()
{
left_pwm=20;
right_pwm=10;
IN1=0;
IN2=1;
IN3=0;
IN4=0;
}
void right()
{
right_pwm=20;
left_pwm=10;
IN1=0;
IN2=0;
IN3=0;
IN4=1;
}
void back()
{
right_pwm=20+speed;
left_pwm=22+speed;
IN1=1;
IN2=0;
IN3=1;
IN4=0;
}
void stop()
{
right_pwm=0;
left_pwm=0;
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
void bizhang()
{
flag=1;
while(flag==1)
{
if(RI==1)
{
RI=0;
tmp=SBUF;
}
if(tmp==9)
{
flag=0;
}
if(P0^0==1)
{
straight();
}
if(P0^0==0)
{
back();
}
}
}
void work()
{
switch(tmp)
{
case 8:
bizhang();
break;
case 6:
speed=5;
break;
case 7:
speed=-5;
break;
case 1:
straight();
break;
case 2:
back();
break;
case 3:
left();
break;
case 4:
right();
break;
case 5:
stop();
break;
default:
break;
}
}
void main()
{
init();
while(1)
{
if(RI==1)
{
RI=0;
tmp=SBUF;
}
work();
}
}
看看代码哪里有问题 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询