版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、附录1 main.c#include "wei.h"#defineP_IOA_Data(volatile unsigned int *)0x7000#defineP_IOA_Dir(volatile unsigned int *)0x7002#defineP_IOA_Attrib(volatile unsigned int *)0x7003#defineP_IOB_Data(volatile unsigned int *)0x7005#defineP_IOB_Dir(volatile unsigned int *)0x7007#defineP_IOB_Attrib(vola
2、tile unsigned int *)0x7008#defineP_TimerB_Data(volatile unsigned int *)0x700C#defineP_TimerB_Ctrl(volatile unsigned int *)0x700D#defineP_INT_Ctrl(volatile unsigned int *)0x7010#defineP_INT_Clear(volatile unsigned int *)0x7011#defineP_ADC(volatile unsigned int *)0x7014#defineP_ADC_Ctrl(volatile unsig
3、ned int *)0x7015#defineP_ADC_MUX_Ctrl(volatile unsigned int *)0x702B#defineP_ADC_MUX_Data(volatile unsigned int *)0x702C#defineC_FIQ_PWM0x8000/ P_INT_Ctrl#defineC_FIQ_TMA0x2000/ P_INT_Ctrl#defineC_FIQ_TMB0x0800/ P_INT_Ctrl#defineC_IRQ4_1KHz0x0010/ P_INT_Ctrl#defineC_IRQ4_2KHz0x0020/ P_INT_Ctrl#defin
4、eC_IRQ4_4KHz0x0040/ P_INT_Ctrl#defineC_IRQ5_2Hz0x0004/ P_INT_Ctrl#defineC_IRQ5_4Hz0x0008/ P_INT_Ctrl#defineC_TMB_32KHz0x0002/ P_TimerB_Ctrl#defineC_TMB_PWM_OFF0x0000/ P_TimerB_Ctrl#defineP_Watchdog_Clear(volatile unsigned int *)0x7012unsigned int mm,min,cny,Delaynumber,xp=255,flag,dd; unsigned int l
5、eft,right; unsigned int pulse_x,pulse_y;unsigned int paixu7; /排序unsigned int chdata7; /火焰传感器检测通道unsigned int workstate=0;/进入检测范围unsigned int sucessfire=0;/灭火成功标记void PWM_left(unsigned int high_time,unsigned int cyclical_time);void PWM_right(unsigned int high_time,unsigned int cyclical_time);void for
6、ward(unsigned int m,unsigned int n) ;void forward_s(unsigned int m,unsigned int n);void forward_s2(unsigned int m,unsigned int n); void backward(unsigned int m,unsigned int n);void turn_left (unsigned int m);void turn_right(unsigned int m);void stop(void);void get_AD(void); /AD转换void system_init(voi
7、d); /系统初始化void delay_ms(unsigned int t ); /ms级延时程序void delay_s(unsigned int t ); /s级延时程序void adjust(void); /调整void adjust_s(void); /粗调void seekfire(void); /寻找火源void fire(void); /灭火void hf(void);int abs(int m);void bizhan(void ) ; /避障void biya(void ); /避崖void Stage0(void);/阶段0void Stage1(void); /阶段1v
8、oid Stage2(void); /阶段2void Stage3(void); /阶段3void revison();/修正函数/+主函数+/int main(void) system_init(); delay_s(3); while(1) seekfire(); fire(); delay_s(8); /*系统初始化*/void system_init(void) _asm("INT OFF"); *P_IOA_Dir=0xE700; / *P_IOA_Attrib=0xE7FF; *P_IOA_Data=0x1800; *P_IOB_Dir = 0x0FFF; *P
9、_IOB_Attrib = 0x0FFF; *P_IOB_Data = 0xF000; *P_TimerB_Ctrl=(C_TMB_32KHz|C_TMB_PWM_OFF); *P_TimerB_Data=(65536-0.1*0.001*32768); *P_INT_Ctrl |=(C_FIQ_TMB + C_IRQ4_1KHz + C_IRQ5_2Hz + C_IRQ5_4Hz); /TMB用来控制2个电机,1KHZ控制避崖, _asm("INT IRQ,FIQ"); /2HZ用来控制AD采集 4HZ用来避障/*寻找火源*/void seekfire(void)Stag
10、e0(); /阶段0 按一定的路径走Stage1(); /阶段1 有发现火源,做初步调整Stage2(); /阶段2 接近火源,边走边调整Stage3(); /阶段3 到达火源前面,做最后的调整 void Stage0(void) /阶段0 flag=0;while(1) turn_left(120); /转360度 扫描有没有火源 forward(250,250); if(workstate=1 && flag=0) break; void Stage1(void) /阶段1 flag=1;Set_IOB_Data(0,1); / 开启蜂鸣器报警adjust_s(); swi
11、tch(mm) case 1: forward(60,60); break; case 2: forward(55,55); break; case 3: forward(50,50); break; case 4: forward(45,45); break; case 5: forward(35,35); break; case 6: forward(30,30); break; case 7: forward(20,20); break; case 8: forward(10,10); break; case 9: forward(5,5); break; case 10: forwar
12、d(2,2); break; case 11: forward_s(1,1); break; default: break; void Stage2(void) /阶段2 flag=2; while(1) adjust(); switch(mm) case 1: forward(40,40); break; case 2: forward(30,30); break; case 3: forward(25,25); break; case 4: forward(20,20); break; case 5: forward(18,18); break; case 6: forward(15,15
13、); break; case 7: forward(12,12); break; case 8: forward(10,10); break; case 9: forward(5,5); break; case 10: forward(2,2); break; case 11: forward_s(1,1); break; default: break; if(chdata3<=0x05 | chdata4<= 0x05)break; void Stage3(void) /阶段3 灭火前最后调整 unsigned int i; flag=3;for(i=0;i<20;i+)
14、if(chdata3<=0x05 && chdata4<=0x05 && abs(chdata3-chdata4)<=2 &&abs(chdata2-chdata5)<=8 ) break;if(chdata3<=0x05 && chdata4 >0x05 ) turn_left(1) ; delay_ms(10); if(chdata3>0x05 && chdata4 <=0x05) turn_right(1); delay_ms(10); stop(); /*灭火
15、*/void fire() unsigned int n; while(chdata3<=0x7f | chdata4<=0x7f) if(n>=1)revison();/如果1次吹不灭则调用修正 Set_IOA_Data(15,1); / 灭火风扇开启 delay_s(8); Set_IOA_Data(15,0); / 灭火风扇关闭 delay_s(3); n+; sucessfire=1;/ 1 灭火成功 workstate=0; flag=0; Set_IOB_Data(0,0); / 开启蜂鸣器报警 xp=255; mm=0; min=255;/*修正子程序*/voi
16、d revison() forward(8,8); adjust(); /*调整*/void adjust_s(void) if(min=chdata1)turn_left(21); if(min=chdata2)turn_left(12); if(min=chdata3 && abs(chdata3-chdata4)>30 )turn_left(5) ; if(min=chdata4 && abs(chdata3-chdata4)>30 )turn_right(5); if(min=chdata5) turn_right(15); if(min=c
17、hdata6) turn_right(30); void adjust(void) while(min<=0xc9) if(min=chdata1)turn_left(21); delay_ms(20); if(min=chdata2)turn_left(5); delay_ms(20); if( min=chdata3 && abs(chdata3-chdata4)>20 )turn_left(1); delay_ms(20); if( min=chdata4 && abs(chdata3-chdata4)>20 )turn_right(1)
18、; delay_ms(20); if( min=chdata3 && abs(chdata3-chdata4)<=20 ) break; if( min=chdata4 && abs(chdata3-chdata4)<=20 ) break; if(min=chdata5) turn_right(5); delay_ms(20); if(min=chdata6) turn_right(30); delay_ms(20); /*前进*/void forward(unsigned int m,unsigned int n) pulse_x=0; puls
19、e_y=0; while(1) if(pulse_x<=m && pulse_y<=n) PWM_left(17,217); PWM_right(13,213); if(pulse_x>m && pulse_y<=n) PWM_right(13,213); if(pulse_x<=m && pulse_y>n) PWM_left(17,217); if(pulse_x>m && pulse_y>n) pulse_x=0;pulse_y=0 ; break; if(workstate=
20、1 && flag=0) pulse_x=0;pulse_y=0 ; break; void forward_s(unsigned int m,unsigned int n) pulse_x=0; pulse_y=0; while(1) if(pulse_x<=m && pulse_y<=n) PWM_left(17,417); PWM_right(13,413); if(pulse_x>m && pulse_y<=n) PWM_right(13,413); if(pulse_x<=m && puls
21、e_y>n) PWM_left(17,417); if(pulse_x>m && pulse_y>n) pulse_x=0;pulse_y=0 ; break; if(workstate=1 && flag=0) pulse_x=0;pulse_y=0 ; break; void forward_s2(unsigned int m,unsigned int n) pulse_x=0; pulse_y=0; while(1) if(pulse_x<=m && pulse_y<=n) PWM_left(15,215);
22、PWM_right(15,215); if(pulse_x>m && pulse_y<=n) PWM_right(15,215); if(pulse_x<=m && pulse_y>n) PWM_left(15,215); if(pulse_x>m && pulse_y>n) pulse_x=0;pulse_y=0 ; break; if(workstate=1 && flag=0) pulse_x=0;pulse_y=0 ; break; /*后退*/void backward(unsigne
23、d int m,unsigned int n) pulse_x=0; pulse_y=0; while(1) if(pulse_x<=m && pulse_y<=n) PWM_left(13,213); PWM_right(17,217); if(pulse_x>m && pulse_y<=n) PWM_right(17,217); if(pulse_x<=m && pulse_y>n) PWM_left(13,213); if(pulse_x>m && pulse_y>n) pul
24、se_x=0;pulse_y=0 ; break; if(workstate=1 && flag=0) pulse_x=0;pulse_y=0 ; break; /*左转*/void turn_left(unsigned int m) pulse_x=0; pulse_y=0; while(1) if(pulse_x<=m && pulse_y<=m) PWM_left(13,413); PWM_right(13,413); if(pulse_x>m && pulse_y<=m) PWM_right(13,413); if
25、(pulse_x<=m && pulse_y>m) PWM_left(13,413) ; if(pulse_x>m && pulse_y>m) pulse_x=0;pulse_y=0 ; break; if(workstate=1 && flag=0) pulse_x=0;pulse_y=0 ; break; /*右转*/void turn_right(unsigned int m) pulse_x=0; pulse_y=0;while(1) if(pulse_x<=m && pulse_y<=
26、m) PWM_left(17,417); PWM_right(17,417); if(pulse_x>m && pulse_y<=m) PWM_right(17,417); if(pulse_x<=m && pulse_y>m) PWM_left(17,417) ; if(pulse_x>m && pulse_y>m) pulse_x=0;pulse_y=0 ; break; if(workstate=1 && flag=0) pulse_x=0;pulse_y=0 ; break; /*停止*
27、/void stop(void)*P_IOA_Data &=(bit13 + bit14); /将IOA13、IOA14清0 pulse_x=0; pulse_y=0;/*控制小车左轮*/void PWM_left(unsigned int high_time,unsigned int cyclical_time) if(left <= high_time) /*high_time为脉冲的高电平时间,cyclical_time为脉冲周期时间 */Set_IOA_Data(14,1); / 将IOA14置1else Set_IOA_Data(14,0); / 将IOA14清0if(
28、left > cyclical_time)left=0;pulse_x +; *P_Watchdog_Clear=0x0001;/*控制小车右轮*/void PWM_right(unsigned int high_time,unsigned int cyclical_time)if(right <= high_time) Set_IOA_Data(13,1); / 将IOA13置1else Set_IOA_Data(13,0); / 将IOA13清0if(right > cyclical_time)right=0;pulse_y +; *P_Watchdog_Clear=0x
29、0001;/*IRQ4中断*/void IRQ4(void) _attribute_ (ISR);void IRQ4(void)if(*P_INT_Ctrl & C_IRQ4_1KHz) *P_INT_Clear |=C_IRQ4_1KHz ; dd+; if(dd>=100) /100ms检测一次 dd=0; biya(); if(*P_INT_Ctrl & C_IRQ4_2KHz) *P_INT_Clear |=C_IRQ4_2KHz ;else *P_INT_Clear |=C_IRQ4_4KHz; /*IRQ5中断*/void IRQ5(void) _attrib
30、ute_ (ISR);void IRQ5(void)if(*P_INT_Ctrl & C_IRQ5_4Hz) /IRQ5_4HZ *P_INT_Clear |=C_IRQ5_4Hz ; bizhan(); /避障 else /IRQ5_2HZ *P_INT_Clear |=C_IRQ5_2Hz; hf(); /比较检测到AD的最小值 if(xp<=0xc8)workstate=1;/ AD值达到200以内就认为发现火源。workstate=1进入检测范围 /*A/D转换*/void get_AD(void) unsigned int ak15; unsigned int i,j,
31、temp,k,sum; unsigned int Line,AD_Data; *P_ADC_Ctrl=0x01; /允许转换 for(Line=1;Line<7;Line+) *P_ADC_MUX_Ctrl=Line;/选择通道 AD_Data=*P_ADC_MUX_Data;/启动转换 for(i=0;i<15;i+) while(!(*P_ADC_MUX_Ctrl&0x8000); /没有转换完则继续等待 AD_Data=*P_ADC_MUX_Data; /读转换后的数据 AD_Data =AD_Data>>8 ; /右移8位,只取8高位数据 aki=AD_
32、Data; for(i=0;i<15;i+) /从小到大排序 for(j=i+1;j<15;j+) if(aki>akj)temp=aki;aki=akj;akj=temp; sum=0; for(k=5;k<10;k+) sum=sum+akk; chdataLine =sum/5; *P_Watchdog_Clear=0x01; /*/ void hf(void) unsigned int i,j,temp; get_AD();for(i=1;i<=6;i+)paixui=chdatai;for(i=1;i<=6;i+) for(j=i+1;j<=
33、6;j+) if(paixui>paixuj)temp=paixui;paixui=paixuj;paixuj=temp; min=paixu1; /min 为单前这次检测到的最低值xp =xp<min ? xp:min ; /xp 为检测到的历史最低值if(0xC9<xp && xp<=0xFF) mm=1; if(0xA0<xp && xp<=0xC9) mm=2; if(0x90<xp && xp<=0xA0) mm=3;if(0x7F<xp && xp<=0x90
34、) mm=4; if(0x67<xp && xp<=0x7F) mm=5;if(0x50<xp && xp<=0x67) mm=6; if(0x3F<xp && xp<=0x50) mm=7;if(0x0F<xp && xp<=0x3F) mm=8; if(0x0C<xp && xp<=0x0F) mm=9; if(0x08<xp && xp<=0x0C) mm=10; if(0x00<xp && xp&l
35、t;=0x08) mm=11;/*避崖程序*/ void biya(void ) if(!(*P_IOB_Data & 0x8000) /q1 1000 stop(); backward(20,20); turn_left(21); if(!(*P_IOB_Data & 0x4000) /q2 0100 stop(); backward(20,20); turn_left(21); if(!(*P_IOB_Data & 0xc000) /q12 1100 stop(); backward(20,20); turn_left(21); if(!(*P_IOB_Data &
36、amp; 0x2000) /h1 0010 stop(); forward(15,15); turn_left(10); if(!(*P_IOB_Data & 0x1000) /h2 0001 stop(); forward(15,15); turn_left(10); if(!(*P_IOB_Data & 0x3000) /h1 2 0011 stop(); forward(15,15); turn_left(10); else; /*避障程序*/ void bizhan(void ) if(Get_IOA_Data(12)=0 && Get_IOA_Data
37、(11) =1 ) /zuobingbackward(15,15);while(Get_IOA_Data(12)=0) turn_right(30); forward(20,20); turn_left(21); if(Get_IOA_Data(11)=0 && Get_IOA_Data(12) =1 ) /youbingbackward(15,15);while(Get_IOA_Data(11)=0) turn_left(21); forward(20,20); turn_right(30); if(Get_IOA_Data(11)=0 && Get_IOA_
38、Data(12) =0) backward(15,15); turn_left(21); forward(20,20); turn_right(30); /*延时子程序*/ void delay_s(unsigned int t ) / s级延时程序 unsigned int i; for(i=0;i<t;i+) delay_ms(1000); void delay_ms(unsigned int t) / ms级延时程序 Delaynumber=0; while(Delaynumber<=10*t)*P_Watchdog_Clear=0x01; /没有延时完则继续等待 附录2 F
39、IQ.asm.DEFINE P_INT_Ctrl 0x7010 / Control port for interrupt source.DEFINE P_INT_Clear 0x7011 / Clear interrupt source.DEFINE C_FIQ_TMB 0x0800 / Timer B FIQ.DEFINE C_FIQ_TMA 0x2000 / Timer A FIQ.DEFINE C_FIQ_PWM 0x8000 / PWM FIQ.external _left,_right,_Delaynumber.PUBLIC _IRQ0,_IRQ1,_IRQ2,_IRQ3,_IRQ6
40、,_IRQ7,_BREAK.TEXT.PUBLIC _FIQ_FIQ:push r1,r5 to spr1 = C_FIQ_TMAtest r1,P_INT_Ctrljnz L_FIQ_TimerAr1 = C_FIQ_TMBtest r1,P_INT_Ctrljnz L_FIQ_TimerBL_FIQ_PWM:r1 = C_FIQ_PWMP_INT_Clear = r1pop r1,r5 from spretiL_FIQ_TimerA:P_INT_Clear = r1pop r1,r5 from spretiL_FIQ_TimerB:P_INT_Clear = r1 r2=_left r2
41、+=1 _left=r2 r3=_right r3 +=1 _right=r3 r4=_Delaynumber r4 +=1 _Delaynumber=r4 pop r1,r5 from spreti_IRQ0:reti_IRQ1: reti_IRQ2: reti_IRQ3: reti_IRQ6: reti_IRQ7: reti_BREAK: reti附录3 wei.h#defineP_IOA_Data(volatile unsigned int *)0x7000#defineP_IOB_Data(volatile unsigned int *)0x7005#ifndef_WEI_h_#def
42、ine_WEI_h_/write your header here#define bit0 0x0001#define bit1 0x0002#define bit2 0x0004#define bit3 0x0008#define bit4 0x0010#define bit5 0x0020#define bit6 0x0040#define bit7 0x0080#define bit8 0x0100#define bit9 0x0200#define bit10 0x0400#define bit11 0x0800#define bit12 0x1000#define bit13 0x2
43、000#define bit14 0x4000#define bit15 0x8000/*单独设置I/O的Data信息*voidSet_IOA_Data(unsigned int x,unsigned int y)switch(x)case 0:if(y=1)*P_IOA_Data|=0x0001;if(y=0)*P_IOA_Data&=0xfffe;break;case 1:if(y=1)*P_IOA_Data|=0x0002;if(y=0)*P_IOA_Data&=0xfffd;break;case 2: if(y=1)*P_IOA_Data|=0x0004; if(y=0)*P_IOA_Data&=0xfffb; break;case 3: if(y=1)*P_IOA_Data|=0x0008; if(y=0)*P_IOA_Data&=0xfff7; break;case 4:if(y=1)*P_IOA_Data|=0x0010;if(y=0)*P_IOA_Data&=0xffef;break;case 5:if(y=1)*P_IOA_Data|=0x0020;if(y=0)*P_IOA_Data&=0xffdf;break;case 6:if(y=1)*P_IOA_Data|=0
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- JJF(陕) 019-2019 混凝土氯离子电通量测定仪校准规范
- 《让安全伴你我同行》课件
- 增强市场竞争力的行动计划
- 研究员工激励机制效果计划
- 专业发展与教研活动的关系计划
- 精细化管理在仓库中的体现计划
- 消防安全责任落实机制培训
- 小班情景剧表演项目的设计计划
- 家用美容、保健电器具相关项目投资计划书范本
- 《销售管理培训》课件
- 山东省青岛市2023年七年级上学期期末数学试题附答案
- 物品移交接收单(模板)
- 第二单元 地球上的大气(二)大气受热过程和大气运动(课时作业)高一地理上学期 大单元高效课堂 (人教2019必修第一册)
- 工厂动力设施外包方案范本
- 2023-2024学年广东省中山一中物理高二上期末统考试题含解析
- 2022年湖南师范大学心理学专业《普通心理学》期末试卷A(有答案)
- 物业纠纷沟通应对实务与法律依据
- 【物理】2023-2024学年高一上人教版(2019)必修第一册 相互作用-力(单元解读课件)
- 班级活动安排表秋季学期德育主题教育活动安排表
- 内蒙12J9-1 室外工程建筑标准图集
- 电源线测试报告
评论
0/150
提交评论