#include<reg51.h>
/#define uint unsigned int
/#define uchar unsigned char
/*****延时程序******/
void delay(uint times){
uint i;
while(times--)
for(i=0;i<75;i++);
}
/***电动机控制****/
void motor_go(){
P2=0x3A;//00011010
delay(20);
}
void motor_back(){
P2=0x35;//00110101;
delay(20);
}
void motor_left(){
P2=0x39;//00111001;
delay(20);
}
void motor_right(){
P2=0x36;//00110110;
delay(20);
}
/*****************避障程序***************/
void bizhang(){
uint a=!(P3&&0xC0);//00000011
switch(a){
case 0xc0:
motor_left();
break;
case 0x80:
motor_left();
break;
case 0x40:
motor_right();
break;
case 0x00:
motor_go();
default:
break;
}
}
/****循迹程序***/
void xunji(){
uint b=(P3&&0x30);//00110000
switch(b){
case 0x10:
motor_left();
break;
case 0x20://
motor_right();
break;
case 0x30:
motor_go();
break;
default:
motor_left();
break;
}
}
void main(){
uint button=0;
while(1){
/*******按一下按钮为避障,两下循迹*******/
if(P3^3==0){
delay(100);
if(P3^3==1){
button=!button;
}
}
switch(button){
case 0:
bizhang();
break;
case 1:
xunji();
break;
default:
break;
}
}
}
程序还不完善,我努力修改