-
/******
**************************************************
******************
**
*/
硬件连接
************
**************************************************
*************
#include
#include
#define Sevro_moto_pwm
P2_7
//
接舵机信号端输入
PWM
信号调节速度
#define
ECHO
P2_4
//
超声波接口定义
#define
TRIG
P2_5
//
超声波接口定义
#define
Left_moto_go
{P1_0=1,P1_1=0,P1_2=1,P1_3=0;}
//<
/p>
左边两个电机向前
走
#define Left_moto_back
{P1_0=0,P1_1=1,P1_2=0,P1_3=1;}
//
左边两个电机向后
转
#define
Left_moto_Stop
{P1_0=0,P1_1=0,P1_2=0,P1_3=0;}
//
左边两个电机停转
#define Right_moto_go
{P1_4=1,P1_5=0,P1_6=1,P1_7=0;}
//
右边两个电机向前走
#define Right_moto_back
{P1_4=0,P1_5=1,P1_6=0,P1_7=1;}
//
右边两个电机向前走
#define Right_moto_Stop
{P1_4=0,P1_5=0,P1_6=0,P1_7=0;}
//
右边两个电机停转
unsigned
char
const
discode[]
={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0
xF8,0x80,0x90,0xBF,0xff/*-*/};
unsigned char const positon[3]={
0xfe,0xfd,0xfb};
unsigned
char disbuff[4]
={
0,0,0,0,};
unsigned char posit=0;
unsigned char pwm_val_left
=
0;//
变量定义
unsigned char push_val_left
=14;//
舵机归中,产生约,
1.5MS
信号
unsigned long
S=0;
unsigned long S1=0;
unsigned long S2=0;
unsigned long S3=0;
unsigned long S4=0;
unsigned int
time=0;
//
时间变量
unsigned int
timer=0;
//
延时基准变量
unsigned char timer1=0;
//
扫描时间变量
/**************************
**********************************************/
void
delay(unsigned int k)
//
延时函数
{
unsigned int
x,y;
for(x=0;x
for(y=0;y<2000;y++);
}
/*******************************************
*****************************/
void Display(void)
//
扫描数码管
{
if(posit==0)
p>
{P0=(discode[disbuff[posit]])&0x7f;}//
产生点
else
{P0=discode[disbuff[posit]];}
if(posit==0)
{ P2_1=0;P2_2=1;P2_3=1;}
if(posit==1)
{P2_1=1;P2_2=0;P2_3=1;}
if(posit==2)
{P2_1=1;P2_2=1;P2_3=0;}
if(++posit>=3)
posit=0;
}
/******************************************* *****************************/
void
StartModule()
//
启动测距信号
{
TRIG=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TRIG=0;
}
<
/p>
/***************************************
************/
void Conut(void)
//
计算距离
{
while(!ECHO);
//<
/p>
当
RX
为零时等待
TR0=1;
//
开启计数
while(ECHO);
//<
/p>
当
RX
为
1
p>
计数并等待
TR0=0;
//
关闭计数
time=TH0*256+TL0;
//
读取脉宽长度
TH0=0;
TL0=0;
S=(time*1.7)/100;
//
算出来是
CM
disbuff[0]=S%1000/100;
//
更新显示
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10 %10;
}
/*************************
***********************************************/
//
前速前进
void
run(void)
{
Left_moto_go
//
左电机往前走
Right_moto_go
//
右电机往前走
}
/*************************
***********************************************/
//
前速后退
void
backrun(void)
{
Left_moto_back
//
左电机往前走
Right_moto_back
;
//
右电机往前走
}
/*************************
***********************************************/
//
左转
void
leftrun(void)
{
Left_moto_back
//
左电机往前走
Right_moto_go
//
右电机往前走
}
/*************************
***********************************************/
//
右转
void
rightrun(void)
{
Left_moto_go
//
左电机往前走
Right_moto_back
;
//
右电机往前走
}
/*************************
***********************************************/
//STOP
void
stoprun(void)
{
-
-
-
-
-
-
-
-
-
上一篇:商务英语对话:商务宴请
下一篇:通用示波器操作中应注意的几个问题