基于GSM和GPS的汽车防盗及定位系统的主程序_第1页
基于GSM和GPS的汽车防盗及定位系统的主程序_第2页
基于GSM和GPS的汽车防盗及定位系统的主程序_第3页
基于GSM和GPS的汽车防盗及定位系统的主程序_第4页
基于GSM和GPS的汽车防盗及定位系统的主程序_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、基于GSM和GPS的汽车防盗及定位系统的主程序/* 项目内容:本项目是把单片机STC89C52、GSM技术和GPS技术有机结合一起,制作成为一个同时具有防盗和定位的系统。通过GSM通信网络的数据和语音传输及GPS卫星定位系统的定位,车主可以随时随地用手机对汽车的进行监控和监听及了解到车所在的地理位置。同时,其家里人也可以通过手机对GSM进行发短信的形式了解其亲人的具体位置及情况。项目编写人:广东轻工职业技术学院电子通信工程系 钟祥茂&&李飞龙项目完成时间:2008年08月25日*/#include"fandao_GSM_head.c"#include&quo

2、t;AT24C02.c"#include"youhua.c"void main()init(); /串口初始化longdelay(4000); /延时30秒 ,等待GSM通信正常Gsm_init();/初始化 GSM模块(选择模式,读取号码)qidong=0; /GSM主机启动正常灯cuti=1; /复位键youmen=1; /打通油路(灯灭)YaoKong();/对遥控器进行初始化while(1)if(cuti=0)/如按下复位电路,测重新把原电话号和密码写入cuti=1;longdelay(2);if(cuti=0)cuti=1;fuwei();if(zhen

3、dong=1) /如果振动标志位为1,即表示有人进入车。切断油路并报警zhendong=0;jinche();if(RING_n=1) /有电话打入则接听RING_n=0; send(send_ATA);send_cr();if(receive_CMTI_flag=1) /接收到新信息提示。receive_CMTI_flag=0;mode=3;read_delete_number();/读取信息、删除信息、判断号码if(same_number=1) /是监控手机号码发来的信息samenumber(); /启动、关闭、“123456”、“换密码 /123456*1234

4、56”接收到“请问现在在哪里?”receiveleibue1();/启动、关闭、else /不是监控手机发来的信息nosamenumber();receiveleibue2(); void serial () interrupt 4 using 3 /串口中断uchar receive_SBUF; /接收串口数据uchar i,j;ES=0;if(RI=1)RI=0;receive_SBUF=SBUF;switch(mode)case 3:receive_CMGRCMGR=receive_SBUF;CMGR+;if(CMGR>168)mode=0;CMGR=0;CMGR_TEXT_fla

5、g=1;if(CMGR>5)if(receive_CMGRCMGR-1='0'&&receive_CMGRCMGR-2= 0x0a&&receive_CMGRCMGR-3=0x0d)CMGR_TEXT_start=CMGR-1;/信息内容在receive_CMGR数 组中的位置始点。单是CMGR-1则为08所有信息开始 /*-接收是否结否以O K 0d 0a为结束*/if(receive_CMGRCMGR-1=0x0a&&receive_CMGRCMGR-2= 0x0d&&receive_CMGRCMGR-3

6、='K'& &receive_CMGRCMGR-4='O')i=0;j=CMGR_TEXT_start+58;CMGR_TEXT_number=0;while(receive_CMGRj!=0x0d) /把信息内容从receive_CMGR数组中取出存在receive_text数组中CMGR_TEXT_number+; /信息长度receive_texti=receive_CMGRj;i+;j+;for(i=0,j=CMGR_TEXT_start+26;i<12;i+,j+) /提取发送信息的手机号码receive_numberi=rece

7、ive_CMGRj;mode=0;CMGR=0;CMGR_TEXT_flag=1; /信息读取完成标志位break;default:receive_CMGRCMGR=receive_SBUF;CMGR+;if(receive_CMGRCMGR-1=0x0a && CMGR!=2)if(CMGR-1)=7&&receive_CMGR2='R'&& receive_CMGR3='I' && receive_CMGR4='N' &&receive_CMGR5='G&

8、#39;)RING_n=1; /有电话打入标志位if(CMGR-1)>13) if(receive_CMGR2='+'&&receive_CMGR3='C'&& receive_CMGR4='M'&&receive_CMGR5='T'&& receive_CMGR6='I')i=0;j=14;CMTI_number=0;while(receive_CMGRj!=0x0d)/读取+CMTI后短信的保存位置CMTI_number+;receive_C

9、MTIi=receive_CMGRj;i+;j+;receive_CMTI_flag=1; /接收到新信息标志位CMGR=0;break;ES=1;void INT_0() interrupt 0 using 3 /外部中断0,用P3.2脚 ,用于检测振动中断uchar i;EX0=0;if(start_kong=0) /按下遥控的启动按钮EX1=1; /启动外部中断1,就是启动振动监控系统youmen=1; /油路正常(用第4盏灯点灭表示)start_kong=1;for(i=0;i<3;i+)yaokong1=0;/遥控器接收灯:按启动闪3次,按关闭闪2次yaokong2=0;yao

10、kong3=0;longdelay(60);yaokong1=1;/遥控器接收灯:按启动闪3次,按关闭闪2次yaokong2=1;yaokong3=1;longdelay(60);jiankong=0; /点亮启动监控灯if(stop_kong=0) /按下遥控的关闭按钮EX1=0; /关闭外部中断1,就是关闭振动监控系统youmen=1; /油路正常(用第4盏灯点灭表示)stop_kong=1;for(i=0;i<2;i+)yaokong1=0;/遥控器接收灯:按启动闪3次,按关闭闪2次yaokong2=0;yaokong3=0;longdelay(60);yaokong1=1;/遥控

11、器接收灯:按启动闪3次,按关闭闪2次yaokong2=1;yaokong3=1;longdelay(60);jiankong=1;/关闭启动监控灯EX0=1;void INT_2() interrupt 2 using 3 /外部中断1,用P3.3脚,用于振动中断EX1=0;zhendong=1;基于GSM和GPS的汽车防盗及定位系统的子程序头文件fandao_GSM_head.c#include<reg52.h>#define uchar unsigned char#define uint unsigned int/*对IO口进行定义功能*/sbit qidong=P06; /G

12、SM主机启动正常灯sbit jiankong=P05;/启动监控灯sbit get_GPS_deng=P00;/准各向GPS发送低电平sbit youmen=P04;/有人进车。关关总油门sbit yaokong3=P03;/遥控器接收灯:按启动闪3次,按关闭闪2次sbit yaokong2=P02;/sbit yaokong1=P01;/sbit start_kong=P10; /中断启动sbit stop_kong=P14; /中断关闭sbit put_get=P36;/向GPS输低电平并检测有没收到低电平返还sbit cuti=P20; /复位按钮,按下即从新初此化串口.GSM.恢复原此

13、手机号密码123456bit zhendong=0; /有外部振动中断标志位/*串口中断中用到的变量数组和变量*/uchar receive_CMTI5;uchar receive_CMGR170;/发送读取信息指令,返回CMGR信息,包函信息内容uchar CMGR=0;uchar CMGR_TEXT_start;/接收信息内容的起始位uchar CMGR_TEXT_number;/信息长度4*多少个字uchar idata receive_text80;/存储信息内容uchar receive_number12; /接到到信息的手机号码bit CMGR_TEXT_

14、flag=0; /信息接收完成标志位uchar receive_CMTI_flag=0; /接收到新信息标志位。=1时有新信息uchar CMTI_number=0; /新信息存储在的位置是几多位(个,十,百)uchar RING_n=0; /接收到3次RINGuchar mode =0; /模式标志位,=1时,接OK;=2时接+CMGS;=3时接+CMGR号,其它接RING或新信息提示(+CMTI)bit same_number=0; /判断接到的的信息号码是不是与原相同,全相同则置1uchar receive_leibue=0; /接收到的信息标志位,=0,不是内容不符;=1,内容为“启动

15、”=2,内容为“关闭”=3,内容为“123456”=4,内容为“123456”=5,内容为:请问现在在哪里?或(请问现在在哪里?123456)/*对GPS和GSM操作中用到变量及数组*/uchar idata time6; /储存GPS接收到的时间uchar wait_60S;/初此化等待1分钟uchar count;bit wait_flag=0;/初此化等待标志位,=1时向下执行uchar code send_AT_CMGF="AT+CMGF=0" /选择PDU模式uchar code send_AT_CSMP=&qu

16、ot;AT+CSMP=17,167,0,8" /没呢句会造成出现代码而不是文字uchar code send_AT_CNMI="AT+CNMI=2,1,0,0,1" /有信息到来会有提示uchar code send_AT_CMGS="AT+CMGS="uchar code send_ATA="ATA" /收到RING,则接听电话uchar code send_AT_CMGR="AT+CMGR="uchar code send_AT_CMGD="AT+CMGD="uchar code

17、send_text_head1="0891683108707505F011000D9168" /发送信息的头文件1+手机号+头文件2()实际长度从11开始算即15+内容/2uchar code send_text_head2="0008A9" /后有两位数是信息长度。字*2移到信息前面了uchar codesend_text1="1860A8597DFF0C67094EBA52A88F66FF0C8BF796328303FF01" /信息内容“您好,有人动车,请防范!” 24+15=39uchar code send_text2=&q

18、uot;08542F52A86210529F" /信息内容“启动成功”8 + 15= 23uchar code send_text3="08517395ED6210529F" /信息内容 “关闭成功”8 + 15= 23uchar code send_text4="0A636253F778016210529F" /信息内容“换号码成功”10 + 15= 25uchar code send_text5="0A63625BC678016210529F" /信息内容“换密码成功”10 + 15= 25uchar code rece

19、ive_text1="542F52A8" /信息对比“启动”/信息内容“启动成功”8 + 15= 23uchar code receive_text2="517395ED" /信息对比“关闭”/信息内容 “关闭成功”8 + 15= 23uchar code receive_text3="636253F77801" /信息对比“换号码”/信息内容“换号码成功”10 + 15= 25uchar code receive_text4="63625BC67801" /信息对比“换密码”/“换密码成功”10 + 15= 25

20、uchar code receive_find="8BF795EE73B05728572854EA91CCFF1F"/请问现在在哪里?8*4=32+6*4=56/*-AT24C02的变量-*/sbit sda=P35; /AT24C02总线接口sbit scl=P34; uchar code callnumber11=0x31,0x35,0x39,0x31,0x33,0x31,0x30,0x35,0x37,0x35,0x39;uchar readnumber12; /从AT24C02读出来的号码,循环利用储存“东经度10个字符”uchar yuwei_call12; /从A

21、T24C02读出来并经移位后的号码uchar old_yuwei_call12;/上一次从AT24C02读出来并经移位后的号码,方便原手机换号码后“更换号码成功”发到原手机,循环利用储存“北纬度9个字符”uchar code password=0x31,0x32,0x33,0x34,0x35,0x36;uchar read_password6;子程序包youhua.c#include<string.h>void init()/初此化定时器与串口TMOD=0x20; /用定时器1的方式2,定时器0TL1=0xfd; /控制波特率是9600TH1=0xfd;SCON=0x50; /串口

22、工作在方式1并且接收REN位允许,发送与接收的第9位没有.发送与接收的标志位清0PCON=0x00;TR1=1;/定时器开始计数ES=1; /开启串口中断EA=1; /开启总中断EX0=1; /开启外部中断0,下降没有效IT1=0; /外部中断1,低电平有效void delay(void) /延时0.196356Sunsigned char y,z;for(y=0;y<0xff;y+)for(z=0;z<0xff;z+);/*-*/void send_cr()delay();SBUF=0x0d;while(TI=0);TI=0;/*-*/void send( uchar send_

23、AT_text)uchar b,n;n=strlen(send_AT_text);for(b=0;b<n;b+)delay();SBUF=send_AT_textb;while(TI=0);TI=0;void send_read(uchar read_number)delay();SBUF=read_number; while(TI=0);TI=0;/*-*/发送GPS定位所用函数/void send_GPS_head()/信息实际长度send_read(0x34);send_read(0x36);void send_GPS_003()/数字前信息003send_read(0x30);s

24、end_read(0x30);send_read(0x33);void send_GPS_miaohao()/冒号:send_read('F');send_read('F');send_read(0x31);send_read('A');void send_GPS_fenhao()/分号;send_read('F');send_read('F');send_read(0x31);send_read('B');void send_GPS_du()/du号send_read(0x35);send_rea

25、d('E');send_read('A');send_read(0x36);void send_GPS_miao()/miao号send_read(0x35);send_read(0x32);send_read(0x30);send_read(0x36);void send_GPS_xiaodan()/小数点.send_read(0x30);send_read(0x30);send_read(0x32);send_read('E');void send_GPS_table(uchar temp)/发送GPS定位数字编码后的数字switch(tem

26、p)case '0':send_read(0x30);break;case '1':send_read(0x31);break;case '2':send_read(0x32);break;case '3':send_read(0x33);break;case '4':send_read(0x34);break;case '5':send_read(0x35);break;case '6':send_read(0x36);break;case '7':send_rea

