|
避障智能小車(chē)電路原理圖如下:
200983022524847934.gif (12.43 KB, 下載次數(shù): 154)
下載附件
2017-5-16 20:31 上傳
20140512150403-402811096.jpg (30.08 KB, 下載次數(shù): 126)
下載附件
單片機(jī)
2017-5-16 20:31 上傳
單片機(jī)源程序如下:
- 附錄B
- 按照預(yù)定的功能,系統(tǒng)實(shí)現(xiàn)預(yù)定的功能的程序如下所示:
- #include <intrins.h>
- #include <AT89X52.h>
- /********************************************************************************\
- ** 宏定義區(qū) **
- \********************************************************************************/
- /*------------------------------- LCD模塊 ------------------------------------*/
- #define LCD_RW P2_6 //讀寫(xiě)控制端
- #define LCD_RS P2_7 //數(shù)據(jù)命令選擇端
- #define LCD_E P2_5 //執(zhí)行使能端
- #define LCD_Data P1 //P1口
- #define Write 0x00 //低電平寫(xiě)入
- #define Read 0x01 //高電平讀出
- #define Data 0x01 //高電平選擇數(shù)據(jù)
- #define Cmd 0x00 //低電平選擇命令
- #define Enable 0x00 //躍變到低電平時(shí)執(zhí)行命令
- #define Disable 0x01
- #define True 0x01
- #define False 0x00
- #define LCD_Init 0x38 //初始化模式
- #define LCD_DispCtr 0x0C //開(kāi)顯示及光標(biāo)設(shè)置
- #define LCD_CloseCtr 0x08 //關(guān)顯示
- #define LCD_CLS 0x01 //清屏幕
- #define LCD_EnterSet 0x06 //顯示光標(biāo)
- #define Busy P1_7 //忙信號(hào)
- /*-------------------------- 測(cè)速/測(cè)距/測(cè)時(shí)模塊 -------------------------------*/
- #define CircleLength 0.132 //小車(chē)轉(zhuǎn)一輪的長(zhǎng)度為.132m
- /*-------------------------------- 控速模塊 -----------------------------------*/
- #define P03 P0_3 //后電機(jī)
- #define P04 P0_4 //后電機(jī)
- #define P01 P0_1 //前電機(jī)
- #define P02 P0_2 //前電機(jī)
- #define P31 P0_5 //控制液晶背光
- #define P33 P3_3
- /*------------------------------ 菜單選擇模塊 ---------------------------------*/
- #define Line 0x00 //0代表直線模式
- #define Curve 0x01 //1代表S型模式
- #define Normal 0x00 //0 代表正常速度
- #define Low 0x01 //1 代表低速
- #define High 0x02 //2 代表高速
- /*********************************************************************************\
- ** 全局函數(shù)聲明區(qū) **
- \*********************************************************************************/
- /*------------------------------- LCD模塊 -------------------------------------*/
- void LCDInit(void); //LCD初始化
- void SetWriteCmd(void); //設(shè)置寫(xiě)命令模式
- void SetReadCmd(void); //設(shè)置讀命令模式
- void SetWriteData(void); //設(shè)置寫(xiě)數(shù)據(jù)模式
- void WriteCmd(char cmd); //寫(xiě)命令
- void WriteData(char ddata); //寫(xiě)數(shù)據(jù)
- void ExecuteCmd(void); //執(zhí)行命令
- void SetXY(char x,char y); //定位顯示地址
- void DisplaySingleChar(char x,char y,char cchar); //顯示單個(gè)字符
- void DisplayString(char x,char y,char *str); //顯示一段字符串
- void Delay(unsigned int time); //延時(shí)主程序
- void DelayUs(unsigned int time); //延時(shí)子程序
- bit IsBusy(void); //判斷忙標(biāo)志函數(shù)
- void DisplayTime(void); //顯示時(shí)間
- void DisplayAVGSpeed(void); //顯示平均速度
- void DisplayDistance(void); //顯示路程
- /*-------------------------- 測(cè)速/測(cè)距/測(cè)時(shí)模塊 -------------------------------*/
- void INTInit(void); //所有中斷初始化
- void SpeedINT(void); //測(cè)速中斷
- void ComputeTime(void);
- void ComputeSpeedANDDistance(void); //計(jì)算速度和距離
- /*-------------------------------- 控速模塊 -----------------------------------*/
- void CtrSpeedINT(void); //控速中斷
- void Time0INT(void);
- void Time1INT(void); //控速單位時(shí)間中斷
- void Clock0_Init(void); //時(shí)鐘中斷初始化
- void Clock1_Init(void); //時(shí)鐘中斷初始化
- void CtrSpeed(void);
- /********************************************************************************\
- ** 全局變量區(qū) **
- \********************************************************************************/
- float SpeedCount = 0; //測(cè)速計(jì)數(shù)脈沖
- float Speed = 0.0;
- float Distance = 0.0;
- char Time1INTCount=0; //T1中斷時(shí)間計(jì)時(shí)
- float PassTime=0.00; //小車(chē)行走的時(shí)間
- short IsT0INT=1;
- bit IsT1INT; //判斷T1是否已經(jīng)響應(yīng)中斷
- short IsT0INT2=1;
- char Thx[5]={0xf4,0xf4,0xc5,0xf4,0xff}; //3ms,3ms,15ms,3ms
- char Tlx[5]={0x48,0x48,0x68,0x48,0xff};
- char Thx0=0xd8;
- char Tlx0=0xf0;
- char Thx1=0xb1; //20ms
- char Tlx1=0xe0;
- short Round=0;
- short Back=0;
- short Back0=0;
- bit Backid;
- bit Stop=0;
- char Area0=0;
- char Area1=0; //區(qū)域變量
- char LowSpeedArea1StartTime;
- char LowSpeedArea1EndTime;
- char HighSpeedAreaEndTime;
- char LowSpeedArea2EndTime;
- char LowSpeedArea1PassTime=0; //第一個(gè)低速區(qū)通過(guò)時(shí)間
- char HighSpeedAreaPassTime=0; //高速區(qū)通過(guò)時(shí)間
- char LowSpeedArea2PassTime=0; //第二個(gè)低速區(qū)通過(guò)時(shí)間
- char ReadyToGo=4; //倒計(jì)時(shí)
- char flag;
- bit Roundid=0;
- char Nocurve=0;
- char ChangeFlag;
- char Mode;
- bit Running;
- bit SelectedAll; //模式和速度是否選擇完畢標(biāo)志
- bit IsSelectingMode; //模式選擇標(biāo)志
- bit IsSelectingSpeed; //速度選擇標(biāo)志
- bit ModeSelected; //已經(jīng)被選擇的模式標(biāo)志
- char SelectedMode=10; //模式選擇是否完畢標(biāo)志
- bit Next; //Next鍵標(biāo)志
- bit SpeedSelected; //已經(jīng)被選中的速度方案標(biāo)志
- char SelectedSpeed; //速度選擇是否完畢標(biāo)志
- bit ChoosingDisplay; //人工選擇菜單開(kāi)始標(biāo)志
- bit SelectedShow; //顯示選擇標(biāo)志
- bit SelectedReturn; //返回選擇標(biāo)志
- bit Selected; //確定/返回鍵選擇標(biāo)志
- bit ReturnSelection; //返回鍵啟用標(biāo)志
- bit AVGSpeedShow; //平均速度顯示標(biāo)志
- bit TotalDistanceShow; //總路程顯示標(biāo)志
- bit ReturnMain; //返回主菜單標(biāo)志
- bit AutoDisplay; //自動(dòng)顯示標(biāo)志
- bit GoToChoosingDisplay; //人工選擇標(biāo)志
- bit AutoMode=0;
- char PassLineID=0;
- char PassLine=0;
- float PrepareDistance;
- float FirstDistance;
- float SecondDistance;
- float ThirdDistance;
- int FirstHigh;
- int SecondHigh;
- int ThirdHigh;
- float Rate=1.25; //5ms時(shí)的速度
- float Count=4; //時(shí)間的倍數(shù)
- /********************************************************************************\
- ** 全局函數(shù)實(shí)現(xiàn)區(qū) **
- \********************************************************************************/
- /*-------------------------------- 主函數(shù) ------------------------------------*/
- void main()
- {
- P01=0;
- P02=0;
- P03=0;
- P04=0;
- P31=1; //單片機(jī)復(fù)位,背光開(kāi)
- Delay(40); //延時(shí)等待LCD啟動(dòng)
- LCDInit(); //初始化LCD
- DisplayString(0x0,0," Starting... ");
- DisplayString(0x0,1,"Designed By 202");
- Delay(300);
- WriteCmd(LCD_CLS);
-
- EA=1; //開(kāi)總中斷
- EX0=1; //開(kāi)INT0中斷
- IT0=1; //INTO邊沿觸發(fā)
- EX1=1; //開(kāi)INT1中斷
- IT1=1; //INT1邊沿觸發(fā)
- SelectedAll=False; //開(kāi)始模式和速度選擇
-
- /*------------------------------ 模式選擇 ---------------------------------*/
- DisplayString(0x0,0,"Choose The Mode ");
- DisplayString(0x0,1,"you want. ");
- Delay(50);
- WriteCmd(LCD_CLS);
-
- IsSelectingMode=True;
- while(1)
- {
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Line Mode ");
- DisplayString(0x0,1,"Next Yes");
- Delay(300); //延時(shí)消除抖動(dòng)
- while (1) //不斷檢測(cè)中斷,直到按確定鍵或是NEXT鍵
- {
- if (Next==True) //如果按Next鍵則直接跳出
- break;
- if (ModeSelected==True) //如果按確定鍵則設(shè)置模式為L(zhǎng)ine并跳出
- {
- SelectedMode=Line;
- break;
- } //如果什么鍵都沒(méi)有按下,那么一直顯示等待
- }
- if (ModeSelected==True) //按下了確定鍵,退出模式選擇
- {
- IsSelectingMode=False;
- break;
- }
- if (Next==True) //按下了Next鍵,顯示下一個(gè)菜單項(xiàng)
- {
- Next=False;
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Curve Mode ");
- DisplayString(0x0,1,"Next Yes");
-
- Delay(300); //延時(shí)消除抖動(dòng)
- while(1) //不斷檢測(cè)中斷,直到按確定鍵或是Next鍵
- {
- if (Next==True) //如果再一次按下Next鍵,則跳出
- break;
- if (ModeSelected==True) //如果按下確定鍵,則設(shè)置模式為Curve,并跳出
- {
- SelectedMode=Curve;
- break;
- }
- }
- }
-
- if (ModeSelected==True) //按下了確定鍵,退出模式選擇
- {
- IsSelectingMode=False;
- break;
- }
- if (Next==True) //再一次按下了Next鍵,則循環(huán)模式選擇
- {
- Next=False;
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," AutoMode ");
- DisplayString(0x0,1,"Next Yes");
- Delay(300); //延時(shí)消除抖動(dòng)
- while(1)
- {
- if (Next==True)
- break;
- if (ModeSelected==True)
- {
- AutoMode=1;
- break;
- }
- }
- }
- if (ModeSelected==True)
- {
- IsSelectingMode=False;
- break;
- }
- if (Next==True)
- {
- Next=False;
- continue;
- }
- }
-
- Delay(50);
- WriteCmd(LCD_CLS);
-
- /*------------------------------ 速度選擇 ---------------------------------*/
- if (SelectedMode==Line && AutoMode==0)
- {
- DisplayString(0x0,0," Now Choose a ");
- DisplayString(0x0,1," kind of Speed ");
- Delay(50);
- WriteCmd(LCD_CLS);
-
- IsSelectingSpeed=True;
- while(1)
- {
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Normal Speed ");
- DisplayString(0x0,1,"Next Yes");
- Delay(300); //延時(shí)消除抖動(dòng)
-
- while(1)
- {
- if (Next==True) //如果按Next鍵則直接跳出
- break;
- if (SpeedSelected==True) //如果按確定鍵則設(shè)置速度為Normal并跳出
- {
- Thx[0]=0xec;Tlx[0]=0x78; //5ms
- Thx[1]=0xf0;Tlx[1]=0x60; //4ms
- Thx[2]=0x8a;Tlx[2]=0xd0; //30ms
- Thx[3]=0xf4;Tlx[3]=0x48; //3ms
- SelectedSpeed=Normal;
- break;
- } //如果什么鍵都沒(méi)有按下,那么一直顯示等待
- }
-
- if (SpeedSelected==True) //按下了確定鍵,退出速度選擇
- {
- IsSelectingSpeed=False;
- break;
- }
-
- if (Next==True)
- {
- Next=False;
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Low Speed ");
- DisplayString(0x0,1,"Next Yes");
-
- Delay(300); //延時(shí)消除抖動(dòng)
- while(1)
- {
- if (Next==True) //如果再一次按下Next鍵,則跳出
- break;
- if (SpeedSelected==True) //如果按下確定鍵,則設(shè)置速度為L(zhǎng)ow,并跳出
- {
- SelectedSpeed=Low; //這里沒(méi)有速度設(shè)置,因?yàn)槟J(rèn)速度就是Low
- break;
- }
- }
- }
-
- if (SpeedSelected==True) //按下了確定鍵,退出速度選擇
- {
- IsSelectingSpeed=False;
- break;
- }
-
- if (Next==True)
- {
- Next=False;
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," High Speed ");
- DisplayString(0x0,1,"Next Yes");
- Delay(300); //延時(shí)消除抖動(dòng)
-
- while(1)
- {
- if (Next==True) //如果再一次按下Next鍵,則跳出
- break;
- if (SpeedSelected==True) //如果按下確定鍵,則設(shè)置速度為High,并跳出
- {
- Thx[0]=0xe0;Tlx[0]=0xc0; //8ms
- Thx[1]=0xe0;Tlx[1]=0xc0; //8ms
- Thx[2]=0x63;Tlx[2]=0xc0; //40ms
- Thx[3]=0xec;Tlx[3]=0x78; //5ms
- SelectedSpeed=High;
- break;
- }
- }
- }
-
- if (SpeedSelected==True) //按下了確定鍵,退出速度選擇
- {
- IsSelectingSpeed=False;
- break;
- }
-
- if (Next==True) //再一次按下了Next鍵,則循環(huán)速度選擇
- {
- Next=False;
- continue;
- }
-
- }
- }
- SelectedAll=True; //標(biāo)志模式選擇和速度選擇完畢
-
- Running=True;
- Delay(50);
- WriteCmd(LCD_CLS);
-
- /*------------------------- 顯示所選擇的模式和速度方案 -------------------------*/
- if (SelectedMode==Line)
- {
- DisplayString(0x0,0,"Choosen Mode is ");
- DisplayString(0x0,1," Line ");
- Delay(50);
- WriteCmd(LCD_CLS);
- }
- if (SelectedMode==Curve)
- {
-
- DisplayString(0x0,0,"Choosen Mode is ");
- DisplayString(0x0,1," Curve ");
- Delay(50);
- WriteCmd(LCD_CLS);
- }
- if (AutoMode==1)
- {
- DisplayString(0x0,0,"Choosen Mode is ");
- DisplayString(0x0,1," AutoMode ");
- Delay(50);
- WriteCmd(LCD_CLS);
- }
- if (SelectedMode==Line)
- {
- if (SelectedSpeed==Normal)
- {
- DisplayString(0x0,0,"Choosen Speed is");
- DisplayString(0x0,1," Normal ");
- Delay(50);
- WriteCmd(LCD_CLS);
- }
-
- if (SelectedSpeed==Low)
- {
- DisplayString(0x0,0,"Choosen Speed is");
- DisplayString(0x0,1," Low ");
- Delay(50);
- WriteCmd(LCD_CLS);
- }
-
- if (SelectedSpeed==High)
- {
- DisplayString(0x0,0,"Choosen Speed is");
- DisplayString(0x0,1," High ");
- Delay(50);
- WriteCmd(LCD_CLS);
- }
- }
- INTInit(); //初始化所有中斷
- DisplayString(0x0,0,"Left Times To Go");
- while (ReadyToGo--)
- {
- DisplaySingleChar(0x7,1,ReadyToGo+0x30);
- DisplaySingleChar(0x09,1,'s');
- Delay(300);
- }
- WriteCmd(LCD_CLS);
- DisplayString(0x05,0,"Go!!!");
- Delay(100);
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Living... ");
- DisplayString(0x0,1,"Designed by 202");
-
- if (SelectedMode==Line&&AutoMode==0)
- flag=Area0;
- else
- flag=1;
-
- while(flag<5)
- {
- if(AutoMode==1) //自動(dòng)模式
- {
- switch(PassLineID)
- {
- case 0 :{
- if(IsT0INT==1)
- {P01=P02=P04=0;P03=1;}
- else
- {P01=P02=P03=P04=0;}
- }break;
- case 1 :{
- P01=P02=P03=0;P04=1;
- }break;
- case 2 :{
- P01=P02=P04=0;P03=1;
- }break;
- default :break;
- }
- }
- else
- {
- if(SelectedMode==Line) //直線模式
- {
- flag=Area0;
- if(IsT0INT==1)
- {P03=1;P04=0;P01=P02=0;}
- else
- {P03=0;P04=0;P01=P02=0;}
- }
- else
- { //S型模式
- if((Nocurve<2)&&Round!=0&&(Back0>0)&&Back!=0)
- {
- if(Backid==1)
- {P01=1;P02=0;P03=0;P04=1;}
- else
- {P01=0;P02=1;P03=0;P04=1;}
- Back=1;
- }
- else
- {
- if(Round==0)
- {
- if(IsT0INT2==1)
- {P01=0;P02=0;P03=1;P04=0;}
- else
- {P01=0;P02=0;P03=0;P04=0;}
- }
- else
- {
- if(P33==0)
- {
- if(IsT0INT2==1)
- {P01=0;P02=0;P03=1;P04=0;}
- else
- {P01=0;P02=0;P03=0;P04=0;}
- }
- else
- {
- EX1=1;
- if(Round%2)
- {
- if(IsT0INT2==1)
- {P01=1;P02=0;P03=1;P04=0;Backid=1;}
- else
- {P01=1;P02=0;P03=0;P04=0;}
- }
- else
- {
- if(IsT0INT2==1)
- {P01=0;P02=1;P03=1;P04=0;Backid=0;}
- else
- {P01=0;P02=1;P03=0;P04=0;}
- }
- }
- }
- }
- }
- }
-
- if (IsT1INT==1)
- {
- IsT1INT=0;
- ComputeTime();
- ComputeSpeedANDDistance();
- }
- }
-
- //補(bǔ)中斷路程,加上最后一次中斷缺失的路程
- ComputeSpeedANDDistance();
- P04=1;P03=0;P01=P02=0;Delay(90);
- P03=0;P04=0; //行程結(jié)束,小車(chē)停止
- P31=1; //行程結(jié)束,背光開(kāi)
- ET0=0x0; //關(guān)T0中斷
- ET1=0x0; //關(guān)T1中斷
- EX1=0x01; //開(kāi)INT1中斷
- Running=False;
-
-
- AutoDisplay=True; //默認(rèn)情況下直線模式會(huì)自動(dòng)顯示各個(gè)區(qū)域經(jīng)過(guò)的時(shí)間
- WriteCmd(LCD_CLS);
-
- if (SelectedMode==Line) //直線模式才顯示
- {
- while(1)
- {
- if (GoToChoosingDisplay==True)
- break;
- Delay(200);
- WriteCmd(LCD_CLS);
- Delay(200);
- DisplayString(0,0," LowSpeedArea1");
- DisplayString(0,1," Costed ");
- DisplaySingleChar(0x0C,1,'s');
- LowSpeedArea1PassTime=LowSpeedArea1EndTime-LowSpeedArea1StartTime;
- DisplaySingleChar(0x0A,1,LowSpeedArea1PassTime%10+0x30);
- if (LowSpeedArea1PassTime > 9) //通過(guò)第一個(gè)低速區(qū)的時(shí)間超過(guò)s
- DisplaySingleChar(0x0B,1,LowSpeedArea1PassTime/10+0x30);
-
- if (GoToChoosingDisplay==True)
- break;
- Delay(200);
- WriteCmd(LCD_CLS);
- Delay(200);
- DisplayString(0,0," HighSpeedArea ");
- DisplayString(0,1," Costed ");
- DisplaySingleChar(0x0C,1,'s');
- HighSpeedAreaPassTime=HighSpeedAreaEndTime-LowSpeedArea1EndTime;
- DisplaySingleChar(0x0A,1,HighSpeedAreaPassTime%10+0x30);
- if ( HighSpeedAreaPassTime> 9) //通過(guò)高速區(qū)的時(shí)間超過(guò)s
- DisplaySingleChar(0x0B,1,HighSpeedAreaPassTime/10+0x30);
-
- if (GoToChoosingDisplay==True)
- break;
- Delay(200);
- WriteCmd(LCD_CLS);
- Delay(200);
- DisplayString(0,0," LowSpeedArea2 ");
- DisplayString(0,1," Costed ");
- DisplaySingleChar(0x0C,1,'s');
- LowSpeedArea2PassTime=LowSpeedArea2EndTime-HighSpeedAreaEndTime;
- DisplaySingleChar(0x0A,1,LowSpeedArea2PassTime%10+0x30);
- if ( LowSpeedArea2PassTime> 9) //通過(guò)第二個(gè)低速區(qū)的時(shí)間超過(guò)s
- DisplaySingleChar(0x0B,1,LowSpeedArea2PassTime/10+0x30);
- }
- }
-
- AutoDisplay=False;
- /*---------------- 菜單選擇你想要看的內(nèi)容--總時(shí)間、總路程以及平均速度 --------------*/
- ChoosingDisplay=True;
- WriteCmd(LCD_CLS);
- /*首先顯示主菜單,然后顯示第一個(gè)選項(xiàng)*/
- DisplayString(0x0,0,"Now Choose what ");
- DisplayString(0x0,1,"you want to see ");
- Delay(100);
- while(1)
- {
-
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Costed Time ");
- DisplayString(0x0,1,"Next Show");
- Delay(250); //延時(shí)消除抖動(dòng)
- /*------------------------------------第一次按鍵--------------------------------------*/
-
- /*不斷檢測(cè)確定鍵和Next鍵*/
- while(1)
- {
- if (Next==True)
- break;
- if (SelectedShow==True)
- break;
- }
- /*按下了確定鍵,顯示第一個(gè)選項(xiàng)的內(nèi)容*/
- if (SelectedShow==True)
- {
- SelectedShow=False;
- SelectedReturn=False;
- Selected=False;
- WriteCmd(LCD_CLS);
- DisplayString(0,0,"Costed Time is");
- DisplayTime();
- DisplayString(0x0A,1,"s");
- ReturnSelection=True; //按下了確定鍵,那么這個(gè)時(shí)候開(kāi)啟返回鍵的功能
- AVGSpeedShow=False;
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- /**按下了Next鍵,則顯示第二個(gè)選項(xiàng)*/
- if (Next==True) //按下Next鍵,顯示AVGSpeed菜單項(xiàng)
- {
- Next=False;
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," AVGSpeed ");
- DisplayString(0x0,1,"Next Show");
- ReturnMain=False;
- ReturnSelection=False; //按下了Next鍵,那么這個(gè)時(shí)候關(guān)閉返回鍵的功能
- AVGSpeedShow=True; //表明AVGSpeed選項(xiàng)已經(jīng)顯示過(guò)了
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- /*------------------------------------第二次按鍵--------------------------------------*/
- /*顯示第一個(gè)選項(xiàng)的內(nèi)容后又不斷檢測(cè)返回鍵(確定鍵)和Next鍵*/
- while(1)
- {
- if (Next==True)
- break;
- if (Selected==True)
- break;
- }
-
- if (Next==True)
- {
- Next=False;
- ReturnMain=False;
- ReturnSelection=False; //按下了Next鍵,那么這個(gè)時(shí)候關(guān)閉返回鍵的功能
- if (AVGSpeedShow==False) //還沒(méi)有顯示AVGSpeed選項(xiàng),顯示它
- { //即第一次選擇了確定鍵
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," AVGSpeed ");
- DisplayString(0x0,1,"Next Show");
- TotalDistanceShow=False; //顯示了AVGSpeed,則表明TotalDistance還沒(méi)有顯示
-
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- if (AVGSpeedShow==True) //已經(jīng)顯示過(guò)AVGSpeed選項(xiàng)了,則顯示下一個(gè)選項(xiàng)
- { //即第一次選擇了Next鍵
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Total Distance ");
- DisplayString(0x0,1,"Next Show");
- TotalDistanceShow=True; //表明顯示了TotalDistance選項(xiàng)
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- }
- if (Selected==True) //按下了確定鍵或返回鍵
- {
- SelectedShow=False;
- SelectedReturn=False;
- Selected=False;
- if (ReturnSelection==True) //第一次選擇了確定鍵,故這次按下的是返回鍵
- ReturnMain=True;
- if (ReturnSelection==False)
- {
- WriteCmd(LCD_CLS);
- DisplayString(0,0,"The AVGSpeed is");
- DisplayAVGSpeed();
- DisplayString(0x0A,1,"m/s");
- ReturnSelection=True; //按下了確定鍵,那么這個(gè)時(shí)候開(kāi)啟返回鍵的功能
-
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- TotalDistanceShow=False;
- }
- if (ReturnMain==True) //按下了返回鍵,返回主菜單
- {
- ReturnMain=False;
- continue;
- }
- /*------------------------------------第三次按鍵--------------------------------------*/
- /*如果沒(méi)有返回主菜單,則繼續(xù)檢測(cè)Next鍵和確定鍵*/
-
- while(1)
- {
- if (Next==True)
- break;
- if (SelectedShow==True)
- break;
- }
-
- /*按下Next鍵,顯示下一個(gè)選項(xiàng)*/
- if (Next==True)
- {
- Next=False;
- ReturnMain=False;
- ReturnSelection=False; //按下了Next鍵,那么這個(gè)時(shí)候關(guān)閉返回鍵的功能
- if (TotalDistanceShow==True)
- ReturnMain=True;
- if (TotalDistanceShow==False) //還沒(méi)有顯示TotalDistance選項(xiàng),顯示它
- {
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Total Distance ");
- DisplayString(0x0,1,"Next Show");
- TotalDistanceShow=True;
-
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- }
- if (Selected==True) //按下了確定鍵或返回鍵
- {
- SelectedShow=False;
- SelectedReturn=False;
- Selected=False;
- if (ReturnSelection==True) //按下的是返回鍵
- ReturnMain=True;
- if (ReturnSelection==False)
- {
- if (TotalDistanceShow==False) //表明AVGSpeed選項(xiàng)的內(nèi)容還沒(méi)有顯示
- {
- WriteCmd(LCD_CLS);
- DisplayString(0,0,"The AVGSpeed is");
- DisplayAVGSpeed();
- DisplayString(0x0A,1,"m/s");
- ReturnSelection=True;
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- if (TotalDistanceShow==True)
- {
- WriteCmd(LCD_CLS);
- DisplayString(0,0,"Total Distance");
- DisplayDistance();
- DisplayString(0x0A,1,"m");
- ReturnSelection=True; //按下了確定鍵,那么這個(gè)時(shí)候開(kāi)啟返回鍵的功能
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- }
- }
- if (ReturnMain==True) //按下了返回鍵,返回主菜單
- {
- ReturnMain=False;
- continue;
- }
- /*------------------------------------第四次按鍵--------------------------------------*/
- while(1)
- {
- if (Next==True)
- break;
- if (SelectedShow==True)
- break;
- }
-
- if (Next==True) //所有菜單項(xiàng)已經(jīng)顯示完畢,返回主菜單
- {
- Next=False;
- ReturnMain=False;
- ReturnSelection=False;
- if (TotalDistanceShow==False)
- {
- WriteCmd(LCD_CLS);
- DisplayString(0x0,0," Total Distance ");
- DisplayString(0x0,1,"Next Show");
- TotalDistanceShow=True;
-
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- }
- if (SelectedShow==True)
- {
- SelectedShow=False;
- SelectedReturn=False;
- Selected=False;
- if (ReturnSelection==True) //按下的是返回鍵
- ReturnMain=True;
- if (ReturnSelection==False)
- {
- if (TotalDistanceShow==True)
- {
- WriteCmd(LCD_CLS);
- DisplayString(0,0,"Total Distance");
- DisplayDistance();
- DisplayString(0x0A,1,"m");
- ReturnSelection=True; //按下了確定鍵,那么這個(gè)時(shí)候開(kāi)啟返回鍵的功能
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- }
- }
-
- if (ReturnMain==True) //按下了返回鍵,返回主菜單
- {
- ReturnMain=False;
- continue;
- }
- /*------------------------------------第五次按鍵--------------------------------------*/
- while(1)
- {
- if (Next==True)
- break;
- if (SelectedShow==True)
- break;
- }
- if (Next==True) //所有菜單項(xiàng)已經(jīng)顯示完畢,返回主菜單
- {
- Next=False;
- ReturnMain=False;
- ReturnSelection=False;
- if (TotalDistanceShow==True) //最后一個(gè)選項(xiàng)已經(jīng)顯示完畢,返回主菜單
- {
- ReturnMain=True;
- }
- }
-
- if (SelectedShow==True)
- {
- SelectedShow=False;
- SelectedReturn=False;
- Selected=False;
- if (ReturnSelection==True) //按下的是返回鍵
- ReturnMain=True;
- if (ReturnSelection==False)
- {
- if (TotalDistanceShow==True)
- {
- WriteCmd(LCD_CLS);
- DisplayString(0,0,"Total Distance");
- DisplayDistance();
- DisplayString(0x0A,1,"m");
- ReturnSelection=True; //按下了確定鍵,那么這個(gè)時(shí)候開(kāi)啟返回鍵的功能
- Delay(250); //延時(shí)消除抖動(dòng)
- }
- }
- }
- /*------------------------------------第六次按鍵--------------------------------------*/
- while(1)
- {
- if (Next==True)
- break;
- if (SelectedShow==True)
- break;
- }
- if (Next==True)
- {
- Next=False;
- ReturnMain=False;
- ReturnSelection=False;
- }
- if (SelectedShow==True)
- {
- SelectedShow=False;
- SelectedReturn=False;
- Selected=False;
- }
- continue;
- }
- while(1);
- }
- /****************************************************************************************\
- ** LCD驅(qū)動(dòng)模塊 **
- \****************************************************************************************/
- /*--------------------------------- LCD初始化函數(shù)--------------------------------------*/
- void LCDInit(void)
- {
- //三次顯示模式設(shè)置
- LCD_Data=0;
- LCD_E=Disable;
- Delay(5);
- WriteCmd(LCD_Init);
- Delay(5);
- WriteCmd(LCD_Init);
- Delay(5);
- WriteCmd(LCD_Init);
- WriteCmd(LCD_Init); //初始化
- WriteCmd(LCD_CloseCtr); //關(guān)顯示
- WriteCmd(LCD_CLS); //清屏幕
- WriteCmd(LCD_EnterSet); //光標(biāo)移動(dòng)設(shè)置
- WriteCmd(LCD_DispCtr); //顯示開(kāi)以及光標(biāo)設(shè)置
-
- }
- /*--------------------------------- LCD模式設(shè)置函數(shù)--------------------------------------*\
-
- SetWriteCmd() 設(shè)置LCD為寫(xiě)命令模式
- SetReadCmd() 設(shè)置LCD為讀命令模式
- SetWriteData() 設(shè)置LCD為寫(xiě)數(shù)據(jù)模式
-
- \*----------------------------------------------------------------------------------------*/
- void SetWriteCmd(void)
- {
- LCD_RW=Write;
- LCD_RS=Cmd;
- }
- void SetReadCmd(void)
- {
- LCD_RW=Read;
- LCD_RS=Cmd;
- }
- void SetWriteData(void)
- {
- LCD_RW=Write;
- LCD_RS=Data;
- }
- /*--------------------------------- LCD功能執(zhí)行函數(shù)--------------------------------------*\
-
- WriteCmd() 寫(xiě)命令
- WriteData() 寫(xiě)數(shù)據(jù)
- ExecuteCmd() 執(zhí)行命令
- SetXY() 顯示定位
- DisplaySingleChar() 顯示單個(gè)字符
- DisplayString() 顯示一串字符
- IsBusy() 忙標(biāo)志檢測(cè)
- \*----------------------------------------------------------------------------------------*/
- void WriteCmd(char cmd)
- {
- while(IsBusy());
- LCD_Data=cmd;
- SetWriteCmd();
- ExecuteCmd();
- }
- void WriteData(char ddata)
- {
- while(IsBusy());
- LCD_Data=ddata;
- SetWriteData();
- ExecuteCmd();
- }
- void ExecuteCmd(void)
- {
- LCD_E=Enable;
- LCD_E=Disable;
- }
- void SetXY(char x,char y)
- {
- if (y)
- x|=0x40;
- x|=0x80;
- Delay(5);
- WriteCmd(x);
- }
- void DisplaySingleChar(char x,char y,char cchar)
- {
- SetXY(x,y);
- WriteData(cchar);
- }
- void DisplayString(char x,char y,char *str)
- {
- while(*str)
- {
- Delay(5);
- DisplaySingleChar(x++,y,*str);
- str++;
- }
- }
- bit IsBusy(void)
- {
- LCD_Data=0xFF;
- SetReadCmd();
- ExecuteCmd();
- return (bit)(P1 & 0x80);
- }
- /*------------------------------------- 延時(shí)函數(shù) -------------------------------------*/
- void Delay(unsigned int time)
- {
- unsigned int timeCounter = 0;
- for (timeCounter = time;timeCounter > 0 ;timeCounter --)
- DelayUs(255);
- }
- void DelayUs(unsigned int time)
- {
- unsigned int timeCounter = 0;
- for (timeCounter = 0;timeCounter < time;timeCounter ++)
- _nop_();
- }
- /*******************************************************************************************\
- ** LCD顯示模塊 **
- \*******************************************************************************************/
- void ComputeTime(void)
- {
- if (Area0 < 5)
- PassTime+=0.5;
- }
- void ComputeSpeedANDDistance(void)
- {
- Speed=SpeedCount/4*CircleLength; //計(jì)算瞬時(shí)速度
- Distance+=Speed; //計(jì)算距離
- SpeedCount=0;
-
- }
- /*------------------------------------- 顯示時(shí)間 ----------------------------------------*/
- void DisplayTime(void)
- {
- char PassTime1=0x30;
- char PassTime2=0x30;
- char PassTime3=0x30;
- char PassTime4=0x30;
-
- if ((int)PassTime*100<100) //時(shí)間未夠1s
- {
- PassTime1+=0;
- PassTime2+=(int)(PassTime*100)/10;
- PassTime3+=(int)(PassTime*100)%10;
- }
- else if ((int)(PassTime*100) > 100 && (int)(PassTime*100) < 1000) //夠1s而未夠10s
- {
- PassTime1+=(int)(PassTime*100)/100;
- PassTime2+=(int)(PassTime*100)/10%10;
- PassTime3+=(int)(PassTime*100)%10;
- }
- else
- {
- PassTime1+=(int)(PassTime*100)/1000;
- PassTime2+=(int)(PassTime*100)/100%10;
- PassTime3+=(int)(PassTime*100)/10%10;
- PassTime4+=(int)(PassTime*100)%10;
- }
- if ((int)(PassTime*100) < 1000)
- {
- DisplaySingleChar(0x05,1,PassTime1);
- DisplaySingleChar(0x06,1,'.');
- DisplaySingleChar(0x07,1,PassTime2);
- DisplaySingleChar(0x08,1,PassTime3);
- }
- else
- {
- DisplaySingleChar(0x04,1,PassTime1);
- DisplaySingleChar(0x05,1,PassTime2);
- DisplaySingleChar(0x06,1,'.');
- DisplaySingleChar(0x07,1,PassTime3);
- DisplaySingleChar(0x08,1,PassTime4);
- }
-
-
- }
- /*------------------------------------ 顯示平均速度----------------------------------------*/
- void DisplayAVGSpeed(void)
- {
- int Speed1=0x30; //初始化為0的ASCII碼
- int Speed2=0x30;
- int Speed3=0x30;
- if ((int)(Distance/PassTime*100)< 100)
- {
- Speed1+=0;
- Speed2+=(int)(Distance/PassTime*100)/10;
- Speed3+=(int)(Distance/PassTime*100)%10;
- }
- else
- {
- Speed1+=(int)(Distance/PassTime*100)/100;
- Speed2+=(int)(Distance/PassTime*100)/10%10;
- Speed3+=(int)(Distance/PassTime*100)%10;
- }
- DisplaySingleChar(0x05,1,Speed1);
- DisplaySingleChar(0x06,1,'.');
- DisplaySingleChar(0x07,1,Speed2);
- DisplaySingleChar(0x08,1,Speed3);
- }
- /*------------------------------------- 顯示路程 ----------------------------------------*/
- void DisplayDistance(void)
- {
- int Distance1=0x30;
- int Distance2=0x30;
- int Distance3=0x30;
- int Distance4=0x30;
- if ((int)(Distance*100) < 100)
- {
- Distance1+=0;
- Distance2+=(int)(Distance*100)/10;
- Distance3+=(int)(Distance*100)%10;
- }
- else if ((int)(Distance*100) > 100 && (int)(Distance*100) < 1000)
- {
- Distance1+=(int)(Distance*100)/100;
- Distance2+=(int)(Distance*100)/10%10;
- Distance3+=(int)(Distance*100)%10;
- }
- else
- {
- Distance1+=(int)(Distance*100)/1000;
- Distance2+=(int)(Distance*100)/100%10;
- Distance3+=(int)(Distance*100)/10%10;
- Distance4+=(int)(Distance*100)%10;
- }
-
- if ((int)(Distance*100) < 1000)
- {
- DisplaySingleChar(0x05,1,Distance1);
- DisplaySingleChar(0x06,1,'.');
- DisplaySingleChar(0x07,1,Distance2);
- DisplaySingleChar(0x08,1,Distance3);
- }
- else
- {
- DisplaySingleChar(0x04,1,Distance1);
- DisplaySingleChar(0x05,1,Distance2);
- DisplaySingleChar(0x06,1,'.');
- DisplaySingleChar(0x07,1,Distance3);
- DisplaySingleChar(0x08,1,Distance4);
- }
- }
- /*------------------------------------- 中斷初始化 -------------------------------------*/
- void INTInit(void)
- {
- EA=1; //開(kāi)總中斷
- IT0=1; //INTO邊沿觸發(fā)
- PX0=1; //INTO優(yōu)先級(jí)為高級(jí)
- EX1=1; //開(kāi)INT1中斷
- IT1=1; //INT1邊沿觸發(fā)
- PX1=1; //INT1優(yōu)先級(jí)為高級(jí)
- Clock0_Init(); //初始化時(shí)鐘中斷
- TMOD=0x11; //T0/T1定時(shí)方式1
- ET0=0x01; //開(kāi)T0中斷
- ET1=0x01; //開(kāi)T1中斷
- }
- void Clock0_Init(void)
- {
- TR0=0x01; //啟動(dòng)T0
- TH0=Thx0; //定時(shí)初值
- TL0=Tlx0;
- }
- void Clock1_Init(void)
- {
- TR1=0x01; //啟動(dòng)T1
- TH1=0x3C; //定時(shí)初值-50ms中斷一次
- TL1=0x0B0;
- }
- /*******************************************************************************************\
- ** 中斷處理程序 **
- \*******************************************************************************************/
- /*----------------------------------- 外部中斷0 ----------------------------------------*\
- 外部中斷0有兩個(gè)功能
- (1)作為菜單選擇的Next鍵
- (2)作為測(cè)速的計(jì)數(shù)器
- \*-----------------------------------------------------------------------------------------*/
- void SpeedINT(void) interrupt 0 //中斷INT0
- {
- if (SelectedAll==False) //如果模式和速度還沒(méi)有選擇完畢,則此中斷作為Next鍵
- Next=True;
- if (Running==True) //如果模式和速度已經(jīng)選擇完畢,則此中斷作為測(cè)速中斷
- SpeedCount++;
- if (ChoosingDisplay==True) //如果是在選擇要顯示的內(nèi)容
- Next=True;
- }
- /*----------------------------------- 外部中斷1 ----------------------------------------*\
- 外部中斷1有兩個(gè)功能
- (1)作為菜單選擇的確定鍵/返回鍵
- (2)控速
- \*-----------------------------------------------------------------------------------------*/
- void CtrSpeedINT(void) interrupt 2 //中斷INT1
- {
-
- if (SelectedAll==False) //如果模式和速度還沒(méi)有選擇完畢,則此中斷作為確定鍵
- {
- if (IsSelectingMode==True) //模式選擇標(biāo)志
- ModeSelected=True;
- if (IsSelectingSpeed==True) //速度選擇標(biāo)志
- SpeedSelected=True;
- }
- if (Running==True) //如果模式和速度選擇已經(jīng)完畢,則此中斷作為控速中斷
- {
- if (Area0==0) //經(jīng)過(guò)第一條鐵線(即起跑線),開(kāi)始計(jì)時(shí),開(kāi)始測(cè)速
- {
- EX0=1;
- Clock1_Init();
- P31=0; //過(guò)起跑線,背光滅
- }
- if (Area0==1)
- LowSpeedArea1StartTime=PassTime; //讀取進(jìn)入第一個(gè)低速區(qū)的時(shí)刻
- if (Area0==2)
- LowSpeedArea1EndTime=PassTime;//讀取離開(kāi)第一個(gè)低速區(qū)的時(shí)刻,也就是進(jìn)入高速區(qū)的時(shí)刻
- if (Area0==3)
- HighSpeedAreaEndTime=PassTime;//讀取離開(kāi)高速區(qū)的時(shí)刻,也就是進(jìn)入第二個(gè)低速區(qū)的時(shí)刻
- if (Area0==4)
- LowSpeedArea2EndTime=PassTime; //讀取離開(kāi)第二個(gè)低速去的時(shí)刻
-
- if(AutoMode==1) //自動(dòng)模式
- {
- PassLine++;
- switch(PassLine)
- {
- case 5 :PassLineID=1;break;
- case 10 :PassLineID=2;break;
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
chengxu.docx
(38.44 KB, 下載次數(shù): 130)
2017-5-16 20:32 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|