Nghiên cứu Khoa học
Chương trình cho robot dò đường
#include "io430.h"
#include "intrinsics.h"
#define COT0 BIT0
#define COT1 BIT1
#define COT2 BIT2
#define COT3 BIT3
#define COT4 BIT4
#define COT5 BIT5
#define COT6 BIT6
#define COT7 BIT7
void quetphim (int *nhappass,int *p)
{
int i=0,n=0;
while(i < 6)
{
__delay_cycles(5000);
{
P2OUT |= COT3;
nhappass[i] = 1;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT4) == 0)
n=0;
else
n=1;
}
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT5) == 0)
n=0;
else
n=1;
}
}
{
P2OUT |= COT3;
nhappass[i] = 3;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT6) == 0)
n=0;
else
n=1;
}
}
P2OUT &= ~COT3;
__delay_cycles(5000);
{
P2OUT |= COT3;
nhappass[i] = 4;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT4) == 0)
n=0;
else
n=1;
}
}
{
P2OUT |= COT3;
nhappass[i] = 5;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT5) == 0)
n=0;
else
n=1;
}
}
{
P2OUT |= COT3;
nhappass[i] = 6;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT6) == 0)
n=0;
else
n=1;
}
}
{
if (*p==1)
{
if (i==0)
{
P2OUT |= COT3;
n=0;
while ( n==0 )
{
if ((P1IN&COT7) == 0)
n=0;
else
{
P2OUT &= ~COT3;
n=0;
while ( n<4 )
{
P2OUT |= COT1;
__delay_cycles(35000);
P2OUT &= ~COT1;
__delay_cycles(35000);
__delay_cycles(35000);
n++;
}
n=1;
}
}
}
}
}
{
P2OUT |= COT3;
nhappass[i] = 7;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT4) == 0)
n=0;
else
n=1;
}
}
{
P2OUT |= COT3;
nhappass[i] = 8;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT5) == 0)
n=0;
else
n=1;
}
}
{
P2OUT |= COT3;
nhappass[i] = 9;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT6) == 0)
n=0;
else
n=1;
}
}
P2OUT &= ~COT3;
__delay_cycles(5000);
if (*p==0)
{
{
P2OUT |= COT3;
n=0;
while ( n==0 )
{
if ((P1IN&COT4) == 0)
n=0;
else
{
n=1;
i=7;
*p = *p+1;
P2OUT &= ~COT3;
}
}
}
}
{
P2OUT |= COT3;
nhappass[i] = 0;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT5) == 0)
n=0;
else
{
n=1;
P2OUT &= ~COT3;
}
}
}
i--;
P2OUT |= COT3;
}
n=0;
while ( n==0 )
{
if ((P1IN&COT6) == 0)
n=0;
else
n=1;
}
}
{
P2OUT |= COT3;
i++;
n=0;
while ( n==0 )
{
if ((P1IN&COT7) == 0)
n=0;
else
{
n=1;
P2OUT &= ~COT3;
}
}
}
i--;
P2OUT |= COT3;
}
n=0;
while ( n==0 )
{
if ((P1IN&COT6) == 0)
n=0;
else
n=1;
}
}
else if (*p==0)
{
{
P2OUT |= COT3;
n=0;
while ( n==0 )
{
if ((P1IN&COT4) == 0)
n=0;
else
{
n=1;
i=7;
*p = *p+1;
P2OUT &= ~COT3;
}
}
}
}
{
if (*i==0)
{
if(nhappass[*i] == a)
{
*i = *i+1;
if(nhappass[*i] == b)
{
*i = *i+1;
if(nhappass[*i] == c)
{
*i = *i+1;
if(nhappass[*i] == d)
{
*i = *i+1;
if(nhappass[*i] == e)
{
*i = *i+1;
if(nhappass[*i] == h)
*i = *i+1;
}
}
}
}
}
}
else
*i = 7;
}
void treohethong (void)
{
int j;
P2OUT |= COT3;
j=0;
while (j<=60)
{
__delay_cycles(1000000);
j++;
}
P2OUT &= ~COT3;
}
{
int j=0;
while (j<=10)
{
P2OUT ^= COT3;
__delay_cycles(50000);
j++;
}
P2OUT &= ~COT3;
}
void loakeu(void)
{
P2OUT |= COT0;
__delay_cycles(500000);
P2OUT &= ~COT0;
}
int j=0,i,k,n,m,p,g;
int nhappass[6],passdoi[6];
int a=3,b=5,c=7,d=1,e=5,h=9;
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P2DIR = 0xFF;
P2OUT = 0x00;
P1DIR = 0x0F; //port 1.0,1.1,1.2,1.3 ngo ra,port 1.4,1.5,1.6,1.7 ngo vao.
while(1)
{
P2OUT = 0x00;
p=1;
quetphim(nhappass,&p);
i=0;
sosanh(nhappass,&i,a,b,c,d,e,h);
k=0;
while (k==0)
{
{
denchop ();
{
j++;
{
loakeu();
treohethong ();
m=1;
}
{
loakeu();
{
P2OUT |= COT3;
n=0;
while ( n==0 )
{
if ((P1IN&COT7) == 0)
n=0;
else
{
P2OUT &= ~COT3;
n=0;
while ( n<4 )
{
P2OUT |= COT2;
__delay_cycles(35000);
P2OUT &= ~COT2;
__delay_cycles(35000);
__delay_cycles(35000);
n++;
}
n=1;
}
}
}
P1OUT = 0x0D; /*Quet phim close.*/
__delay_cycles(5000);
if ((P1IN&COT7) == 0) /*Nhan close.*/
{
m=1;
P2OUT |= COT3;
n=0;
while ( n==0 )
{
if ((P1IN&COT7) == 0)
n=0;
else
{
P2OUT &= ~COT3;
n=0;
while ( n<4 )
{
P2OUT |= COT1;
__delay_cycles(35000);
P2OUT &= ~COT1;
__delay_cycles(35000);
__delay_cycles(35000);
n++;
}
n=1;
}
}
}
P1OUT = 0x0B; /*Quet phim change.*/
__delay_cycles(5000);
if ((P1IN&COT7) == 0) /*Nhan chance de thay doi pass.*/
{
P2OUT |= COT3;
__delay_cycles(500000);
n=0;
while ( n==0 )
{
if ((P1IN&COT7) == 0)
n=0;
else
{
P2OUT &= ~COT3;
n=1;
}
}
else
m=1;
if (p==0)
{
k=0;
while (k<6)
{
k++;
else
k=7;
}
{
denchop ();
a = nhappass[0];
b = nhappass[1];
c = nhappass[2];
d = nhappass[3];
e = nhappass[4];
h = nhappass[5];
}
else
{
g++; //Tang g len 1 khi nhap pass sai.
if (g == 3)
{
loakeu();
loakeu();
}
}
}
else
m=1;
}
}
}