蓝牙智能小车程序
#define uchar unsigned char
#define uint unsigned int
sbit IN1=P0^0;
sbit IN2=P0^1; //小车前左
sbit INT3=P0^2;
sbit INT4=P0^3; //小车前右
sbit ENA1=P0^4;
sbit ENB1=P0^5;
sbit IN12=P2^7;
sbit IN22=P2^6; //小车后左
sbit INT32=P2^5;
sbit INT42=P2^4; //小车后右
sbit ENA2=P2^3;
sbit ENB2=P2^2;
uint b=100,c=400,Mark=0;
uchar a;
void Delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=120;j>0;j--);
}
void time0() interrupt 1
{
if(++Mark==10)
Mark=0;
}
void time1() interrupt 3
{
TH1=0xFD;
TL1=0xFD;
}
void CX() interrupt 4
{
a=SBUF;
while(RI==0);
RI=0;
TI=0;
}
void main()
{
TMOD=0x21;
SCON=0x50;
TH0=0xD8;
TL0=0xF0;
TR0=1;
ET0=0;
TH1=0xFD;
TL1=0xFD;
TR1=1;
EA=1;
ET1=0;
ES=1;
switch(a)
{
case '1':b+=50,c-=50;break;
case '2':straight0();break;
case '3':straight1();break;
case '4':left();break;
case '5':right();break;
case '6':back();break;
case '7':stop();break;
}
}
请问一下,我的这个蓝牙智能小车的中断程序与初始对吗?
你这个小车是用PWM控制速度的吗?还是直接的高低点平
高低点平,用的是四个电机,问题是手机蓝牙传输数据后小车无反应,小车的电路正常,
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit IN1=P0^0;
sbit IN2=P0^1; //小车前左
sbit INT3=P0^2;
sbit INT4=P0^3; //小车前右
sbit ENA1=P0^4;
sbit ENB1=P0^5;
sbit IN12=P2^7;
sbit IN22=P2^6; //小车后左
sbit INT32=P2^5;
sbit INT42=P2^4; //小车后右
sbit ENA2=P2^3;
sbit ENB2=P2^2;
void right();
void left();
void straight0();
void straight1();
void stop();
void back();
uint b=100,c=400,Mark=0,d;
uchar a;
void Delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=120;j>0;j--);
}
void straight0()
{
ENA1=1;
ENB1=1;
IN1=1;
IN2=0;
INT3=1;
INT4=0;
ENA2=1;
ENB2=1;
IN12=1;
IN22=0;
INT32=1;
INT42=0;
}
void straight1()
{
ENA1=1;
ENB1=1;
IN1=1;
IN2=0;
INT3=1;
INT4=0;
ENA2=1;
ENB2=1;
IN12=1;
IN22=0;
INT32=1;
INT42=0;
Delay(100);
ENA1=0;
ENB1=0;
ENA2=0;
ENB2=0;
Delay(200);
ENA1=1;
ENB1=1;
IN1=1;
IN2=0;
INT3=1;
INT4=0;
ENA2=1;
ENB2=1;
IN12=1;
IN22=0;
INT32=1;
INT42=0;
Delay(200);
}
void stop()
{
ENA1=0;
ENB1=0;
ENA2=0;
ENB2=0;
}
void back()
{
ENA1=1;
ENB1=1;
IN1=0;
IN2=1;
INT3=0;
INT4=1;
ENA2=1;
ENB2=1;
IN12=0;
IN22=1;
INT32=0;
INT42=1;
}
void left()
{
ENA1=1;
ENB1=1;
IN1=1;
IN2=0;
INT3=1;
INT4=0;
ENA2=1;
ENB2=1;
IN12=1;
IN22=0;
INT32=1;
INT42=0;
Delay(b);
ENA1=1;
ENB1=1;
IN1=0;
IN2=0;
INT3=1;
INT4=0;
ENA2=1;
ENB2=1;
IN12=0;
IN22=0;
INT32=1;
INT42=0;
Delay(c);
while(1)
{
ENA1=1;
ENB1=1;
IN1=1;
IN2=0;
INT3=1;
INT4=0;
ENA2=1;
ENB2=1;
IN12=1;
IN22=0;
INT32=1;
INT42=0;
}
}
void right()
{
ENA1=1;
ENB1=1;
IN1=1;
IN2=0;
INT3=1;
INT4=0;
ENA2=1;
ENB2=1;
IN12=1;
IN22=0;
INT32=1;
INT42=0;
Delay(b);
ENA1=1;
ENB1=1;
IN1=1;
IN2=0;
INT3=0;
INT4=0;
ENA2=1;
ENB2=1;
IN12=1;
IN22=0;
INT32=0;
INT42=0;
Delay(c);
while(1)
{
ENA1=1;
ENB1=1;
IN1=1;
IN2=0;
INT3=1;
INT4=0;
ENA2=1;
ENB2=1;
IN12=1;
IN22=0;
INT32=1;
INT42=0;
}
}
void time0() interrupt 1
{
TH1=0xFD;
TL1=0xFD;
++Mark;
if(Mark==10)
Mark=0;
}
void time1() interrupt 3
{
TH1=0xFD;
TL1=0xFD;
}
void CX() interrupt 4
{
RI=0;
a=SBUF;
TI=0;
}
void main()
{
TMOD=0x21;
SCON=0x50;
TH0=0xD8;
TL0=0xF0;
TR0=1;
ET0=1;
TH1=0xFD;
TL1=0xFD;
TR1=1;
ET1=1;
EA=1;
ES=1;
while(1)
{
if(RI==1)
{ a=SBUF;
RI=0;
switch(a)
{
case '1':b+=50,c-=50;break;
case '2':straight0();break;
case '3':straight1();break;
case '4':left();break;;
case '5':right();break;;
case '6':back();break;
case '7':stop();break;
default :break;
}
}
}
}
9fef真心不错的技术
switch(a)
{
case '1':b+=50,c-=50;break;
case '2':straight0();break;
case '3':straight1();break;
case '4':left();break;;
case '5':right();break;;
case '6':back();break;
case '7':stop();break;
default :break;
}
这一段是利用串口接收到的字符来控制小车的动作,你先测试一下能不能正常接收呢?
不能,传输不同的数值,小车状态不变;问题主要在传输的a没有进行判定