|
//9212011 感應器的值要反轉..終點的表示法要變動..
//9212031 將mouseState 改為mouseState..加入ab_direction..開始做search..
//9212051 將陣列值反映到led 上..微調for 電池..top[0]當陣列第一個
// 當最兩邊感應到時..往前走..直到空白或終點為止
// 轉彎修正18 0c 會重複到..也要當往前走
#include <AT89X51.H>
//----------全域變數-----------
unsigned char time0_temp=0;
unsigned char time1_temp=0;
unsigned char mainState=0; //功能鍵
unsigned char mouseState=0; //電腦鼠的狀態
unsigned char ab_direction=1; //絕對方向
bit correct=0; //禁止中斷影響
//bit norepeat=0; //用在左T..讓他不要重複存值
bit GOTOEND=0; //走到終點
unsigned char r=20; //轉90 度所需要的時間25->22->20->15
unsigned char step=0; //走幾格
unsigned char littleStep=1; //走一小步
unsigned char countStep=0; //4in1 記步器
unsigned char maxStack=30;
unsigned char stack[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//stack[0]不用
unsigned char top=0; //top=0 當作pop 底
unsigned char top2=0;
//-----------函數宣告----------
void time_init(void);
void delayX1ms(unsigned char);
void delayX5ms(unsigned char);
- 72 -
void delayX10ms(unsigned char);
void delay2X10ms(unsigned char);
void beep(void);
void push(unsigned char);
//char pop(void);
void adjust_direction(unsigned char t);
void modulate(void);
void algorithm(void);
char sensor(void);
void algorithm_action(unsigned char t);
void action(unsigned char t);
void correct_straight(void);
void correct_turn(void);
void straight(void);
void turnleft(void);
void turnright(void);
void goback(void);
//-----------------------------
void main(){
//-----------宣告區-------------
unsigned char temp=0;
unsigned char temp2=0;
unsigned char i=0;
bit temp_bit=0;
//-----------初始化區-----------
time_init();
//-------------------------------
while(mainState==0){
P3=0xff;
temp=P3;
temp=~temp;
- 73 -
if (temp==0x01){ //test1 感應器輸出到led
mainState=1;
}
else if (temp==0x02){ //test2 動作測試
mainState=2;
}
else if(temp==0x04){ //test3 地形偵測
mainState=3;
}
else if(temp==0x08){ //test4 地形判斷
mainState=4;
}
else if(temp==0x10){ //test5 連續地形判斷
mainState=5;
}
else if(temp==0x20){
mainState=6; //test6 邊走邊修正+連續地形判斷
}
else if(temp==0x40){
mainState=7; //test7 搜尋
}
else if(temp==0x80){
mainState=8; //test8
}
else{}
}
//------------test1-----------------
while(mainState==1){
P2=P0;
}
//------------test2-----------------
while(mainState==2){
P3=0xff;
temp=P3;
temp=~temp;
if (mouseState!=0){ //有動作正在進行中
continue;
- 74 -
}
switch(temp){
case 0x01: //直走一格
mouseState=1;
straight();
break;
case 0x02: //左轉
mouseState=2;
turnleft();
break;
case 0x04: //右轉
mouseState=3;
turnright();
break;
case 0x08: //迴轉
mouseState=4;
goback();
break;
default:;
}
}
//------------test3-----------------
while(mainState==3){
}
//------------test4-----------------
while(mainState==4){
}
//------------test5-----------------
while(mainState==5){
}
//------------test6-----------------
while(mainState==6){
}
//-------------test7----------------
while(mainState==7){
P3=0xff;
temp=P3;
- 75 -
temp=~temp;
if (temp==0x01){
while (GOTOEND==0){
if(mouseState==1){
temp2=sensor();
if (temp2!=0){
action(temp2);
}else {}
correct_straight();
}else if(mouseState==0){
step=10;
mouseState=1;
straight();
}else {}
delayX10ms(1);
P2=~((top&0x0f)|(stack[top-1]<<4));
}
i=0;
do{
P2=~0x00;
delayX10ms(10);
P2=~((i&0x0f)|(stack[i]<<4));
if (i>=top){
i=0;
}else {
i++;
}
delayX10ms(250);
P3=0xff;
temp=P3;
temp=~temp;
}while(temp!=0x02);
beep();
beep();
beep();
- 76 -
algorithm();
}
else {}
}
//-------------test8-------------------
while(mainState==8){
}
//----------------------------------------
}
void algorithm_action(unsigned char t){
// P2=~0x00;
// delayX10ms(10);
while(stack[top2]==6){
top2++;
}
P2=~stack[top2];
if(t==3||t==4||t==5){
switch(stack[top2]){
case 1:
if (ab_direction==4){//right
mouseState=3;
turnright();
correct_turn();
ab_direction++;
}
else if(ab_direction==2){//left
mouseState=2;
turnleft();
correct_turn();
ab_direction--;
}
else {
}
break;
case 2:
if (ab_direction==1){//right
- 77 -
mouseState=3;
turnright();
correct_turn();
ab_direction++;
}
else if(ab_direction==3){//left
mouseState=2;
turnleft();
correct_turn();
ab_direction--;
}
else {
}
break;
case 3:
if (ab_direction==2){//right
mouseState=3;
turnright();
correct_turn();
ab_direction++;
}
else if(ab_direction==4){//left
mouseState=2;
turnleft();
correct_turn();
ab_direction--;
}
else {
}
break;
case 4:
if (ab_direction==1){//left
mouseState=2;
turnleft();
correct_turn();
ab_direction--;
}
- 78 -
else if(ab_direction==3){//right
mouseState=3;
turnright();
correct_turn();
ab_direction++;
}
else {
}
break;
default:
;
}
top2++;
if (ab_direction==0){
ab_direction=4;
}
else if(ab_direction==5){
ab_direction=1;
}
else{
}
}
else if(t==6){
GOTOEND=1;
mouseState=0;
P1_0=1;
P1_1=1;
}
else{
}
}
void action(unsigned char t){
unsigned char temp;
switch (t){
case 1://直線
break;
case 2://牆
- 79 -
mouseState=4;
goback();
correct_turn();
adjust_direction(2);
push(ab_direction);
break;
case 3://T
mouseState=3;
turnright();
correct_turn();
adjust_direction(3);
push(ab_direction);
break;
case 4://左T
adjust_direction(4);
push(ab_direction);
while ((temp = sensor())!=1){
if(mouseState==0){
step=10;
mouseState=1;
straight();
}
}
break;
case 5://右T
mouseState=3;
turnright();
correct_turn();
adjust_direction(5);
push(ab_direction);
break;
case 6://終點
break;
case 7://最兩邊感應到
correct=1;
P1_0=0;
P1_1=0;
- 80 -
delayX10ms(2);
P1_0=1;
P1_1=1;
correct=0;
if ((temp = sensor())==4){
goto action_7;
}
temp = sensor();
while(!(temp==3||temp==6)){//直到T 或終點..才會繼續感應下一個值
if(mouseState==0){
step=10;
mouseState=1;
straight();
}
temp = sensor();
}
if (temp==6){
GOTOEND=1;
mouseState=0;
P1_0=1;
P1_1=1;
adjust_direction(6);
push(ab_direction);
}
action_7:
break;
default:
beep();
}
}
void adjust_direction(unsigned char t){
switch (t){
case 1:
break;
case 2://牆
if (ab_direction==1){
- 81 -
ab_direction=3;
}
else if (ab_direction==2){
ab_direction=4;
}
else if (ab_direction==3){
ab_direction=1;
}
else if (ab_direction==4){
ab_direction=2;
}
else {
beep();
}
break;
case 3://T
ab_direction++;
if (ab_direction==0){
ab_direction=4;
}
else if(ab_direction==5){
ab_direction=1;
}
else {}
break;
case 4://左T
break;
case 5://右T
ab_direction++;
if (ab_direction==0){
ab_direction=4;
}
else if(ab_direction==5){
ab_direction=1;
}
else {}
break; |
|