51单片机的循迹小车程序

2019-08-02 01:31

#include

#define uint unsigned int #define uchar unsigned char sbit port_1=P3^5; sbit port_2=P3^4; sbit port_3=P3^1; sbit port_4=P3^0; sbit en_1=P3^2; sbit en_2=P3^3;

sbit infrared1 = P0^2; sbit infrared2 = P0^3; sbit infrared3 = P0^4;

uchar infrared,num,left,right; void init() { TMOD=0x01; TH0=(65536-1000)/256; TR0=(65536-1000)%6; EA=1; ET0=1; TR0=1; en_1 = 1; en_2 = 1; }

void motor(uchar pesition) { if(pesition == 0) { port_1=0; //左电机 port_2=1; port_3=0; //右电机 port_4=1; } if(pesition == 1) { port_1=0; //左电机 port_2=1; port_3=1; //右电机 port_4=0; } if(pesition == 2) { port_1=1; //左电机 port_2=0;

port_3=0; //右电机 port_4=1; } if(pesition == 3) { port_1=1; //左电机 port_2=0; port_3=1; //右电机 port_4=0; } if(pesition == 4) { port_1=0; //左电机 port_2=0; port_3=0; //右电机 port_4=0; } }

void infraredscan() { if(!infrared1 && !infrared2 && !infrared3) { motor(4); } if(!infrared1 && !infrared2 && infrared3) { // if(num <= 60) motor(1); // else // motor(4); } if(!infrared1 && infrared2 && !infrared3) { // if(num <= 60) motor(0); // else // motor(4); } if(!infrared1 && infrared2 && infrared3) { // if(num <= 60)

motor(1); // else // motor(4); } if(infrared1 && !infrared2 && !infrared3) { // if(num <= 60) motor(2); // else // motor(4); } if(infrared1 && !infrared2 && infrared3) { // if(num <= 60) motor(4); // else // motor(4); } if(infrared1 && infrared2 && !infrared3) { // if(num <= 60) motor(2); // else // motor(4); } if(infrared1 && infrared2 && infrared3) { // if(num <= 60) motor(0); // else // motor(4); } }

void main() { init(); while(1) { infraredscan(); }

}

void T0_time() interrupt 1 { TH0=(65536-10000)/256; TR0=(65536-10000)%6; num++; if(num == 100) { num = 0; } }


51单片机的循迹小车程序.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:零损耗深度限流装置运行规程

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: