超声波避障小车c程序(Ultrasonic obstacle avoidance car C program)
超声波避障小车c程序(Ultrasonic obstacle avoidance car C
program)
The trigger signal / angle P3.0
Accept / signal pin p3.2
#include / / device configuration file
#include
Sbit TX=P3^0; / / trigger signal pin
Sbit K1=P3^4;
Sbit K2=P3^5;
Sbit PWM1=P3^6; //pwm signal output
Sbit PWM2=P3^7;
Static char click=0;
Unsigned, char, ZK1, ZK2;
Unsigned int time=0;
Unsigned int timer=0;
Unsigned char posit=0;
Unsigned long S=0;
Bit flag =0;
Unsigned char const discode[] ~0xC0 ={, ~0xF9, ~0xA4, ~0xB0,
~0x99, ~0x92, ~0x82, ~0xF8, ~0x80, ~0x90};
Unsigned, char, const, positon[3]={, 0x7F, 0xbf, 0xdf};
Unsigned char disbuff[4] 0,0,0,0 ={};
Void delay (void) / 0us error
{
Unsigned, char, a, b;
For (b=215; b>0; b--)
For (a=45; a>0; a--);
}
Void Display (void)
{
{P0=discode[disbuff[posit]];}
P2=positon[posit];
If (++posit>=3)
Posit=0;
}
Void Conut (void)
{
Time=TH0*256+TL0;
TH0=0;
TL0=0;
S= (time*1.7) /100;
Disbuff[0]=S%1000/100;
Disbuff[1]=S%1000%100/10;
Disbuff[2]=S%1000%10%10;
}
Void, Timer2Interrupt ()
{
RCAP2H=0x0fe;
RCAP2L=0x33;
ET2=1; / / allow T2 timer interrupt
EA=1; / / open interrupt
TR2=1; / / T2 start timer
}
Void zd0 () interrupt 1
{
Flag=1;
}
The void zd3 () interrupt 3 //T1 interrupt is used to scan the
digital tube and the 800MS boot module
{
TH1=0x0f8;
TL1=0x30;
Display ();
Timer++;
If (timer>=400)
{
Timer=0;
TX=1; //800MS starts the module once
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
TX=0;
}
}
Void Timer2 (void) interrupt 5
{
TF2=0; / / T2 timer overflow interrupt when the user needs to
clear the overflow flag
RCAP2H=0x0fe;
RCAP2L=0x33; / * * / recovery timer initial value
++click;
If (click>=100) click=0;
If (click<=ZK1)
PWM1=1;
Else
PWM1=0;
If (click<=ZK2)
PWM2=1;
Else
PWM2=0;
}
Void main (void)
{
TMOD=0x19; / / T0 1, GATE=1;
TH0=0;
TL0=0;
TH1=0x0f8; //2MS timing
TL1=0x30;
ET0=1; / / T0 is allowed to interrupt
ET1=1; / / T1 is allowed to interrupt
TR1=1; / / open timer
Timer2Interrupt ();
EA=1; / / open total outage
ZK1=20;
ZK2=20;
While (1)
{
While (INT0==0); / / when RX is zero wait
TR0=1;
While (INT0==1); / / when RX is 1 and wait for the count
TR0=0; / / close count
(Conut); / / calculation
If (S>40) / / acceleration control
{
ZK1=ZK1+10;
ZK2=ZK2+10;
P1=0x0a;
}
Else
If ((S<30) & (K1==0) & (K2==0)) / / steering control
{
ZK1=ZK1-5;
ZK2=ZK2-5;
P1=0X08;
Delay ();
}
Else
If ((S<30) & (K1==1) & (K2==0))
{
ZK1=ZK1-5;
ZK2=ZK2-5;
P1=0X01;
Delay ();
}
Else
If ((S<30) & (K1==0) & (K2==1))
{
ZK1=ZK1-5;
ZK2=ZK2-5;
P1=0X04;
Delay ();
}
Else
If ((S<30) & (K1==1) & (K2==1))
{
ZK1=ZK1-5;
ZK2=ZK2-5;
P1=0X05;
Delay ();
}
If (ZK1>99) ZK1=1;
If (ZK1<1) ZK1=99;
If (ZK2>99) ZK2=1;
If (ZK2<1) ZK2=99;
}
}
本文档为【超声波避障小车c程序(Ultrasonic obstacle avoidance car C program)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。