27、d(0x37);break;case '8':send_read(0x38);break;case '9':send_read(0x39);break;default:send_read('N');break;YaoKong()/对遥控器进行初始化start_kong=1; /遥控的启动stop_kong=1; /遥控的关闭jiankong=1; /关闭动监控灯yaokong1=1; /遥控器接收灯:按启动闪3次,按关闭闪2次yaokong2=1; /遥控器接收灯:按启动闪3次,按关闭闪2次yaokong3=1; /遥控器接收灯:按启动闪3次,按

28、关闭闪2次Gsm_init() /初始化 GSM模块(选择模式,读取号码)send(send_AT_CMGF);/选择PDU模式send_cr();send(send_AT_CSMP);/AT+CSMP=17,167,0,8send_cr();send(send_AT_CNMI);/AT+CNMI=2,1,0,0,1有信息到来会有提示send_cr();write_nbyte(55,password,6); /尝试,没实际意义longdelay(2);read_nbyte(1,readnumber,11); /读取电话号码longdelay(2);read_nbyte(15,read_pass

29、word,6);/读取密码yuwei(readnumber,yuwei_call); /对手机号码进行移位fuwei()youmen=1;/打开油门(灯灭)qidong=1;/GSM主机启动正常灯longdelay(60);qidong=0;longdelay(60);qidong=1;init();send(send_AT_CMGF);/选择PDU模式send_cr();send(send_AT_CSMP);/AT+CSMP=17,167,0,8send_cr();send(send_AT_CNMI);/AT+CNMI=2,1,0,0,1有信息到来会有提示send_cr();write_nb

30、yte(1,callnumber,11); / 写手机号码longdelay(2);write_nbyte(15,password,6); / 写系统密码longdelay(2);read_nbyte(1,readnumber,11); /读手机号码longdelay(2);read_nbyte(15,read_password,6);/读系统密码yuwei(readnumber,yuwei_call);start_kong=1;stop_kong=1;jiankong=1;/启动监控灯yaokong1=1;/遥控器接收灯:按启动闪3次,按关闭闪2次yaokong2=1;/遥控器接收灯:按启动

31、闪3次,按关闭闪2次yaokong3=1;/遥控器接收灯:按启动闪3次,按关闭闪2次qidong=0; /GSM主机启动正常灯jinche()uchar i;EX1=0;jiankong=1;/关闭启动监控灯youmen=0;/切断油路(用第4盏灯点亮表示)send(send_AT_CMGS);send_read(0x33);send_read(0x39);send_cr();send(send_text_head1);for(i=0;i<12;i+)send_read(yuwei_calli);send(send_text_head2);send(send_text1);delay()

32、;SBUF=0x1A; /发送Ctrl+Z结束符while(TI=0);TI=0;read_delete_number()uchar i;send(send_AT_CMGR);for(i=0;i<CMTI_number;i+) /接收到信息存储在何位置send_read(receive_CMTIi);send_cr();while(CMGR_TEXT_flag=0);CMGR_TEXT_flag=0;/*-删除卡上接收到的信息-*/send(send_AT_CMGD);for(i=0;i<CMTI_number;i+) /接收到信息存储在何位置send_read(receive_C

33、MTIi);send_cr();/for(i=0;i<12;i+) /判断接收到信息电话号码与储存号码是不是一样。if (receive_numberi!=yuwei_calli) /不相同,置0并跳出same_number=0;break;same_number=1;receiveleibue1()uchar i,j;switch(receive_leibue)case 1: /“启动”EX1=1;/开启振动中断,即可启动监控send(send_AT_CMGS);send_read(0x32);send_read(0x33);send_cr();send(send_text_head1

34、);for(i=0;i<12;i+)send_read(yuwei_calli);send(send_text_head2);send(send_text2);delay();SBUF=0x1A; /发送Ctrl+Z结束符while(TI=0);TI=0;youmen=1;/打开油门(灭灯)jiankong=0;/点亮启动监控灯break;case 2: /“关闭”EX1=0;/关闭振动中断,即可停止监控send(send_AT_CMGS);send_read(0x32);send_read(0x33);send_cr();send(send_text_head1);for(i=0;i&

35、lt;12;i+)send_read(yuwei_calli);send(send_text_head2);send(send_text3);delay();SBUF=0x1A; /发送Ctrl+Z结束符while(TI=0);TI=0;youmen=1;/开启总油门(用第4盏灯点灭表示)jiankong=1;/灭启动监控灯break;case 3:/“123456”for(i=0;i<12;i+) /把原来号码保存,以便发本次提示信息old_yuwei_calli=yuwei_calli;for(i=0,j=4;i<11;i+)/读取新号码存到readn

