初步探月小车程序Word下载.docx
《初步探月小车程序Word下载.docx》由会员分享,可在线阅读,更多相关《初步探月小车程序Word下载.docx(18页珍藏版)》请在冰点文库上搜索。
ucharx=0,y=0,i=1;
ucharl=0;
bitzd=0,sd=0,quwutingzhi=1;
bita=0,b=0;
m=0;
n=0;
bitq=0,w=0,eee=0,rrr=0,zidongmoshi=0;
voiddelay(uintz)
{
uintx,y;
for(x=z;
x>
0;
x--)
for(y=122;
y>
y--);
}
voidpwm()
TMOD=0X20;
/*设定T0的工作模式为2*/
//TH1=1000;
/*100us中断一次*/
//TL1=1000;
EA=1;
ET1=1;
TR1=1;
/************
初始化中断
*************/
voidTimer0Init(){
//0度=0.5ms,45度=1ms,90度=1.5ms,135度=2ms,180度=2.5ms
//0.5ms初始值FE33,(12n/11059200=0.5/1000,n=460,X=65535-460=65075>
FE33)
TMOD|=0x01;
//使用模式1,16位定时器,使用"
|"
符号可以在使用多个定时器时不受影响
TH0=0xFF;
//建议直接赋值,如果计算会出现偏差
TL0=0x06;
//定时周期为0.25ms
//总中断打开
ET0=1;
//定时器中断打开
TR0=1;
//定时器开关打开
//向左转动
***********/
voidServoLeft(){
jd=jd-1;
//角度标识减1
count=0;
timer=0;
if(jd==0)
jd=2;
//已经是0度,则保持
}
voidServoLeft1(){
jd1=jd1-1;
if(jd1==0)
jd1=2;
voidServoLeft2(){
jd2=jd2-2;
if(jd2==0)
jd2=2;
voidServoLeft3(){
jd3=jd3-2;
if(jd3==0)
jd3=2;
voidServoLeft4(){
jd3=jd3-4;
/***********
//向右转动
voidServoRight(){
jd=jd+1;
//角度标识加1
if(jd==6)
jd=10;
//已经是180度,则保持
voidServoRight1(){
jd1=jd1+1;
if(jd1==6)
jd1=10;
voidServoRight2(){
jd2=jd2+2;
if(jd2==6)
jd2=10;
voidServoRight3(){
jd3=jd3+7;
if(jd3==6)
jd3=10;
/**********
//从任何地方回到90度地方
**********/
voidServo90(){
jd=6;
//回到中间
voidServo901(){
jd1=6;
//按键按下则20ms周期从新开始
voidServo902(){
jd2=6;
voidServo903(){
jd3=6;
voids_w()//捡物体
{
//x=0;
y=0;
a=1;
b=0;
quwutingzhi=0;
left1=0;
left2=0;
right1=0;
right2=0;
//小车停止
//Servo902();
//delay(9000);
//ServoLeft2();
x=0;
a=0;
b=1;
//Servo903();
ServoRight3();
delay(9000);
//while
(1);
}
voids_w1()
quwutingzhi=0;
Servo902();
//delay(9000);
Servo903();
ServoLeft3();
ServoLeft2();
quwutingzhi=1;
voidzd_douji()//云台自动转
x=1;
Servo90();
for(i=0;
i<
3;
i++)
ServoRight();
};
for(i=0;
ServoLeft();
delay(8500);
2;
voidsd_douji()//手动控制云台
if(P3==0xc1){x=1;
delay(9000);
if(P3==0xc2){x=1;
if(P3==0xc4){x=0;
y=1;
ServoRight1();
if(P3==0xc8){x=0;
ServoLeft1();
//if(jiguang1==1){turn(4,5);
turn(0,5);
//if(jiguang2==1){turn(4,5);
//if(jiguang1==3){turn(3,5);
//if(P3==0xd0)break;
//if(P3==0xe0)break;
voidturn(uchardirection,charspeed1,charspeed2)//小车行走方式
pro_left=speed1;
pro_right=speed2;
switch(direction)
case0:
left1=1;
right1=1;
break;
/*前进*/
case1:
/*停止*/
case2:
delay(1000);
left1=0;
left2=1;
right2=1;
delay(10000);
/*后退*/
case3:
delay(5000);
/*左转*/
case4:
/*右转*/
case5:
delay(500);
case6:
//***超声波模块*******//
/*
voidStartModule()//启动模块
TX=1;
//启动一次模块
_nop_();
TX=0;
}*/
voidzd_panduan()//云台的自动模式
zd_douji();
//turn(0,7);
//if(P3==0xe0)break;
voidsd_panduan()//手动控制舵机
if(P3==0xc1){turn(0,6,5);
while(P3==0xc1);
if(P3==0xc2){turn(1,7,7);
while(P3==0xc2);
if(P3==0xc4){turn(2,7,7);
while(P3==0xc4);
if(P3==0xc8){turn(3,9,9);
while(P3==0xc8);
if(P3==0xd0){turn(4,9,9);
while(P3==0xd0);
if(P3==0xf0){turn(5,9,9);
if(P3==0xcc){turn(6,9,9);
while(sd==1)sd_douji();
while(zd==1)zd_panduan();
while(m==1){s_w();
while(n==1){s_w1();
}m,m
//if(jiguang3==1){turn(3,5);
时钟中断0
************/
voidServoInt()interrupt1
inttmp1,tmp2,i;
if(x==1)
if(count<
jd){
PWN=1;
}else{
PWN=0;
if(y==1)
jd1){
PWN1=1;
PWN1=0;
if(a==1)
jd2){
PWN2=1;
PWN2=0;
if(b==1)
jd3){
PWN3=1;
PWN3=0;
if(count>
80&
&
timer<
100){//只循环100个周期,以免太费电
//次数始终保持为40=(20/0.5)即保持周期为20ms
timer++;
}elseif(count<
=80&
100){
count=count+1;
//0.5ms次数加1
{tmp1=pro_left;
tmp2=pro_right;
if(i<
tmp1)en1=1;
elseen1=0;
tmp2)en2=1;
elseen2=0;
i++;
if(i>
=25)i=0;
/*1个pwm信号由10次的中断产生(4.6ms)*/
if(P3==0xe0){while(P3==0xe0);
sd=~sd;
if(P3==0xc3){while(P3==0xc3);
zd=~zd;
if(P3==0xd4){while(P3==0xd4);
m=1;
if(P3==0xe8){while(P3==0xe8);
n=1;
}
voidtimer0()interrupt3
/*bitcishu=0,biaozhi=0,cishu1=0;
uintjishu0=0,jishu=0,cishu2=0;
/*inttmp1,tmp2,i;
=20)i=0;
/*1个pwm信号由10次的中断产生(1ms)*/
//if(jiguang1==1){turn(4,7);
turn(0,7);
if(zidongmoshi==1)
if(quwutingzhi==1)
{if(jiguang2==1){q=1;
w=1;
if(q==1){pro_left=15;
pro_right=15;
}//{pro_left=pro_right=5;
delay(50);
left1=1;
if(q==0&
w==1){pro_left=6;
pro_right=5;
w=0;
q=0;
if(jiguang1==1){eee=1;
rrr=1;
if(eee==1){pro_left=15;
if(eee==0&
rrr==1){pro_left=6;
rrr=0;
eee=0;
}
/*if(jiguang3==1){eee=1;
if(eee==1){pro_left=7;
rrr==1){pro_left=7;
rrr==0;
*/
zidongmoshi=~zidongmoshi;
if(++l>
=2){left1=~left1;
right1=~right1;
l=0;
}}
if(g_m==1)d_p=1;
elsed_p=0;
/*StartModule();
if(RX==1&
cishu==0){jishu0=TH0*256+TL0;
cishu=1;
cishu1=1;
};
//当RX为1计数并等待
if(cishu1==1)cishu2++;
if(RX==0&
cishu==1){jishu=(cishu2-1)*5+TH0*256+TL0-jishu0,cishu=0;
biaozhi=1;
cishu1=0;
cishu2=0;
if(biaozhi==1)
{S=(jishu*1.7)/100;
jishu=0;
if(S>
=50){turn(4,5);
}biaozhi=0;
}*bg*/
voidmain()
pwm();
Timer0Init();
while
(1)
//sd_panduan();
sd_panduan();