#include<p18f2682.h>
#include"data_type.h"
#include"head.h"
u16 motor_zkb=0;
u8 sensor1=0,sensor2=0;
u8 round_flag =0;
void go_front(u16 a)
{
LATCbits.LATC6 = 1;
LATCbits.LATC7 = 0;
motor_zkb = a;
}
void go_back(u16 b)
{
LATCbits.LATC6 = 0;
LATCbits.LATC7 = 1;
motor_zkb = b;
}
void stop(void)
{
LATCbits.LATC6 = 0;
LATCbits.LATC7 = 0;
motor_pwm = 0;
}
void duoji_turn(u8 zkb)
{
dj_pwm = zkb;
}
void delay_ms(u8 delaytime)
{
u8 a,b,c;
for(a=delaytime;a>0;a--)
for(b=10;b>0;b--)
for(c=100;c>0;c--);
}
void xiaodou(void)
{
delay_ms(10);
sensor1 = (PORTA&0x1F);
sensor2 = (PORTB&0xFF);
}
void xiaodou8(void)
{
delay_ms(3);
sensor2 = (PORTB&0xFF);
}
/*
RB0 RB7
0 0 0 0 0 0 0 0 //保持上次 PORTB &0XFF = 0X00
0 0 0 1 1 0 0 0 //直行 PORTB &0XFF = 0X18
0 0 0 1 0 0 0 0 //左转 PORTB &0XFF = 0X08
0 0 1 1 0 0 0 0 //左转 PORTB &0XFF = 0X0C
0 0 1 0 0 0 0 0 //左转 PORTB &0XFF = 0X04
0 1 1 0 0 0 0 0 PORTB &0XFF = 0X06
0 1 0 0 0 0 0 0 PORTB &0XFF = 0X02
1 1 0 0 0 0 0 0 PORTB &0XFF = 0X03
1 0 0 0 0 0 0 0 PORTB &0XFF = 0X01
0 0 0 0 1 0 0 0 //右转 PORTB &0XFF = 0X10
0 0 0 0 1 1 0 0 PORTB &0XFF = 0X30
0 0 0 0 0 1 0 0 PORTB &0XFF = 0X20
0 0 0 0 0 1 1 0 PORTB &0XFF = 0X60
0 0 0 0 0 0 1 0 PORTB &0XFF = 0X40
0 0 0 0 0 0 1 1 PORTB &0XFF = 0XC0
0 0 0 0 0 0 0 1 PORTB &0XFF = 0X80
1 1 1 1 1 1 1 1 //停止 PORTB &0XFF = 0XFF
*/
void find_line8(void)
{
sensor2 = (PORTB&0xFF);
switch(sensor2)
{
case 0x18:
xiaodou8();
if(sensor2==0x18)
{
go_front(250);
duoji_turn(32);
}
break;
//////////////////////////////turn right
case 0x10:
xiaodou8();
if(sensor2==0x10)
{
go_front(200);
duoji_turn(33);
}
break;
case 0x30:
xiaodou8();
if(sensor2==0x30)
{
go_front(150);
duoji_turn(34);
}
break;
case 0x20:
xiaodou8();
if(sensor2==0x20)
{
go_front(130);
duoji_turn(35);
}
break;
case 0x60:
xiaodou8();
if(sensor2==0x60)
{
go_front(120);
duoji_turn(36);
}
break;
case 0x40:
xiaodou8();
if(sensor2==0x40)
{
go_front(110);
duoji_turn(37);
}
break;
case 0xC0:
xiaodou8();
if(sensor2==0xc0)
{
go_front(100);
duoji_turn(38);
}
break;
case 0x80:
xiaodou8();
if(sensor2==0x80)
{
go_front(100);
duoji_turn(40);
}
break;
////////////////////////////// turn left
case 0x08:
xiaodou8();
if(sensor2==0x08)
{
go_front(200);
duoji_turn(31);
}
break;
case 0x0C:
xiaodou8();
if(sensor2==0x0c)
{
go_front(150);
duoji_turn(30);
}
break;
case 0x04:
xiaodou8();
if(sensor2==0x04)
{
go_front(130);
duoji_turn(29);
}
break;
case 0x06:
xiaodou8();
if(sensor2==0x06)
{
go_front(120);
duoji_turn(28);
}
break;
case 0x02:
xiaodou8();
if(sensor2==0x02)
{
go_front(110);
duoji_turn(27);
}
break;
case 0x03:
xiaodou8();
if(sensor2==0x03)
{
go_front(100);
duoji_turn(26);
}
break;
case 0x01:
xiaodou8();
if(sensor2==0x01)
{
go_front(100);
duoji_turn(24);
}
break;
case 0xff:
xiaodou8();
if(sensor2==0xff)
{
stop();
duoji_turn(32);
}
break;
default:
break;
}
}
/*-------------------------
巡线主程序 sensor13
RA0 RA1 RA2 RA3 RA4 RB0 |RB1| RB2 RB3 RB4 RB5 RB6 RB7
sensor1 sensor2
0 0 0 0 0 0 0 0 0 0 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x00 保持上一次状态
0 0 0 0 0 0 1 0 0 0 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x02 直行
0 0 0 0 0 1 1 0 0 0 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x03 左转
0 0 0 0 0 1 0 0 0 0 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x01
0 0 0 0 1 1 0 0 0 0 0 0 0 PORTA&0x1F = 0x10 .PORTB&0xFF = 0x01
0 0 0 0 1 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x10 .PORTB&0xFF = 0x00
0 0 0 1 1 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x18 .PORTB&0xFF = 0x00
0 0 0 1 0 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x08 .PORTB&0xFF = 0x00
0 0 1 1 0 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x0c .PORTB&0xFF = 0x00
0 0 1 0 0 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x04 .PORTB&0xFF = 0x00
0 1 1 0 0 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x06 .PORTB&0xFF = 0x00
0 1 0 0 0 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x02 .PORTB&0xFF = 0x00
1 1 0 0 0 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x03 .PORTB&0xFF = 0x00
1 0 0 0 0 0 0 0 0 0 0 0 0 PORTA&0x0F = 0x01 .PORTB&0xFF = 0x00
0 0 0 0 0 0 1 1 0 0 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x06 右转
0 0 0 0 0 0 0 1 0 0 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x04
0 0 0 0 0 0 0 1 1 0 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x0c
0 0 0 0 0 0 0 0 1 0 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x08
0 0 0 0 0 0 0 0 1 1 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x18
0 0 0 0 0 0 0 0 0 1 0 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x10
0 0 0 0 0 0 0 0 0 1 1 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x30
0 0 0 0 0 0 0 0 0 0 1 0 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x20
0 0 0 0 0 0 0 0 0 0 1 1 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x60
0 0 0 0 0 0 0 0 0 0 0 1 0 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x40
0 0 0 0 0 0 0 0 0 0 0 1 1 PORTA&0x1F = 0x00 .PORTB&0xFF = 0xc0
0 0 0 0 0 0 0 0 0 0 0 0 1 PORTA&0x1F = 0x00 .PORTB&0xFF = 0x80
0 1/0 1/0 1 1/0 1/0 1 1/0 0/1 1 0/1 1/0 0 PORTA&0x1F = 0x03 .PORTB&0xFF = 0xc5 START
---------------------------*/
void find_line(void)
{
/*// if(((PORTA&0X09) ==0x08)&&((PORTB&0x92) ==0x12))
// {
// START_RUN =0x55; //启动标志位
// if(round_flag==0)
// {
// ROUND_NUM++; //圈数
// round_flag=1;
// go_front(250);
// if(ROUND_NUM>=3) //x-1圈后停止
// {
// START_RUN =0x00;
// ROUND_NUM =0;
// round_flag =