中科杯机器人应用技术机器人车运行程序.docx
- 文档编号:2874436
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:59
- 大小:21.11KB
中科杯机器人应用技术机器人车运行程序.docx
《中科杯机器人应用技术机器人车运行程序.docx》由会员分享,可在线阅读,更多相关《中科杯机器人应用技术机器人车运行程序.docx(59页珍藏版)》请在冰点文库上搜索。
中科杯机器人应用技术机器人车运行程序
/*========================================================================================
【程序功能】:
STC12C5A60S2单片机双PWM输出;16位巡线功能;右转功能,A200
【编写日期】:
2011年2月25日
========================================================================================*/
#include
#include
sbitW0=P1^3;
sbitW1=P1^4;
sbitDIR_0=P1^2;//PWM0的正反转控制位,1代表正
sbitDIR_1=P1^5;//PWM1的正反转控制位,1代表正
sbitstart_=P4^6;//启动按键为
sbitP4_4=P4^4;//读取传感器信号控制为
sbitWP_8=P2^0;//传感器高8位
sbitWP_9=P2^1;
sbitWP_10=P2^2;
sbitWP_11=P2^3;
sbitWP_12=P2^4;
sbitWP_13=P2^5;
sbitWP_14=P2^6;
sbitWP_15=P2^7;
sbitP17=P1^7;
sbitP16=P1^6;
sbitO1=P1^0;
sbitO2=P1^1;
sbitO3=P3^2;//投球
sbitO4=P3^3;
sbitO5=P3^4;
sbitO6=P3^5;
sbitO7=P3^6;
sbitO8=P3^7;
bitW_0;//传感器低8位
bitW_1;
bitW_2;
bitW_3;
bitW_4;
bitW_5;
bitW_6;
bitW_7;
sbitP0_0=P0^0;//红蓝
sbitP0_1=P0^1;
sbitP0_2=P0^2;
sbitP0_3=P0^3;
sbitP0_4=P0^4;//左限位
sbitP0_5=P0^5;//右限位
sbitP0_6=P0^6;
sbitP0_7=P0^7;//接近开关
unsignedcharnum,c=0,jp=0,j,co,ball=0,sener;
unsignedcharmode;
#definel1//左电机
#definer2//右电机
#definerl3
//#defineS_B70//基准占空比设置
#definef'f'//代表电机正转
#defineforward'f'
#defineb'b'//代表电机反转
#definebackward'b'
/*=======================================================================
【函数原形】:
voidpwm0_1()
【功能说明】:
PCA时钟源控制寄存器计数初值设置
【编写日期】:
2011年1月20日
========================================================================*/
voidpwm0_1()
{
CCON=0x00;
CH=0;
CL=0;
CMOD=0x00;
}
/*=======================================================================
【函数原形】:
voidPWM0(unsignedcharFosc_1)
【参数说明】:
Fosc_0Fosc_0为占空比设置形参
【编写日期】:
2011年1月20日
========================================================================*/
voidPWM0(unsignedintFosc_0)//Fosc_0为占空比设置形参
{
CCAP0H=CCAP0L=((Fosc_0)*25)/10;
CCAPM0=0X42;//8位PWM输出,无中断
PCA_PWM0=0x00;
}
/*=======================================================================
【函数原形】:
voidPWM1(unsignedcharFosc_1)
【参数说明】:
Fosc_1Fosc_1为占空比设置形参
【编写日期】:
2011年1月20日
========================================================================*/
voidPWM1(unsignedintFosc_1)
{
CCAP1H=CCAP1L=((Fosc_1)*25)/10;
CCAPM1=0X42;
PCA_PWM1=0x00;//8位PWM输出,无中断
}
/*=======================================================================
【函数原形】:
voiddelay_ms(unsignedintT)
【参数说明】:
T延时时间设定
【函数功能】:
ms级延时函数
【编写日期】:
2011年1月20日
========================================================================*/
voiddelay_ms(unsignedintT)
{
unsignedintCON;
unsignedinti;
for(i=0;i for(CON=0;CON<500;CON++); } /*======================================================================= 【函数原形】: voidmotor(unsignedcharm,unsignedcharz,unsignedintn,) 【参数说明】: m电机,z方向,n占空比 【编写日期】: 2011年1月20日 ========================================================================*/ voidmotor(unsignedcharm,unsignedcharz,unsignedintn) {switch(m) { caser: if(z==forward||z==f){CR=1;PWM0(n);DIR_0=1;} elseif(z==backward||z==b){CR=1;PWM0(n);DIR_0=0;} break; casel: if(z==forward||z==f){CR=1;PWM1(n);DIR_1=1;} elseif(z==backward||z==b){CR=1;PWM1(n);DIR_1=0;} break; default: break; } } /*==================================================== 【函数原形】: voidstop(unsignedcharm) 【参数说明】: m: 选择电机 【功能说明】: 停止电机转动 【修改时间】: ====================================================*/ voidstop(unsignedcharm) { switch(m) { caser: /*CR=0;*/PWM0(0);break; casel: /*CR=0;*/PWM1(0);break; caserl: CR=0;PWM0(0);DIR_0=0;PWM1(0);DIR_1=0;break; default: break; } } /*==================================================== 【函数原形】: voidFOLL_FINE(unsignedintS_B,unsignedintti) 【功能说明】: 16位巡线功能 【参数说明】: S_B机器人基准速度设置ti巡线条数的设定 【编写时间】: 2011年1月25日 【修改时间】: 正巡线数线 ====================================================*/ voidFOLL_FINE(unsignedintS_B,unsignedintti) {intn=0; while (1) { num=0; motor(r,f,S_B); motor(l,f,S_B); P4_4=0;//读取低8位 if(WP_8)num++; if(WP_9)num++; if(WP_10)num++; if(WP_11)num++; if(WP_12)num++; if(WP_13)num++; if(WP_14)num++; if(WP_15)num++; W_0=WP_8; W_1=WP_9; W_2=WP_10; W_3=WP_11; W_4=WP_12; W_5=WP_13; W_6=WP_14; W_7=WP_15; P4_4=1; if(WP_8)num++;//读取高8位 if(WP_9)num++; if(WP_10)num++; if(WP_11)num++; if(WP_12)num++; if(WP_13)num++; if(WP_14)num++; if(WP_15)num++; if(num>8) { for(co=0;co<200;co++);//寻白条时去除干扰信号的影响 num=0; P4_4=0; if(WP_8)num++;//低位 if(WP_9)num++; if(WP_10)num++; if(WP_11)num++; if(WP_12)num++; if(WP_13)num++; if(WP_14)num++; if(WP_15)num++; W_0=WP_8; W_1=WP_9; W_2=WP_10; W_3=WP_11; W_4=WP_12; W_5=WP_13; W_6=WP_14; W_7=WP_15; P4_4=1; if(WP_8)num++;//高位 if(WP_9)num++; if(WP_10)num++; if(WP_11)num++; if(WP_12)num++; if(WP_13)num++; if(WP_14)num++; if(WP_15)num++; if(num>8) { c=c+1; } if(c==ti) { c=0; return; } else { delay_ms(300); } } if((S_B>=60)&&(S_B<=80)) {if(W_7&&WP_8) { motor(r,f,S_B); motor(l,f,S_B); n=0; } elseif(W_7) { motor(r,f,S_B-10); motor(l,f,S_B+10); n=0; } elseif(WP_8) { motor(r,f,S_B+10); motor(l,f,S_B-10); n=0; } elseif(W_6) { motor(r,f,S_B-20); motor(l,f,S_B+15); n--; } elseif(WP_9) { motor(r,f,S_B+15); motor(l,f,S_B-20); n++; } elseif(W_5) { motor(r,f,S_B-25); motor(l,f,S_B+15); n--; } elseif(WP_10) { motor(r,f,S_B+20); motor(l,f,S_B-30); n++; } elseif(W_4) { motor(r,f,S_B-30); motor(l,f,S_B+15); n--; } elseif(WP_11) { motor(r,f,S_B+20); motor(l,f,S_B-35); n++; } elseif(W_3) { motor(r,f,S_B-35); motor(l,f,S_B+20); n--; } elseif(WP_12) { motor(r,f,S_B+20); motor(l,f,S_B-50); n++; } elseif(W_2) { motor(r,f,S_B-50); motor(l,f,S_B+20); n--; } elseif(WP_13) { motor(r,f,S_B+20); motor(l,f,S_B-60); n++; } elseif(W_1) { motor(r,f,S_B-60); motor(l,f,S_B+20); n--; } elseif(WP_14) { motor(r,f,S_B+20); motor(l,f,S_B-60); n++; } elseif(W_0) { motor(r,f,S_B-60); motor(l,f,S_B+20); n--; } elseif(WP_15) { motor(r,f,S_B+20); motor(l,f,S_B-60); n++; } } else if(S_B>80)//基准速度大于80时的巡线程序 {if(W_7&&WP_8) { motor(r,f,S_B); motor(l,f,S_B); n=0; } elseif(W_7) { motor(r,f,S_B-10); motor(l,f,S_B+10); n=0; } elseif(WP_8) { motor(r,f,S_B+10); motor(l,f,S_B-10); n=0; } elseif(W_6) { motor(r,f,S_B-20); motor(l,f,S_B+10); n--; } elseif(WP_9) { motor(r,f,S_B+10); motor(l,f,S_B-20); n++; } elseif(W_5) { motor(r,f,S_B-50); motor(l,f,S_B+10); n--; } elseif(WP_10) { motor(r,f,S_B+10); motor(l,f,S_B-50); n++; } elseif(W_4) { motor(r,f,S_B-60); motor(l,f,S_B+10); n--; } elseif(WP_11) { motor(r,f,S_B+10); motor(l,f,S_B-60); n++; } elseif(W_3) { motor(r,f,S_B-60); motor(l,f,S_B+10); n--; } elseif(WP_12) { motor(r,f,S_B+10); motor(l,f,S_B-60); n++; } elseif(W_2) { motor(r,f,S_B-70); motor(l,f,S_B+10); n--; } elseif(WP_13) { motor(r,f,S_B+10); motor(l,f,S_B-70); n++; } elseif(W_1) { motor(r,f,S_B-80); motor(l,f,S_B+10); n--; } elseif(WP_14) { motor(r,f,S_B+10); motor(l,f,S_B-70); n++; } elseif(W_0) { motor(r,f,S_B-80); motor(l,f,S_B+10); n--; } elseif(WP_15) { motor(r,f,S_B+10); motor(l,f,S_B-80); n++; } elseif(! (W_0&&W_1&&W_2&&W_3&&W_4&&W_5&&W_6&&W_7&&WP_8&&WP_9&&WP_10&&WP_11&&WP_12&&WP_13&&WP_14&&WP_15)&&n<0) { DIR_0=0; DIR_1=1; delay_ms(80); motor(r,b,60); motor(l,f,60); delay_ms(500); do { P4_4=0; W_0=WP_8; W_1=WP_9; W_2=WP_10; W_3=WP_11; W_4=WP_12; W_5=WP_13; W_6=WP_14; W_7=WP_15; if(W_7) { delay_ms(3); P4_4=0; W_0=WP_8; W_1=WP_9; W_2=WP_10; W_3=WP_11; W_4=WP_12; W_5=WP_13; W_6=WP_14; W_7=WP_15; } } while(! (W_7)); } elseif(! (W_0&&W_1&&W_2&&W_3&&W_4&&W_5&&W_6&&W_7&&WP_8&&WP_9&&WP_10&&WP_11&&WP_12&&WP_13&&WP_14&&WP_15)&&n>0) { DIR_0=1; DIR_1=0; delay_ms(80); motor(r,f,60); motor(l,b,60); delay_ms(500); do { P4_4=1; if(WP_8) { delay_ms(3); P4_4=1; } } while(! (WP_8)); } } } } /*==================================================== 【函数原形】: voidTURN_90() 【功能说明】: 控制左右转向 【参数说明】: 【编写时间】: 2011年2月17日 【修改时间】: ====================================================*/ voidTURN_90(unsignedchare) { delay_ms(150); PWM0(0); PWM1(0); delay_ms(10); if(e==r)//右转控制选择语句 { DIR_0=0;//右 DIR_1=1;//左 delay_ms(80); motor(r,b,75); motor(l,f,65); delay_ms(1000); do { P4_4=0; W_0=WP_8; W_1=WP_9; W_2=WP_10; W_3=WP_11; W_4=WP_12; W_5=WP_13; W_6=WP_14; W_7=WP_15; if(W_5||W_4||W_3) { delay_ms(3); P4_4=0; W_0=WP_8; W_1=WP_9; W_2=WP_10; W_3=WP_11; W_4=WP_12; W_5=WP_13; W_6=WP_14; W_7=WP_15; } } while(! (W_5||W_4||W_3)); } else if(e==l)//左转控制选择语句 { DIR_0=1;//右 DIR_1=0;//左 delay_ms(80); motor(r,f,60); motor(l,b,80); delay_ms(1000); do { P4_4=1; if(WP_10||WP_11||WP_12) { delay_ms(3); P4_4=1; } } while(! (WP_10||WP_11||WP_12)); } PWM0(0); PWM1(0); delay_ms(20); DIR_0=1; DIR_1=1; delay_ms(80);//延时等待继电器动作 } /*==================================================== 【函数原形】: voidTURN_90l() 【功能说明】: 控制左右转向 【参数说明】: 【编写时间】: 2011年2月17日 【修改时间】: ====================================================*/ voidTURN_901(unsignedcharh) { delay_ms(150); PWM0(0); PWM1(0); delay_ms(10); if(h==r)//右转控制选择语句 { DIR_0=0;//右 DIR_1=1;//左 delay_ms(80); motor(r,b,6
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 中科杯 机器人 应用技术 运行 程序
![提示](https://static.bingdoc.com/images/bang_tan.gif)