|
这是我代码
#include<reg52.h>
typedef unsigned int u16;
typedef unsigned char u8;
sbit int1=P0^0;
sbit int2=P0^1;
sbit int3=P0^2;
sbit int4=P0^3;
sbit kaiguan=P3^7;
u8 receiveData;
void delay(u8 a)
{
while(a--);
}
void Timer0Init()
{
TMOD|=0X01;
TH0=0X4c;
TL0=0X00;
ET0=1;
EA=1;
TR0=1;
}
void UsartInit()
{
SCON=0X50;
TMOD=0X20;
PCON=0X00;
TH1=0XFD;
TL1=0XFD;
ES=1;
EA=1;
TR1=1;
}
void forward()
{
int1=1;
int2=0;
int3=1;
int4=0;
}
void backward()
{
int1=0;
int2=1;
int3=0;
int4=1;
}
void turn_left()
{
int1=1;
int2=0;
int3=0;
int4=0;
}
void rotate_left()
{
int1=1;
int2=0;
int3=0;
int4=1;
}
void turn_right()
{
int1=0;
int2=0;
int3=1;
int4=0;
}
void rotate_right()
{
int1=0;
int2=1;
int3=1;
int4=0;
}
void stop()
{
int1=1;
int2=1;
int3=1;
int4=1;
}
void tip1()
{
rotate_left();
delay(3);
rotate_right();
delay(3);
}
void tip2()
{
rotate_left();
delay(6);
rotate_right();
delay(6);
}
void tip3()
{
turn_left();
delay(66);
}
void tip4()
{
turn_right();
delay(55);
}
void ctrl()
{
if(kaiguan==0)
{
while(1)
{
if(receiveData==0x01)
forward();
if(receiveData==0x02)
backward();
if(receiveData==0x03)
turn_left();
if(receiveData==0x04)
turn_right();
if(receiveData==0x05)
stop();
if(receiveData==0x06)
tip1();
if(receiveData==0x07)
tip2();
if(receiveData==0x08)
tip3();
if(receiveData==0x09)
tip4();
}
}
}
void main ()
{ Timer0Init();
UsartInit();
while(1);
}
void Timer0() interrupt 1
{
static u8 i;
TH0=0XFC;
TL0=0X18;
i++;
if(i==50)
{
i=0;
}
}
void Usart() interrupt 4
{
if(RI==1)
{
receiveData=SBUF;
RI=0;
}
ctrl();
}
|
|