標(biāo)題:
這個程序 過紅外循跡直道彎會沖出去 ,有能幫忙解答解答的嗎
[打印本頁]
作者:
LLLhh
時間:
2018-7-2 09:53
標(biāo)題:
這個程序 過紅外循跡直道彎會沖出去 ,有能幫忙解答解答的嗎
#include<reg52.h>
#include<pwmkl.h>
unsigned int count,i,j;
unsigned int n;
void delay(int xms)
{
for(i=1000;i--;i>0)
for(j=xms;j--;j>0);
}
void xunji()
{
if((leftxled==0)&&(rightxled==0)/*&&(leftxled1==0)&&(rightxled1==0)*/) //向前走
{
n=0;
}
else if((leftxled==1)&&(rightxled==0)/*&&(leftxled1==0)&&(rightxled1==0)*/) //左1
{
n=3;
}
/* else if((leftxled==0)&&(rightxled==1)&&(leftxled1==0)&&(rightxled1==0)) //左2
{
n=2;
}*/
else if((leftxled==0)&&(rightxled==1)/*&&(leftxled1==0)&&(rightxled1==0)*/)//右1
{
n=1;
}
/*else if((leftxled==0)&&(rightxled==0)&&(leftxled1==0)&&(rightxled1==1))//右2
{
n=4;
}*/
else if((leftxled==1)&&(rightxled==1)/*&&(leftxled1==1)&&(rightxled1==1)*/)//停
{
n=5;
}
switch(n)
{
case(0):run();;break;
case(1):leftrun();break;
case(2):rightrun();break;
case(3):rightrun();break;
case(4):rightrun();break;
case(5):stop();break;
default:break;
}
}
void main()
{
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
/*TH1=0xff;
TL1=0xa3; */
EA=1;
/*ET1=1;
TR1=1;*/
ET0=1;
TR0=1;
/*IT0=1;
EX0=1;*/
while(1)
{
xunji();
}
}
/*void Time1_Int() interrupt 3//
{
TH1=0xff;
TL1=0xa3;
count=(count+1);//0.5ms次數(shù)加1
count=count%40;//次數(shù)始終保持為40即保持周期為20ms
}*/
/*中斷*/
void Int0() interrupt 0
{
if(leftxled==0&&rightxled==1)//靠左邊1檢查到白,右邊檢查到黑
{
leftrun();
delay(100);
stop();
}
if(leftxled1==0&&rightxled1==1)//靠左邊2檢查到白,右邊檢查到黑
{
leftrun();
delay(100);
stop();
}
if(leftxled==1&&rightxled==0) //靠右邊1檢查到白,左邊檢查到黑
{
rightrun();
delay(100);
stop();
}
if(leftxled1==1&&rightxled1==0) //靠右邊1檢查到白,左邊檢查到黑
{
rightrun();
delay(100);
stop();
}
}
/****************************************pwm 頭文件***************************/
#ifndef _LED_H_
#define _LED_H_
sbit IN1=P1^0; // 右1電機(jī)
sbit IN2=P1^1; // 右1電機(jī)
sbit IN3=P1^2; // 左1電機(jī)
sbit IN4=P1^3; // 左1電機(jī)
//循跡傳感器定義 左P3^6,右P3^5。
#define leftxled P3_6 //靠左邊1循跡小燈
#define rightxled P3_5 //右1
#define leftxled1 P3_4 //靠左邊2循跡小燈
#define rightxled1 P3_7
/*避障傳感器定義 左P3^7,右P3^4。
#define leftbled P3^7 //右邊循跡小燈
#define rightbled P3^4 */
//定義pwm始能端
#define leftpwm P1_4 //左邊pwm 始能端
#define rightpwm P1_5 //右邊pwm 始能端
//電機(jī)轉(zhuǎn)向
#define leftgo {IN1=1,IN2=0;} //左邊電機(jī)前進(jìn)
#define leftstop {IN1=0,IN2=0;}// 左 停
#define rightgo {IN3=1,IN4=0;} //右邊電機(jī)前進(jìn)
#define rightstop {IN3=0,IN4=0;}// 右 停
#define leftback {IN1=0,IN2=1;} //左邊電機(jī)后退
#define rightback {IN3=0,IN4=1;} //右邊電機(jī)后退
//定義pwm、占空比
unsigned char pwmleft =0;//變量定義
unsigned char pwmright =0;
unsigned char pushleft =0;//占空比N/10
unsigned char pushright =0;
//定義固定變量
bit k=1;
bit kk=1;
unsigned int time=0;
//前進(jìn)函數(shù)
void run()
{
pushleft=4;
leftgo;
pushright=4;
rightgo;
}
//houtui
void back()
{
leftback;
rightback;
}
void stop()
{
leftstop;
rightstop;
}
//左轉(zhuǎn)函數(shù)
void leftrun()
{
pushleft=15;
leftgo;
pushright=12;
rightback;
}
//右轉(zhuǎn)函數(shù)
void rightrun()
{
pushleft=12;
leftback;
pushright=15;
rightgo;
}
/******************pwm 調(diào)速************************************/
//左電機(jī)進(jìn)行調(diào)速
void leftpwml()
{
if(k)
{
if(pwmleft<=pushleft)
{
leftpwm=1;
}
else
{
leftpwm=0;
}
if(pwmleft>=20)
{
pwmleft=0;
}
}
else
{
leftpwm=1;
}
}
//右電機(jī)調(diào)速
void rightpwmr()
{
if(kk)
{
if(pwmright<=pushright)
{
rightpwm=1;
}
else
{
rightpwm=0;
}
if(pwmright>=20)
{
pwmright=0;
}
}
else
{
rightpwm=1;
}
}
//定時器0產(chǎn)生中斷信號
void time0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
time++;
pwmleft++;
pwmright++;
leftpwml();
rightpwmr();
}
#endif
復(fù)制代碼
作者:
LLLhh
時間:
2018-7-2 09:54
這個程序 過紅外循跡直道彎會沖出去 ,有能幫忙解答解答的嗎
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1