寻迹小车——代码

<p>
嘿嘿嘿!今天下午终于把小车代码写好了,黑胶带还没来,稍微调一下就应该能跑了,是不是很厉害!下面是我下午写的代码</p><div>
</div>
#include<reg52.h>

sbit IN1 = P1^0;
sbit IN2 = P1^1;
sbit ENA = P1^2;
sbit IN3 = P1^3;
sbit IN4 = P1^4;
sbit ENB = P1^5;

sbit left1 = P0^0;
sbit left2 = P0^1;
sbit right1 = P0^2;
sbit right2 = P0^3;

unsigned char zkb1 = 0;
unsigned char zkb2 = 0;
unsigned char t = 0;

void init()
{
	TMOD |= 0x01;
	TH0 =(65536 - 100) / 256;
	TL0 = (65536 - 100)% 256;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
}

void timer0() interrupt 1
{
	if(t < zkb1)
	{
		ENA = 1;
	}
	else
	{
		ENA = 0;
	}
	if(t < zkb2)
	{
		ENB = 1;
	}
	else 
	{
		ENB = 0;
	}
	if(t >= 50)
	{
		t = 0;
	}
}

void turn_left1()
{
	zkb1 = 20;
	zkb2 = 50;
}

void turn_left2()
{
	zkb1 = 0;
	zkb2 = 50;
}

void turn_right1()
{
	zkb1 = 50;
	zkb2 = 20;
}

void turn_right2()
{
	zkb1 = 50;
	zkb2 = 0;
}

void qianjin()
{
	zkb1 = 50;
	zkb2 = 50;
}
void xunji()
{
	unsigned char flag;
	if((right2 == 0)&&(right1 == 0)&&(left1 == 0)&&(left2 == 0))
	{
		flag = 0;
	}
	if((right2 == 0)&&(right1 == 1)&&(left1 == 0)&&(left2 == 0))
	{
		flag = 1;
	}
	if((right2 == 0)&&(right1 == 0)&&(left1 == 1)&&(left2 == 0))
	{
		flag = 2;
	}
	if((right2 == 0)&&(right1 == 0)&&(left1 == 0)&&(left2 == 1))
	{
		flag = 3;
	}
	if((right2 == 1)&&(right1 == 0)&&(left1 == 0)&&(left2 == 0))
	{
		flag = 4;
	}
	switch(flag)
	{
		case 0:qianjin();
				break;
		case 1:turn_right1();break;
		
		case 2:turn_left1();break;
		
		case 3:turn_left2();break;
		
		case 4:turn_right2();break;
		
		default:break;
	}
}

void main()
{
	init();
	zkb1 = 50;
	zkb2 = 50;
	while(1)
	{
		IN1 = 0;
		IN2 = 1;
		IN3 = 0;
		IN4 = 1;
		ENA = 1;
		ENB = 1;
		while(1)
		{
			xunji();
		}
	}
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值