#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit a1=P0^0; //电机
sbit a2=P0^1;
sbit b1=P0^2;
sbit b2=P0^3;
uchar Buffer=0;
uint i=0,j,UARTCount=0,n=1;
uchar flag=0;
uchar Tempdata[4];
void delay(uint i) //延时函数
{
while(i--);
}
void Com_init() //串口初始化
{
TMOD=0x20;
SCON=0x50;
TH1=0xFD;
TL1=0xFD;
ES=1;
EA=1;
TR1=1;
}
void motor_forward() //前进
{
P1=0X05;
delay(10000);
}
void motor_backward()
{
P1=0x0a;
delay(10000);
}
void motor_turnleft()
{
P1=0x06;
delay(10000);
}
void motor_turnright()
{
P1=0x09;
delay(10000);
}
void main()
{
Com_init();
while(1)
{
if(0xff==Tempdata[0]&&0xff==Tempdata[3])
{
switch(Tempdata[2])
{
case 0x00: motor_stop();break;
case 0x01: motor_forward();break;
case 0x02: motor_backward();break;
case 0x03: motor_Turnleft();break;
case 0x04: motor_Turnright();break;
default : break;
}
}
}
}
void uart() interrupt 4
{
uchar temp;
if(1==RI)
{
RI=0;
ES=1;
temp=SBUF;//接收数据
if(0xff==temp&&UARTCount<2)
{
Tempdata[0]=0xff;
UARTCount++;//接到0xff的次数
}
else
{
Tempdata[n]=temp;
n++;
}
if(UARTCount==2)
{
UARTCount=0;
n=1;
temp="";
Tempdata[3]=0xff;
}
ES=1;
}
}
为什么下位机就是收不到上位机如"FF 00 01 00 FF"的控制信号呢?
|