⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyctlextra.c

📁 智能小车的往返行驶,单片机实现,使用红外信号进行路面信号的采集
💻 C
字号:
#include "SPCE061V004.h"
#define   FORWARD             0              //前进状态
#define   BACKWARD            1		         //后退状态
#define   STOP				  2				 //停止状态
#define   BLACKWIRE_B    1
#define   BLACKWIRE_C    2
#define   BLACKWIRE_D    3
#define   BLACKWIRE_E    4
#define   BLACKWIRE_F    5
#define   BLACKWIRE_G    6
#define   BLACKWIRE_H    7
#define   ADJUSTDISTANS  20
/*需要重新定义的输入数据口*/
#define   FORELEFT            1              //前左
#define   FORERIGHT           6              //前右
#define   BACKLEFT            0              //后左
#define   BACKRIGHT           7              //后右
#define   TOPLEFT             3              //上左
#define   TOPRIGHT            4              //上右,靠墙
#define   DISTANS_TEST        5              //轮子检测
#define   START               2              //开始键
#define   PHASE1_CURRENT      0x0800         //L298端口1,对应IOB11
#define   PHASE2_CURRENT      0x1000         //L298端口15,对应IOB12
#define   WHEELTURNONE        7              //辐条检测一次距离,单位:厘米 
typedef unsigned int uint;
extern uint SpeedValue;   //档位值,从1-100
extern uint Status;	      //记录和控制车前进,后退状态
extern uint Blackwire;	  //记录黑线数
extern uint S;			  //总路程,单位:厘米 	
extern uint Time;		  //总时间
extern uint key_array[8];
extern uint KeyUpFlag;
extern uint led[8];
void LEDdisplay( unsigned char , unsigned char  );
unsigned char  PointCTL(unsigned char );
void delay(uint );
void FaceBlackWire();
void Speed(uint speedvalue);
void ForeWheelForword();
void ForeWheelReverse();
void TurnLeft();
void TurnRight();
void Stop();
void Adjust();
void ForeBlackwireDetect();	      /*去时黑线检测*/ 
void BackBlackWireDetect();    /* 返回时黑线检测*/
uint StartFlag=0;
extern uint KeyUpFlagArray[8];
void Keycontrol()
{
     uint  s_buf;
if(Status== FORWARD)                      /*去时黑线检测*/
	       {
	        *P_Watchdog_Clear=1;
	        ForeBlackwireDetect();	 
		   }
if(Status== BACKWARD)                  /* 返回时黑线检测*/
          {
            *P_Watchdog_Clear=1;
  	    	BackBlackWireDetect();  
  	       }   
if(key_array[TOPLEFT] && (KeyUpFlagArray[TOPLEFT] || KeyUpFlag  ))
  	     {
  	     key_array[TOPLEFT]=0;
  	     KeyUpFlagArray[TOPLEFT]=0;
  	     }
if(key_array[TOPRIGHT]==0)           /*靠墙走*/
  	     {
  	     if(Status== FORWARD) TurnRight(); 
  	     if(Status== BACKWARD) TurnLeft(); 
  	     key_array[TOPRIGHT]=0;
  	     }
else if(key_array[TOPRIGHT] && (KeyUpFlagArray[TOPRIGHT] || KeyUpFlag  ))
         {
  	     if(Status== FORWARD)  TurnLeft(); 
  	     if(Status== BACKWARD) TurnRight(); 
  	     key_array[TOPRIGHT]=0;
  	     KeyUpFlagArray[TOPRIGHT]=0;
  	     }
if(key_array[DISTANS_TEST] && (KeyUpFlagArray[DISTANS_TEST] || KeyUpFlag  ))     /*辐条检测处理*/
  	     {
  	     S+=WHEELTURNONE; 
  	     s_buf=S;
  	     led[5]= s_buf/1000;
  	     s_buf= s_buf-led[5]*1000;
  	     led[6]= s_buf/100;
  	     s_buf= s_buf-led[6]*100;
  	     led[7]= s_buf/10;
  	     LEDdisplay( 0x05 ,led[5] ); 
  	     LEDdisplay( 0x06 ,PointCTL(led[6]) ); 
  	     LEDdisplay( 0x07 ,led[7] ); 
  	     *P_Watchdog_Clear=1;
  	     key_array[DISTANS_TEST]=0;
  	     KeyUpFlagArray[DISTANS_TEST]=0;
  	     
  	     }
if(key_array[START] && (KeyUpFlagArray[START] || KeyUpFlag  ))            /*开始键*/
  	     {
  	     Stop();
  	     StartFlag=1;
  	     key_array[START]=0;
  	     KeyUpFlagArray[START] =0;
  	     }
}
uint ss[6]={0,0,0,0,0,0};        //记录各个点之间的距离时间
uint tt[6]={0,0,0,0,0,0};
uint times[6]={0,0,0,0,0,0};
void ForeBlackwireDetect()	      /*去时黑线检测*/ 
{
  
	        if(key_array[FORELEFT ] && (KeyUpFlagArray[FORELEFT] || KeyUpFlag  ))
  	               {
  	                  if (times[0]==0)          /*Face B*/
  	                      {
  	                        ss[0]=S;
  	                        tt[0]=Time;
  	                        times[0]=1;
  	                        key_array[FORELEFT ]=0;
  	     			        Blackwire++;
  	     			        FaceBlackWire();
  	                      }
  	                  else  if(Blackwire==BLACKWIRE_B)  /*Face C*/
  	                       {
  	                        if (times[1]==0)
  	                          {
  	                            ss[1]=S;
  	                            tt[1]=Time;  	                      
  	                            key_array[FORELEFT ]=0;
  	                            if( (ss[1]-ss[0])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((ss[1]-ss[0])>40)
  	                                    {
  	                                    key_array[FORELEFT ]=0;
  	     			                    Blackwire++;
  	     			                    FaceBlackWire();
  	     			                    times[1]=1;
  	     			                    }
  	                          }
  	                        }
  	                  else  if(Blackwire==BLACKWIRE_C)  /*Face D*/
  	                       {
  	                        if (times[2]==0)
  	                          {
  	                            ss[2]=S;
  	                            tt[2]=Time;  	                      
  	                            key_array[FORELEFT ]=0;
  	                            if( (ss[2]-ss[1])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((ss[2]-ss[1])>280)
  	                                    {
  	                                    key_array[FORELEFT ]=0;
  	     			                    Blackwire++;
  	     			                    FaceBlackWire();
  	     			                    times[2]=1;
  	     			                    }
  	                          }
  	                        }
  	                     else  if(Blackwire==BLACKWIRE_D)   /*Face E*/
  	                       {
  	                        if (times[3]==0)
  	                          {
  	                            ss[3]=S;
  	                            tt[3]=Time;  	                      
  	                            key_array[FORELEFT ]=0;
  	                            if( (ss[3]-ss[2])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((ss[3]-ss[2])>150)
  	                                    {
  	                                    key_array[FORELEFT ]=0;
  	     			                    Blackwire++;
  	     			                    FaceBlackWire();
  	     			                    times[3]=1;
  	     			                    }
  	                          }
  	                        }
  	                     else  if(Blackwire==BLACKWIRE_E)   /*Face F*/
  	                       {
  	                        if (times[4]==0)
  	                          {
  	                            ss[4]=S;
  	                            tt[4]=Time;  	                      
  	                            key_array[FORELEFT ]=0;
  	                            if( (ss[4]-ss[3])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((ss[4]-ss[3])>280)
  	                                    {
  	                                    key_array[FORELEFT ]=0;
  	     			                    Blackwire++;
  	     			                    FaceBlackWire();
  	     			                    times[4]=1;
  	     			                    }
  	                          }
  	                        }
  	                      else  if(Blackwire==BLACKWIRE_F)   /*Face G*/
  	                       {
  	                        if (times[5]==0)
  	                          {
  	                            ss[5]=S;
  	                            tt[5]=Time;  	                      
  	                            key_array[FORELEFT ]=0;
  	                            if( (ss[5]-ss[4])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((ss[5]-ss[4])>40)
  	                                    {
  	                                    key_array[FORELEFT ]=0;
  	     			                    Blackwire++;
  	     			                    FaceBlackWire();  	     			                  
  	     			                    times[5]=1;
  	     			                    }
  	                          }
  	                        }
  	                  
  	             KeyUpFlagArray[FORELEFT]=0;   			 
  	     			}
  	 		if(key_array[FORERIGHT] && (KeyUpFlagArray[FORERIGHT] || KeyUpFlag  ))
  	     	     	{
  	     			  key_array[FORERIGHT]=0;
  	     			  KeyUpFlagArray[FORERIGHT]=0;
		  	        }
}
uint sss[6]={0,0,0,0,0,0};
uint ttt[6]={0,0,0,0,0,0};
uint timess[6]={0,0,0,0,0,0};
void BackBlackWireDetect()    /* 返回时黑线检测*/
{
if(key_array[BACKLEFT] && (KeyUpFlagArray[BACKLEFT] || KeyUpFlag  ))
  	{
  	     		   if (Blackwire==BLACKWIRE_H)      /*Face G*/
  	                      {  	                      
  	                           sss[0]=S;
  	                           ttt[0]=Time;
  	                           timess[0]=1;
  	                           key_array[BACKLEFT ]=0; 
  	                           Blackwire--; 	     			
  	     			           FaceBlackWire();
  	                      }
  	               else  if(Blackwire==BLACKWIRE_G)     /*Face F*/
  	                       {
  	                        if (timess[1]==0)
  	                          {
  	                            sss[1]=S;
  	                            ttt[1]=Time;  	                      
  	                            key_array[BACKLEFT ]=0; 
  	                            if( (sss[1]-sss[0])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((sss[1]-sss[0])>40)
  	                                    {
  	                                    key_array[BACKLEFT ]=0; 
  	     			                    Blackwire--;
  	     			                    FaceBlackWire();
  	     			                    timess[1]=1;
  	     			                    }
  	                          }
  	                        }
  	     		 else  if(Blackwire==BLACKWIRE_F)     /*Face E*/
  	                       {
  	                        if (timess[2]==0)
  	                          {
  	                            sss[2]=S;
  	                            ttt[2]=Time;  	                      
  	                            key_array[BACKLEFT ]=0; 
  	                            if( (sss[2]-sss[1])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((sss[2]-sss[1])>280)
  	                                    {
  	                                    key_array[BACKLEFT ]=0; 
  	     			                    Blackwire--;
  	     			                    FaceBlackWire();
  	     			                    timess[2]=1;
  	     			                    }
  	                          }
  	                        }
  	                else  if(Blackwire==BLACKWIRE_E)     /*Face D*/
  	                       {
  	                        if (timess[3]==0)
  	                          {
  	                            sss[3]=S;
  	                            ttt[3]=Time;  	                      
  	                            key_array[BACKLEFT ]=0; 
  	                            if( (sss[3]-sss[2])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((sss[3]-sss[2])>150)
  	                                    {
  	                                    key_array[BACKLEFT ]=0; 
  	     			                    Blackwire--;
  	     			                    FaceBlackWire();
  	     			                    timess[3]=1;
  	     			                    }
  	                          }
  	                        }
  	                  else  if(Blackwire==BLACKWIRE_D)     /*Face C*/
  	                       {
  	                        if (timess[4]==0)
  	                          {
  	                            sss[4]=S;
  	                            ttt[4]=Time;  	                      
  	                            key_array[BACKLEFT ]=0; 
  	                            if( (sss[4]-sss[3])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((sss[4]-sss[3])>280)
  	                                    {
  	                                    key_array[BACKLEFT ]=0; 
  	     			                    Blackwire--;
  	     			                    FaceBlackWire();
  	     			                    timess[4]=1;
  	     			                    }
  	                          }
  	                        }
  	     		      else  if(Blackwire==BLACKWIRE_C)     /*Face B*/
  	                       {
  	                        if (timess[5]==0)
  	                          {
  	                            sss[5]=S;
  	                            ttt[5]=Time;  	                      
  	                            key_array[BACKLEFT ]=0; 
  	                            if( (sss[5]-sss[4])< ADJUSTDISTANS)
  	                                  Adjust();
  	                            else if((sss[5]-sss[4])>35)
  	                                    {
  	                                    key_array[BACKLEFT ]=0; 
  	     			                    Blackwire--;
  	     			                    FaceBlackWire();
  	     			                    timess[5]=1;
  	     			                    }
  	                          }
  	                        }
  KeyUpFlagArray[BACKLEFT]=0;	     	
  }
if(key_array[BACKRIGHT] && (KeyUpFlagArray[BACKRIGHT] || KeyUpFlag  ))
  	     		{
  	     		key_array[BACKRIGHT]=0;
  	     		KeyUpFlagArray[BACKRIGHT]=0;
  	     		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -