#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char;
#define uint unsigned int;
sbit INL=P3^7;
sbit ENL1=P3^6;
sbit ENL2=P3^5;
sbit INR=P3^2;
sbit ENR1=P3^4;
sbit ENR2=P3^3;
sbit L1=P1^3;
sbit L2=P1^1;
sbit R2=P1^4;
sbit R1=P1^2;
sbit beep=P1^5;
uchar zkbL=0,zkbR=0;//左右轮占空比
uchar t=0,s=0,time=0,fx=0;//t为动机时间,s为距离,time为测距距离,fx为传感器的方向
void init(){//初始化函数
ENL1=1;
ENL2=0;
ENR1=1;
ENR2=0;
zkbL=30;
zkbR=30;
INL=1;
INR=1;
}
void time01_init(){
TMOD=0x11;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
TR0=1;
ET0=1;
TF0=0;
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
TR1=1;
ET1=1;
TF1=0;
}
void qianjin(){
zkbL=30;
zkbR=30;}
void xiaozuo(){
zkbL=20;
zkbR=80;}
void dazuo(){
zkbL=10;
zkbR=100;}
void xiaoyou(){
zkbL=80;
zkbR=20;}
void dayou(){
zkbL=100;
zkbR=10;}
void xunji(){//循迹函数
if(L1==0&&L2==0&R2==1&R1==0)
fx=1;
else if(L1==0&&L2==0&R2==0&R1==1)
fx=2;
else if(L1==0&&L2==1&R2==0&R1==0)
fx=-1;
else if(L1==1&&L2==0&R2==0&R1==0)
fx=-2;
switch(fx){
case 1 : xiaoyou();break;
case 2 : dayou();break;
case -1 : xiaozuo();break;
case -2 : dayou();break;
default:qianjin();}}
void timer0() interrupt 1{//中断1 计时器2pwm驱动电机
TH0=(65536-250)/256;
TL0=(65536-250)%256;
TF0=0;
if(t<zkbL){
INL=1;}
else INL=0;
if(t<zkbR){
INR=1;}
else INR=0;
beep=~beep;
t=(t+1)%100;
}
void main(){//主函数
init();
time01_init();
while(1){
beep=0;
xunji();
}}
求助!!目前循迹部分都无法完成
不过用蜂鸣器和车轮试了试第一次中断好像会进入,但是出不来
|