用51单片机像蓝牙模块发AT指令蓝牙模块没反应
本来会返回“OK”的
用电脑发可以收到返回
付程序:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar a[4]={0,0,0,0},i;
void delay(uint a)
{
uint b,c;
for(b=a;b>0;b--)
for(c=110;c>0;c--);
}
void main()
{
TMOD=0x20;
TH1=0xfd;
TH2=0xfd;
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
delay(100);
ES=1;
i=0;
SBUF='A';
while(!TI);
TI=0;
SBUF='T';
while(!TI);
TI=0;
SBUF='\r';
while(!TI);
TI=0;
SBUF='\n';
while(!TI);
TI=0;
delay(3000);
if(a[0]=='O'&&a[1]=='K')
{
P1=0;
i=0;
}
while(1);
}
void ab() interrupt 4
{
if(RI)
{
a=SBUF;
i++;
RI=0;
}
}
————————————————————————————————————————
附上调试好的代码
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define TZ P1=0;
sbit p1_0=P1^0;
sbit p1_1=P1^1;
sbit p1_2=P1^2;
sbit p1_3=P1^3;
bit time=0;
char direc=-1;
void ting()
{
p1_0=1;
p1_1=1;
p1_2=1;
p1_3=1;
}
void qian()
{
p1_0=1;
p1_1=0;
p1_2=1;
p1_3=0;
}
void hou()
{
p1_0=0;
p1_1=1;
p1_2=0;
p1_3=1;
}
void zuo()
{
p1_0=1;
p1_1=0;
p1_2=0;
p1_3=1;
}
void you()
{
p1_0=0;
p1_1=1;
p1_2=1;
p1_3=0;
}
void zuo1()
{
p1_0=1;
p1_1=0;
p1_2=1;
p1_3=1;;
}
void you1()
{
p1_0=1;
p1_1=1;
p1_2=1;
p1_3=0;
}
void zuo2()
{
p1_0=0;
p1_1=1;
p1_2=1;
p1_3=1;
}
void you2()
{
p1_0=1;
p1_1=1;
p1_2=0;
p1_3=1;
}
void delay(uint a)
{
uint b,c;
for(b=a;b>0;b--)
for(c=110;c>0;c--);
}
void main()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1; // T1
REN=1;
SM0=0;
SM1=1; //串口
EA=1; //中断
ES=1;
delay(1000);
while(1)
{
if(time)
{
ting();
delay(500);
direc=-1;
time=0;
}
else
switch (direc)
{
case -1: ting(); break;
case 0: you2(); break;
case 1: you1(); break;
case 2: zuo2(); break;
case 3: zuo1(); break;
case 4:
case 9: qian(); break;
case 5:
case 10: hou(); break;
case 6:
case 11: zuo(); break;
case 7:
case 12: you(); break;
}
}
}
void ab() interrupt 4
{
char sbuf;
sbuf=SBUF;
if(direc>7)
{
if(direc==8)
switch (sbuf)
{
case 'f': direc+=1;break;
case 'b': direc+=2; break;
case 'l': direc+=3; break;
case 'r': direc+=4; break;
}
else if(sbuf=='0')
direc-=5;
}
else
{
switch (sbuf)
{
case '0': break;
case '1': direc=8; break;
case 'f': direc=1; break;
case 'b': direc=0; break;
case 'l': direc+=2; break;
case 'r': direc+=0; break;
case 's': direc=-1; break;
default :time=1;
}
}
RI=0;
}
|