📄 main.c
字号:
//设置电机控制变量
LA = 0;
LB = 0;
RA = 0;
RB = 0;
//设置指示灯
MiddleLight = Off;
LeftLight = Off;
RightLight = Off;
}
/********* 前进模块 ***********/
void RunForward(void)
{
LA = 1;
LB = 0;
RA = 1;
RB = 0;
//设置指示灯
MiddleLight = On;
LeftLight = Off;
RightLight = Off;
}
/********* 后退模块 ***********/
void RunBackward(void)
{
//设置电机控制变量
LA = 0;
LB = 1;
RA = 0;
RB = 1;
//设置指示灯
MiddleLight = On;
LeftLight = On;
RightLight = On;
}
/********* 左转模块 ***********/
void RunLeft(void)
{
//设置电机控制变量
LA = 0;
LB = 1;
RA = 1;
RB = 0;
//设置指示灯
MiddleLight = Off;
LeftLight = On;
RightLight = Off;
}
/********* 原地左转模块 *******/
void RunLeftRoundLeft(void)
{
//设置电机控制变量
LA = 0;
LB = 0;
RA = 1;
RB = 0;
//设置指示灯
MiddleLight = Off;
LeftLight = On;
RightLight = Off;
}
/********* 右转模块 ***********/
void RunRight(void)
{
//设置电机控制变量
LA = 1;
LB = 0;
RA = 0;
RB = 1;
//设置指示灯
MiddleLight = Off;
LeftLight = Off;
RightLight = On;
}
/********* 原地右转模块 *******/
void RunRightRoundRight(void)
{
//设置电机控制变量
LA = 1;
LB = 0;
RA = 0;
RB = 0;
//设置指示灯
MiddleLight = Off;
LeftLight = Off;
RightLight = On;
}
void run_md(void)
{ if(run_mode==1)
RunStop();
else
if(run_mode==2)
RunForward();
else
if(run_mode==3)
RunLeft();
else
if(run_mode==4)
RunRight();
}
void ad_to_mode(void)
{if((a[0]<240)&&(a[1]<240))
{if(a[0]>a[1])
{
if((a[0]-a[1])>25)
run_mode=3;
else
run_mode=2;
}
else
if(a[1]>a[0])
{if((a[1]-a[0])>25)
run_mode=4;
else
run_mode=2;
}
}
else
if((a[0]>240)&&(a[1]<240))
{
if(a[1]>220)
run_mode=4;
else
if(a[1]<180)
run_mode=3;
else
if((a[1]>180)&&(a[1]<220))
run_mode=2;
}
else
if((a[1]>240)&&(a[0]<240))
{
if(a[0]>220)
run_mode=3;
else
if(a[0]<180)
run_mode=4;
else
if((a[0]>180)&&(a[0]<220))
run_mode=2;
}
}
void RunLeft1(int timebefore, int timebehind)
{
if(flag2 <= timebefore) {
LA = 0;
LB = 1;
RA = 1;
RB = 0;
//设置指示灯
MiddleLight = Off;
LeftLight = On;
RightLight = Off;
} else if(flag2 <= timebefore+timebehind) {
LA = 1;
LB = 0;
RA = 0;
RB = 0;
//设置指示灯
MiddleLight = On;
LeftLight = Off;
RightLight = Off;
} else{
flag2 = 0;
}
}
void RunRight1(int timebefore, int timebehind)
{
if(flag2 <= timebefore) {
LA = 1;
LB = 0;
RA = 0;
RB = 1;
//设置指示灯
MiddleLight = Off;
LeftLight = Off;
RightLight = On;
} else if(flag2 <= timebefore+timebehind) {
LA = 1;
LB = 0;
RA = 0;
RB = 0;
//设置指示灯
MiddleLight = On;
LeftLight = Off;
RightLight = Off;
} else{
flag2 = 0;
}
}
//main程序===================================================================
void main(void)
{
daley(100);//系统延时
MCU_init();//单片机初始化
EnableInterrupts;
beef_time=1;
//主控程序==============================================
//a[0]是下面左边的传感器,a[1]是下面右边的传感器;
//a[2]是上面前方传感器,a[3]是上面右边的传感器;>100表示白色
for(;;)
{ beef_conr();
AD_porm();
//run_md();
//ad_to_mode();
//if(a[0] < 100) {
// beef_time = 1;
//}
//RunForward();
if(a[0]<100 && a[1]>100) {
RunRight1(20,3);
} else if(a[0]>100 && a[1]<100){
RunLeft1(10,3);
} else if(a[0]<100 && a[1]<100){
RunForward();
} else if(a[0]>100 && a[1]>100){
RunForward();
}
//if(a[3]<100) { //正面右边传感器
// beef_time = 1; //无反射
//}
//if(a[2]>100) { //正前方传感器
// beef_time = 1; //无反射
//}
/*
switch(FlagRun){
case 0:
RunStop();
break;
case 1:
RunForward();
break;
case 3:
RunLeft1();
break;
case 4:
RunRight1();
break;
default:
RunStop();
}
*/
}
}
//下面的与应用程序无关不必去看==========================================================================
interrupt 15 void Timer15_Interrupt(void) { ;}
interrupt 14 void Timer14_Interrupt(void) { ;}
interrupt 13 void Timer13_Interrupt(void) { ;}
interrupt 12 void Timer12_Interrupt(void) { ;}
interrupt 11 void Timer11_Interrupt(void) { ;}
interrupt 10 void Timer10_Interrupt(void) { ;}
interrupt 9 void Timer9_Interrupt(void) { ;}
interrupt 8 void Timer8_Interrupt(void) { ;}
interrupt 7 void Timer7_Interrupt(void) { ;}
interrupt 5 void Timer5_Interrupt(void) { ;}
interrupt 4 void Timer4_Interrupt(void) { ;}
interrupt 3 void Timer3_Interrupt(void) { ;}
interrupt 1 void Timer1_Interrupt(void) { ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -