单排机循迹小车的最终代码

<pre name="code" class="cpp">#include <reg52.h>

sbit IN1 = P1^4; //ÓÒÂÖÇý¶¯
sbit IN2 = P1^3;

sbit IN3 = P1^2; //×óÂÖÇý¶¯
sbit IN4 = P1^1;

void run()
{
	IN1 = 0;
	IN2 = 1;

	IN3 = 0;
	IN4 = 1;
	
}

void turn_left_min() //С×óתÍä(×óÂÖÇ°½ø£¬ÓÒÂÖÇ°½ø)
{
	IN1 = 0;
	IN2 = 1;

	IN3 = 0;
	IN4 = 1;
	
}

void turn_left_max() //´ó×óתÍä(×óÂÖºóÍË£¬ÓÒÂÖÇ°½ø£©
{
	IN1 = 0;
	IN2 = 1;

	IN3 = 1;
	IN4 = 0;
}

void turn_right_min() //СÓÒתÍ䣨×óÂÖÇ°½ø£¬ÓÒÂÖÇ°½ø£©
{
	IN1 = 0;
	IN2 = 1;
	
	IN3 = 0;
	IN4 = 1;
}

void turn_right_max() //´óÓÒתÍ䣨×óÂÖÇ°½ø£¬ÓÒÂÖºóÍË£©
{
	IN1 = 1;
	IN2 = 0;
	
	IN3 = 0;
	IN4 = 1;
}

void stop()
{
	IN1 = 0;
	IN2 = 0;
	
	IN3 = 0;
	IN4 = 0;
}




#include <reg52.h>#include "./turn/turn.h"#define uint unsigned int #define uchar unsigned charsbit ENA = P1^5;//右驱动sbit ENB = P1^0; //左驱动sbit xj_l2 = P3^0;//左1循迹口sbit xj_l1 = P3^1;//左2循迹口sbit xj_r1 = P3^2;//右1循迹口sbit xj_r2 = P3^3;//右2循迹口uchar ZKB_L = 0; //左轮占空比uchar ZKB_R = 0; //右轮占空比uchar pwm;uchar num = 0;//对中断次数计数void timer0_init(){TMOD |= 0x01;EA = 1;TH0 = (65536 - 500) / 256;TL0 = (65536 - 500) % 256;ET0 = 1;TR0 = 1;}void timer0_isr() interrupt 1{TR0 = 0;TH0 = (65536 - 500) / 256;TL0 = (65536 - 500) % 256;num++;if(num > 32){num = 0;}if(num <= ZKB_L) {ENA = 1;}else{ENA = 0;}if(num <= ZKB_R){ENB = 1;}else{ENB = 0;}TR0 = 1;}void xunji() //循迹函数{if((xj_l2 == 0)&(xj_l1 == 0)&(xj_r1 == 0)&(xj_r2 == 0)){run();ZKB_L = 10;ZKB_R = 10;}if((xj_l2 == 0)&(xj_l1 == 1)&(xj_r1 == 0)&(xj_r2 == 0)){turn_right_min(); ZKB_L = 10;ZKB_R = 7;}if((xj_l2 == 0)&(xj_l1 == 0)&(xj_r1 == 1)&(xj_r2 == 0)){turn_left_min();ZKB_L = 7;ZKB_R = 10;}if((xj_l2 == 1)&(xj_l1 == 0)&(xj_r1 == 0)&(xj_r2 == 0)){turn_left_max();ZKB_L = 5;ZKB_R = 10;}if((xj_l2 == 0)&(xj_l1 == 0)&(xj_r1 == 0)&(xj_r2 == 1)){turn_right_max();ZKB_L = 10;ZKB_R = 5;}if((xj_l2 == 1)&(xj_l1 == 1)&(xj_r1 == 1)&(xj_r2 == 1)){stop();} }void main(){timer0_init();while(1){xunji();}}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值