36、umber中,以便保存到AT24C02readnumberi= receive_textj+3;j=j+4;write_nbyte(1,readnumber,11);longdelay(2); read_nbyte(1,readnumber,11);yuwei(readnumber,yuwei_call);send(send_AT_CMGS);send_read(0x32);send_read(0x35);send_cr();send(send_text_head1);for(i=0;i<12;i+)send_read(old_yuwei_calli);send(send_text_he

37、ad2);send(send_text4);delay();SBUF=0x1A; /发送Ctrl+Z结束符while(TI=0);TI=0;break;case 4:/“换密码654321*123456”for(i=0,j=12;i<6;i+)/保存新密码read_passwordi= receive_textj+3;j=j+4;write_nbyte(15,read_password,6);longdelay(5);read_nbyte(15,read_password,6);send(send_AT_CMGS);send_read(0x32);send_read(0x35);send

38、_cr();send(send_text_head1);for(i=0;i<12;i+)send_read(yuwei_calli);send(send_text_head2);send(send_text5);delay();SBUF=0x1A; /发送Ctrl+Z结束符while(TI=0);TI=0;break;case 5: /接收到“请问现在在哪里?”sda=1;scl=1;longdelay(2);get_GPS_deng=0; /点亮读取GPS的灯put_get=0; /向GPS从机发送低电平信号flash();put_get=1;while(put_get=1); /等待

39、GPS从机返还低电平信号put_get=1;/发送定位内容/read_nbyte(24,time,6);/读取时间longdelay(2);read_nbyte(30,old_yuwei_call,10); /读取北纬度longdelay(2);read_nbyte(40,readnumber,11);/读取东经度send(send_AT_CMGS);send_read(0x38); /信自长度为70+15=85 (88+15=103)send_read(0x35);send_cr();send(send_text_head1);for(i=0;i<12;i+) /发送手机号码send_

40、read(yuwei_calli);send(send_text_head2);/发送定位内容/send_GPS_head();/定位信息实际长度/发送时间 send_GPS_003();send_GPS_table(time0);send_GPS_003();send_GPS_table(time1);send_GPS_miaohao();send_GPS_003();send_GPS_table(time2);send_GPS_003();send_GPS_table(time3);send_GPS_miaohao();send_GPS_003();send_GPS_table(time4

41、);send_GPS_003();send_GPS_table(time5);send_GPS_fenhao();/发送北纬度send_GPS_003();send_GPS_table(old_yuwei_call0);send_GPS_003();send_GPS_table(old_yuwei_call1);send_GPS_du();send_GPS_003();send_GPS_table(old_yuwei_call2);send_GPS_003();send_GPS_table(old_yuwei_call3);send_GPS_xiaodan();send_GPS_003();s

42、end_GPS_table(old_yuwei_call5);send_GPS_003();send_GPS_table(old_yuwei_call6);send_GPS_003();send_GPS_table(old_yuwei_call7);send_GPS_003();send_GPS_table(old_yuwei_call8);send_GPS_003();send_GPS_table(old_yuwei_call9);send_GPS_miao();send_GPS_fenhao();/发送东经度send_GPS_003();send_GPS_table(readnumber0

43、);send_GPS_003();send_GPS_table(readnumber1);send_GPS_003();send_GPS_table(readnumber2);send_GPS_du();send_GPS_003();send_GPS_table(readnumber3);send_GPS_003();send_GPS_table(readnumber4);send_GPS_xiaodan();send_GPS_003();send_GPS_table(readnumber6);send_GPS_003();send_GPS_table(readnumber7);send_GP

44、S_003();send_GPS_table(readnumber8);send_GPS_003();send_GPS_table(readnumber9);send_GPS_003();send_GPS_table(readnumber10);send_GPS_miao();delay();SBUF=0x1A; /发送Ctrl+Z结束符while(TI=0);TI=0;get_GPS_deng=1;/从新读取号码和密码read_nbyte(1,readnumber,11); /读取电话号码longdelay(2);read_nbyte(15,read_password,6);/读取密码yuwei(readnumber,yuwei_call);break;default:break;receiveleibue2()uchar i,j;switch(receive_leibue)case 3:/“123456”for(i=0,j=4;i<11;i+) /把新号码存在readnu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论