极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15124|回复: 7

GPS—arduino—蓝牙-1602-DHT11-DS18B20-GP2Y1010测试【1楼完结】

[复制链接]
发表于 2014-10-27 22:54:33 | 显示全部楼层 |阅读模式
本帖最后由 hi55234 于 2014-11-12 22:46 编辑









  1. #include <MemoryFree.h>
  2.   
  3. #include <LiquidCrystal.h>
  4. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  5.   
  6.   
  7.   
  8. #include <SoftwareSerial.h>
  9. SoftwareSerial gps(8, 9);
  10. char tempx[17];
  11. char GPRMC[80];
  12. char GPGGA[80];
  13. int gpschangertest;
  14. boolean gpschanger;
  15. int weiduA;
  16. unsigned long weiduB,weiduC;
  17. int jingduA;
  18. unsigned long jingduB,jingduC;
  19.   
  20.   
  21. boolean GPGGAget=0;
  22. boolean GPRMCget=0;
  23. unsigned long gpstimes;
  24. boolean konghangpanduan;
  25. boolean gpsshuchuqiehuanB;
  26. int jsq1=0;
  27. int jsq2=0;
  28. int jsq3=0;
  29. int jsq4=0;
  30. int jsq5=0;
  31. int jsq6=0;
  32. int jsq7=0;
  33. int GPGGAlong=0;
  34. int GPRMClong=0;
  35. boolean jiaoyanjieguoA,jiaoyanjieguoC;
  36. float sudu;
  37.   
  38. String jianyan="";
  39. int yihuoyunsuan;
  40.   
  41.   
  42.   
  43. boolean dingweiok=0;
  44.   
  45. boolean gpsxianguangA=0;
  46. boolean fangzhilianji=1;
  47.   
  48. int lcdxianshi=0;
  49.   
  50. /*---------------------GPS end---------------------------------*/
  51. int gpsshuchuqiehuanA;
  52.   
  53.   
  54.   
  55. #include<EEPROM.h>
  56. int EEPROMvalue;
  57.   
  58.   
  59.   
  60.   
  61.   
  62. const int chipSelect = 10;
  63.   
  64.   
  65.   
  66.   
  67.   
  68.   
  69. char TrackB0[8]="-0000.0";
  70.   
  71. char TrackB1[11]="00.0000000";
  72. char TrackB2[12]="000.0000000";
  73.   
  74. char TrackB4[20]="2000-00-00T00:00:00";
  75.   
  76. char TrackB5[8]="0000.00";
  77. char TrackB6[7]="000.00";
  78.   
  79.   
  80. boolean weiduxuweitiao=0;
  81. boolean jingduxuweitiao=0;
  82. int weiduzhanwei,jingduzhanwei;
  83.   
  84.   
  85. int utc8s,utc8f,utc8m,utc8n,utc8y,utc8r;
  86.   
  87. int xiaoyue[5]={4,6,9,11};
  88.   
  89. boolean rijinwei,yuejinwei,nianjinwei,xiaoyueok;
  90.   
  91. unsigned long fentodu;
  92.   
  93.   
  94.   
  95.   
  96.   
  97. #include <OneWire.h>
  98. #include <DallasTemperature.h>
  99. OneWire oneWire(10);
  100. DallasTemperature sensors(&oneWire);
  101.   
  102.   
  103.   
  104. #include <dht11.h>
  105. dht11 DHT11;
  106. #define DHT11PIN 11
  107.   
  108.   
  109.   
  110.   
  111. #include <Wire.h>
  112. #include <RTClib.h>
  113. void printDateTime(DateTime dateTime);
  114. RTC_DS1307 RTC;
  115. int nian,yue,ri,shi,fen,miao;
  116. boolean shoushiqidong;
  117.   
  118.   
  119.   
  120.   
  121.   
  122. int gp2y1010dust=2;
  123. int gp2y1010led=15;
  124. int dustVal=0;
  125. int delayTime=280;
  126. int delayTime2=40;
  127. float offTime=9680;
  128.   
  129. int dusti=0;
  130.   
  131. float dustValPM25,averagedustVal;
  132. int AQI;
  133.   
  134. int mySensVals [10];
  135.   
  136. unsigned long time1=millis();
  137.   
  138.   
  139.   
  140. void setup() {
  141.   
  142.   
  143. pinMode(gp2y1010led,OUTPUT);
  144.   
  145.   
  146.   
  147.   
  148.   
  149. lcd.begin(16, 2);
  150.   
  151.   
  152.   Wire.begin();
  153.   RTC.begin();
  154.   
  155. pinMode(12,INPUT);
  156. pinMode(14,INPUT);
  157.   
  158.   
  159.   
  160. Serial.begin(9600);
  161. gps.begin(9600);
  162.   
  163.   
  164. EEPROMvalue= EEPROM.read(0);
  165. if(EEPROMvalue==0)gpsshuchuqiehuanB=0;
  166. else gpsshuchuqiehuanB=1;
  167.   
  168. EEPROMvalue= EEPROM.read(10);
  169. if(EEPROMvalue==0)lcdxianshi=0;
  170. else if(EEPROMvalue==1)lcdxianshi=1;
  171. else if(EEPROMvalue==2)lcdxianshi=2;
  172. else if(EEPROMvalue==3)lcdxianshi=3;
  173. else {
  174.   EEPROM.write(10,0);
  175.   lcdxianshi=0;
  176. }
  177.   
  178.   
  179. }
  180.   
  181.   
  182. void loop() {
  183.   
  184.   
  185.   
  186. if(lcdxianshi<3)ruanchuankou();
  187. else{
  188.   
  189.   
  190. fenchentou();
  191.   
  192.   
  193.   
  194.    sensors.requestTemperatures();
  195.   
  196.    jsq1=sensors.getTempCByIndex(0);
  197.   
  198.       DHT11.read(DHT11PIN);
  199.   
  200.    jsq2=DHT11.humidity;
  201.   
  202.   
  203.     for(int col=0;col<16;col++)tempx[col]=0;
  204.   
  205. DS1307tiquxiaodui();   
  206.   
  207.   
  208.   
  209.   
  210.   
  211.     jsq3=jsq2*0.1;
  212.   tempx[0] = jsq3+48;
  213.   jsq3=jsq2%10;
  214.   tempx[1] = jsq3+48;
  215.   
  216. tempx[2]='%';
  217. tempx[3]=32;
  218.   
  219.   
  220.   jsq3=jsq1*0.1;
  221.   tempx[4] = jsq3+48;
  222.   jsq3=jsq1%10;
  223.   tempx[5] = jsq3+48;
  224.   tempx[6]='C';
  225.   tempx[7]=32;
  226.   
  227. }
  228.   
  229.   
  230.   
  231. if(millis()-gpstimes>10 && !konghangpanduan){
  232. konghangpanduan=1;
  233. gpsxianguangA=1;
  234.   
  235.   
  236.   
  237. }
  238.   
  239.   
  240.   
  241. if(gpsxianguangA && lcdxianshi<3){
  242.   
  243. gpsxianguangA=0;
  244.   
  245.   
  246. gpsdechuli();
  247.   
  248.   
  249. if(!gpsshuchuqiehuanB){
  250. Serial.println(GPGGA);
  251. Serial.println(GPRMC);
  252. Serial.print("freeMemory=");
  253. Serial.println(freeMemory());
  254. Serial.println();
  255. }
  256.   
  257. if(jiaoyanjieguoA)jiexiGPGGA();
  258.   
  259. if(jiaoyanjieguoC){
  260. jiexiGPRMC();
  261.   
  262. if(dingweiok && gpschanger) {
  263. gpschanger=0;
  264.   
  265.   
  266. }
  267.   
  268. }
  269.   
  270.   
  271.   now1602out();
  272.   
  273.   
  274.   
  275. for(int col=0;col<80;col++)GPRMC[col]=0;
  276. for(int col=0;col<80;col++)GPGGA[col]=0;
  277. for(int col=0;col<16;col++)tempx[col]=0;
  278. jsq1=0;
  279. GPGGAget=0;
  280. GPRMCget=0;
  281.   
  282. gpstimes=millis();
  283. }
  284.   
  285.   
  286.   
  287.   
  288.   
  289.   
  290.   
  291.   
  292.   
  293.   gpsshuchuqiehuanA =digitalRead(12);
  294. if (gpsshuchuqiehuanA==HIGH && fangzhilianji){
  295.   
  296. fangzhilianji=0;
  297. gpstimes=millis();
  298. gpsshuchuqiehuanB=!gpsshuchuqiehuanB;
  299. EEPROM.write(0,gpsshuchuqiehuanB);
  300.   
  301. }else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();
  302.   
  303.   
  304.   gpsshuchuqiehuanA =digitalRead(14);
  305. if (gpsshuchuqiehuanA==HIGH && fangzhilianji){
  306.   
  307.   fangzhilianji=0;
  308. gpstimes=millis();
  309.   
  310.   
  311. if(!dingweiok){
  312.   
  313. if(lcdxianshi<3)lcdxianshi=3;
  314. else lcdxianshi=0;
  315.   
  316. }else lcdxianshi++;
  317.   
  318. lcdxianshi=lcdxianshi%4;
  319. EEPROM.write(10,lcdxianshi);
  320.   
  321.   
  322. }else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();
  323.   
  324. if(millis()-gpstimes>450 && !fangzhilianji){
  325. gpstimes=millis();
  326. fangzhilianji=1;
  327. }
  328.   
  329.   
  330.   
  331.   
  332. if(lcdxianshi==3) now1602out();
  333.   
  334.   
  335.   
  336.   
  337.   
  338.   
  339.   
  340.   
  341. }
  342.   
  343.   
  344.   
  345.   
  346.   
  347. void fenchentou()
  348. {
  349.   
  350.   
  351. if (millis() - time1 > 1000){
  352. time1=millis();
  353.   
  354. /*粉尘头控制代码*/
  355. digitalWrite(gp2y1010led,LOW);
  356. delayMicroseconds(delayTime);
  357. dustVal=analogRead(gp2y1010dust);
  358. delayMicroseconds(delayTime2);
  359. digitalWrite(gp2y1010led,HIGH);
  360.   
  361. /*邻采样10次平均averagedustVal的计算*/
  362. mySensVals [dusti]=dustVal;
  363. dusti++;
  364. dusti=dusti%10;
  365.   
  366. averagedustVal=0;
  367. for(int col=0;col<10;col++)averagedustVal=averagedustVal+mySensVals[col];
  368. averagedustVal=averagedustVal*0.1;
  369.   
  370.   
  371.   
  372. /*计算pm2.5的数值 电压阀值以上用:(v*0.172-0.0999)*1000,阀值以下用线性,参考国外帖子,认为输出最低17,对应粉尘5000*/
  373.   
  374.   
  375.   
  376.   
  377.   
  378.   
  379. if(averagedustVal>184.5)dustValPM25=0.83984375*averagedustVal-99.9;
  380. else dustValPM25=averagedustVal*0.298373984;
  381.   
  382.   
  383. /*AQI的计算,美国标准*/
  384.   
  385. if(dustValPM25<15.4) AQI=(50-0)/(15.4-0)*(dustValPM25-0)+0;
  386. else if(dustValPM25<40.4) AQI=(100-51)/(40.4-15.5)*(dustValPM25-15.5)+51;
  387. else if(dustValPM25<65.4) AQI=(150-101)/(65.4-40.5)*(dustValPM25-40.5)+101;
  388. else if(dustValPM25<150.4) AQI=(200-151)/(150.4-65.5)*(dustValPM25-65.5)+151;
  389. else if(dustValPM25<250.4) AQI=(300-201)/(250.4-150.5)*(dustValPM25-150.5)+201;
  390. else if(dustValPM25<350.4) AQI=(400-301)/(350.4-250.5)*(dustValPM25-250.5)+301;
  391. else AQI=(500-401)/(500.4-350.5)*(dustValPM25-350.5)+401;
  392.   
  393.   
  394. lcd.setCursor(0, 0);
  395. lcd.print(dustValPM25);
  396. lcd.print("ug AQI:");
  397. lcd.print(AQI);
  398. lcd.print("     ");
  399.   
  400. }
  401.   
  402.   
  403.   
  404.   
  405. if (millis() < time1){
  406. time1=millis();
  407. }
  408.   
  409.   
  410. }
  411.   
  412.   
  413.   
  414. void ruanchuankou()
  415. {
  416.   
  417.   
  418. while (gps.available() > 0) {
  419. gpstimes=millis();
  420. konghangpanduan=0;
  421.   
  422.   
  423. if(GPRMCget){
  424. GPRMC[jsq1]=gps.read();
  425. if(gpsshuchuqiehuanB)Serial.print(GPRMC[jsq1]);
  426. if(GPRMC[jsq1-3]=='*'){
  427. GPRMClong=jsq1;
  428. GPRMCget=0;
  429. jsq1=0;
  430. }else if(jsq1<79)jsq1++;
  431.   
  432.   
  433.   
  434. }else if(GPGGAget){
  435.   
  436. GPGGA[jsq1]=gps.read();
  437. if(gpsshuchuqiehuanB)Serial.print(GPGGA[jsq1]);
  438. if(GPGGA[jsq1-3]=='*'){
  439. GPGGAlong=jsq1;
  440. GPGGAget=0;
  441. jsq1=0;
  442. }else if(jsq1<79)jsq1++;
  443.   
  444.   
  445.   
  446. }else{
  447. tempx[jsq1]=gps.read();
  448. if(gpsshuchuqiehuanB)Serial.print(tempx[jsq1]);
  449. if(jsq1<6)jsq1++;
  450.   
  451. }
  452.   
  453.   
  454.   
  455. if(jsq1==6){
  456.   
  457.   
  458. if(tempx[4]=='M' && tempx[5]=='C'){
  459.   
  460. GPRMCget=1;
  461. GPGGAget=0;
  462. for(int col=0;col<6;col++)GPRMC[col]=tempx[col];
  463. for(int col=0;col<6;col++)tempx[col]=0;
  464.   
  465. }else if(tempx[jsq1-2]=='G' && tempx[jsq1-1]=='A'){
  466.   
  467. GPGGAget=1;
  468. GPRMCget=0;
  469. for(int col=0;col<6;col++)GPGGA[col]=tempx[col];
  470. for(int col=0;col<6;col++)tempx[col]=0;
  471.   
  472. }else{
  473.   
  474. for(int col=0;col<5;col++)tempx[col]=tempx[col+1];
  475. jsq1=5;
  476. }
  477.   
  478. }
  479.   
  480.   
  481. }
  482.   
  483.   
  484.   
  485. }
  486.   
  487.   
  488. void gpsdechuli()
  489. {
  490.   
  491. for(int col=1;col<GPRMClong-3;col++){
  492. if(col==1)yihuoyunsuan=GPRMC[col];
  493. else yihuoyunsuan=yihuoyunsuan ^ GPRMC[col];
  494. }
  495.   
  496. if(yihuoyunsuan==0){
  497. jianyan="00";
  498. }else if(yihuoyunsuan>15){
  499. jianyan = String(yihuoyunsuan,HEX);
  500. }else{
  501. jianyan = "0";
  502. jianyan += String(yihuoyunsuan,HEX);
  503. }
  504. jianyan.toUpperCase();
  505. if(jianyan[0]==GPRMC[GPRMClong-2] && jianyan[1]==GPRMC[GPRMClong-1] ){
  506. jiaoyanjieguoC=1;
  507. }else{
  508. jiaoyanjieguoC=0;
  509. }
  510.   
  511. jianyan="";
  512.   
  513.   
  514.   
  515.   
  516. for(int col=1;col<GPGGAlong-3;col++){
  517. if(col==1)yihuoyunsuan=GPGGA[col];
  518. else yihuoyunsuan=yihuoyunsuan ^ GPGGA[col];
  519. }
  520.   
  521. if(yihuoyunsuan==0){
  522. jianyan="00";
  523. }else if(yihuoyunsuan>15){
  524. jianyan = String(yihuoyunsuan,HEX);
  525. }else{
  526. jianyan = "0";
  527. jianyan += String(yihuoyunsuan,HEX);
  528. }
  529. jianyan.toUpperCase();
  530. if(jianyan[0]==GPGGA[GPGGAlong-2] && jianyan[1]==GPGGA[GPGGAlong-1] ){
  531. jiaoyanjieguoA=1;
  532. }else{
  533. jiaoyanjieguoA=0;
  534. }
  535.   
  536.   
  537. jianyan="";
  538.   
  539.   
  540. }
  541.   
  542.   
  543. void jiexiGPGGA()
  544. {
  545.   
  546.    jsq2=0;
  547.    jsq3=0;
  548.    jsq4=0;
  549.    jsq5=0;
  550.   
  551.   
  552.     for(int col=1;col<GPGGAlong-3;col++){
  553.      if(GPGGA[col]==',')jsq2++;
  554.    if(jsq2==10){
  555.    jsq3=col;
  556.    col=GPGGAlong-3;
  557.    }
  558.   
  559.   
  560.    if(jsq2<9)jsq4=col;
  561.   
  562.    if(jsq2<7)jsq5=col;
  563.   
  564.    }
  565.   
  566.   
  567.    if(GPGGA[jsq5]==49)dingweiok=1;
  568.    else dingweiok=0;
  569.   
  570.   
  571. for(int col=0;col<8;col++)TrackB0[col]=0;
  572. memcpy(TrackB0,GPGGA+jsq4+2, jsq3-jsq4-2);
  573.   
  574. }
  575.   
  576.   
  577. void jiexiGPRMC()
  578. {
  579.   
  580.    jsq2=0;
  581.    jsq3=0;
  582.    jsq4=0;
  583.    jsq5=0;
  584.    jsq6=0;
  585.    jsq7=0;
  586.   
  587.     for(int col=1;col<GPRMClong-3;col++){
  588.      if(GPRMC[col]==',')jsq2++;
  589.    if(jsq2==9){
  590.    jsq3=col;
  591.    col=GPRMClong-3;
  592.    }
  593.   
  594.    if(jsq2<8)jsq4=col;
  595.    if(jsq2<6)jsq5=col;
  596.    if(jsq2<4)jsq6=col;
  597.    if(jsq2<2)jsq7=col;
  598.    }
  599.   
  600.   
  601.   if(GPRMC[jsq7+2]=='A')chinatime();
  602.   
  603.   
  604. if(dingweiok){
  605.   
  606. jingweiduchuli();
  607. hangxiangyusudu();
  608.   
  609. }else {
  610.   weiduA=0;
  611.   weiduB=0;
  612.   weiduC=0;
  613.   jingduA=0;
  614.   jingduB=0;
  615.   jingduC=0;
  616. }
  617.   
  618.   
  619. }
  620.   
  621.   
  622.   void chinatime()
  623. {
  624.   
  625.   
  626. jianyan = String(GPRMC[7]);
  627. jianyan += GPRMC[8];
  628. utc8s=jianyan.toInt()+8;
  629.   
  630.   
  631. if(utc8s>23)rijinwei=1;
  632. utc8s=utc8s%24;
  633.   
  634. jianyan = String(GPRMC[9]);
  635. jianyan += GPRMC[10];
  636. utc8f=jianyan.toInt();
  637.   
  638.   
  639.   
  640. jianyan = String(GPRMC[11]);
  641. jianyan += GPRMC[12];
  642. utc8m=jianyan.toInt();
  643.   
  644.   
  645.   
  646.   
  647.   
  648.   
  649.   
  650. jianyan = String(GPRMC[jsq3+1]);
  651. jianyan += GPRMC[jsq3+2];
  652. utc8r=jianyan.toInt();
  653.   
  654.   
  655.   
  656.   
  657. jianyan = String(GPRMC[jsq3+3]);
  658. jianyan += GPRMC[jsq3+4];
  659. utc8y=jianyan.toInt();
  660.   
  661.   
  662.   
  663. jianyan = String(GPRMC[jsq3+5]);
  664. jianyan += GPRMC[jsq3+6];
  665. utc8n=jianyan.toInt();
  666.   
  667.   
  668.   
  669.   
  670.   
  671. if(rijinwei){
  672.   
  673. if(utc8y==2 && utc8r==28){
  674. if(utc8n % 400 == 0 || utc8n % 100 != 0 && utc8n % 4 == 0)utc8r=29;
  675. else {
  676. utc8r=1;
  677. yuejinwei=1;
  678. }
  679. }else{
  680.   
  681. for(int col=0;col<4;col++){
  682. if(xiaoyue[col]==utc8y)xiaoyueok=1;
  683. }
  684. if(xiaoyueok && utc8r==30){
  685. utc8r=1;
  686. yuejinwei=1;
  687. }else if(!xiaoyueok && utc8r==31){
  688. utc8r=1;
  689. yuejinwei=1;
  690. }else{
  691. utc8r++;
  692. }
  693. }
  694. }
  695. if(yuejinwei && utc8y==12){
  696. utc8y=1;
  697. nianjinwei=1;
  698. }else if(yuejinwei){
  699. utc8y++;
  700. }
  701. if(nianjinwei)utc8n++;
  702.   
  703.   
  704.   
  705. for(int col=0;col<16;col++)tempx[col]=0;
  706. if(utc8n>9){
  707. sprintf(tempx, "%d", utc8n);
  708. memcpy(TrackB4+2,tempx, 2);
  709. }else{
  710.   TrackB4[2]=48;
  711.   TrackB4[3]=utc8n+48;
  712.   
  713. }
  714.   
  715.   for(int col=0;col<16;col++)tempx[col]=0;
  716.   if(utc8y>9){
  717.   sprintf(tempx, "%d", utc8y);
  718. memcpy(TrackB4+5,tempx, 2);
  719. } else{
  720.   TrackB4[5]=48;
  721.    TrackB4[6]=utc8y+48;
  722.   
  723. }
  724.    for(int col=0;col<16;col++)tempx[col]=0;
  725. if(utc8r>9){
  726. sprintf(tempx, "%d", utc8r);
  727. memcpy(TrackB4+8,tempx, 2);
  728. }else{
  729.   TrackB4[8]=48;
  730. TrackB4[9]=utc8r+48;
  731.   
  732. }
  733.   
  734.     for(int col=0;col<16;col++)tempx[col]=0;
  735. if(utc8s>9){
  736. sprintf(tempx, "%d", utc8s);
  737. memcpy(TrackB4+11,tempx, 2);
  738. }else{
  739. TrackB4[11]=48;
  740. TrackB4[12]=utc8s+48;
  741.   
  742. }
  743.     for(int col=0;col<16;col++)tempx[col]=0;
  744. if(utc8f>9){
  745. sprintf(tempx, "%d", utc8f);
  746. memcpy(TrackB4+14,tempx, 2);
  747. }else{
  748. TrackB4[14]=48;
  749.   TrackB4[15]=utc8f+48;
  750. }
  751.   
  752.     for(int col=0;col<16;col++)tempx[col]=0;
  753.   if(utc8m>9){
  754.   sprintf(tempx, "%d", utc8m);
  755. memcpy(TrackB4+17,tempx, 2);
  756. }else{
  757. TrackB4[17]=48;
  758.   TrackB4[18]=utc8m+48;
  759. }
  760.   
  761.   
  762. }
  763.   
  764.   
  765. void jingweiduchuli()
  766. {
  767.   
  768.   
  769. for(int col=0;col<16;col++)tempx[col]=0;
  770. memcpy(tempx,GPRMC+jsq7+4, 2);
  771. jianyan =tempx;
  772. gpschangertest=jianyan.toInt();
  773. if(weiduA!=gpschangertest){
  774. weiduA=gpschangertest;
  775. gpschanger=1;
  776. }
  777.   
  778.   
  779.   
  780. if(GPRMC[jsq6+2]==83)weiduxuweitiao=1;
  781. if(GPRMC[jsq5+2]==87)jingduxuweitiao=1;
  782.   
  783.   
  784. memcpy(TrackB1,GPRMC+jsq7+4, 2);
  785.   
  786. if(gpschangertest<10) {
  787. TrackB1[0]=32;
  788. weiduzhanwei=1;
  789. }else weiduzhanwei=2;
  790.   
  791.   
  792. for(int col=0;col<16;col++)tempx[col]=0;
  793.   
  794. memcpy(tempx,GPRMC+jsq7+6, 2);
  795. jianyan =tempx;
  796. gpschangertest=jianyan.toInt();
  797. if(weiduB!=gpschangertest){
  798. weiduB=gpschangertest;
  799. gpschanger=1;
  800. }
  801.   
  802.   
  803.   
  804.   for(int col=0;col<16;col++)tempx[col]=0;
  805.   
  806. memcpy(tempx,GPRMC+jsq7+9, 4);
  807. if(jsq6-jsq7==12) memcpy(tempx,GPRMC+jsq7+9, 4);
  808. else if(jsq6-jsq7==13)memcpy(tempx,GPRMC+jsq7+9, 5);
  809.   
  810.   
  811. jianyan =tempx;
  812. gpschangertest=jianyan.toInt();
  813. if(weiduC!=gpschangertest){
  814. weiduC=gpschangertest;
  815. gpschanger=1;
  816. }
  817.   
  818.   
  819. fentodu=weiduB*100000+weiduC*10;
  820. fentodu=fentodu/6;
  821.   
  822.   for(int col=0;col<16;col++)tempx[col]=0;
  823. dtostrf(fentodu,6,0,tempx);
  824.   
  825.   
  826.   
  827.   
  828.   
  829. if(fentodu>99999){
  830. memcpy(TrackB1+3,tempx, 6);
  831. }else if(fentodu>9999){
  832. TrackB1[3]=48;
  833.   memcpy(TrackB1+4,tempx, 5);
  834. }else if(fentodu>999){
  835. TrackB1[3]=48;
  836. TrackB1[4]=48;
  837.   memcpy(TrackB1+5,tempx, 4);
  838.   
  839. }else if(fentodu>99){
  840. TrackB1[3]=48;
  841. TrackB1[4]=48;
  842. TrackB1[5]=48;
  843.   memcpy(TrackB1+6,tempx, 3);
  844.   
  845. }else if(fentodu>9){
  846. TrackB1[3]=48;
  847. TrackB1[4]=48;
  848. TrackB1[5]=48;
  849. TrackB1[6]=48;
  850.   memcpy(TrackB1+7,tempx, 2);
  851. }else{
  852. TrackB1[3]=48;
  853. TrackB1[4]=48;
  854. TrackB1[5]=48;
  855. TrackB1[6]=48;
  856. TrackB1[7]=48;
  857.   memcpy(TrackB1+8,tempx, 1);
  858.   
  859. }
  860.   
  861.   
  862.   
  863.   
  864.   
  865. if(weiduxuweitiao){
  866. weiduxuweitiao=0;
  867.   
  868. if(weiduzhanwei==2){
  869.   
  870.   
  871.    for(int col=0;col<16;col++)tempx[col]=0;
  872. tempx[0]=45;
  873. memcpy(tempx+1,TrackB1, 9);
  874. memcpy(TrackB1,tempx, 10);
  875.   
  876. }else{
  877.   
  878.   TrackB1[0]=45;
  879.   
  880. }
  881.   
  882. }
  883.   
  884.   
  885.   
  886.   
  887.   
  888. for(int col=0;col<16;col++)tempx[col]=0;
  889.   
  890.    memcpy(tempx,GPRMC+jsq6+4, 3);
  891. jianyan =tempx;
  892. gpschangertest=jianyan.toInt();
  893. if(jingduA!=gpschangertest){
  894. jingduA=gpschangertest;
  895. gpschanger=1;
  896. }
  897.   
  898.   
  899.   
  900.   
  901.   
  902.   memcpy(TrackB2,GPRMC+jsq6+4, 3);
  903. jingduzhanwei=3;
  904.   
  905. if(gpschangertest<100) {
  906. TrackB2[0]=32;
  907. jingduzhanwei=2;
  908. }
  909.   
  910. if(gpschangertest<10) {
  911. TrackB2[1]=32;
  912. jingduzhanwei=1;
  913. }
  914.   
  915. for(int col=0;col<16;col++)tempx[col]=0;
  916.   
  917. memcpy(tempx,GPRMC+jsq6+7, 3);
  918. jianyan =tempx;
  919. gpschangertest=jianyan.toInt();
  920. if(jingduB!=gpschangertest){
  921. jingduB=gpschangertest;
  922. gpschanger=1;
  923. }
  924.   
  925.   for(int col=0;col<16;col++)tempx[col]=0;
  926.   
  927. if(jsq5-jsq6==13)memcpy(tempx,GPRMC+jsq6+10, 4);
  928. else if(jsq5-jsq6==14)memcpy(tempx,GPRMC+jsq6+10, 5);
  929.   
  930. jianyan =tempx;
  931. gpschangertest=jianyan.toInt();
  932. if(jingduC!=gpschangertest){
  933. jingduC=gpschangertest;
  934. gpschanger=1;
  935. }
  936.   
  937. jianyan="";
  938.   
  939.   
  940.   fentodu=jingduB*100000+jingduC*10;
  941. fentodu=fentodu/6;
  942.   
  943.   for(int col=0;col<16;col++)tempx[col]=0;
  944. dtostrf(fentodu,6,0,tempx);
  945.   
  946.   
  947.   
  948.   
  949.   
  950. if(fentodu>99999){
  951. memcpy(TrackB2+4,tempx, 6);
  952. }else if(fentodu>9999){
  953. TrackB2[4]=48;
  954.   memcpy(TrackB2+5,tempx, 5);
  955. }else if(fentodu>999){
  956. TrackB2[4]=48;
  957. TrackB2[5]=48;
  958.   memcpy(TrackB2+6,tempx, 4);
  959.   
  960. }else if(fentodu>99){
  961. TrackB2[4]=48;
  962. TrackB2[5]=48;
  963. TrackB2[6]=48;
  964.   memcpy(TrackB2+7,tempx, 3);
  965.   
  966. }else if(fentodu>9){
  967. TrackB2[4]=48;
  968. TrackB2[5]=48;
  969. TrackB2[6]=48;
  970. TrackB2[7]=48;
  971.   memcpy(TrackB2+8,tempx, 2);
  972. }else{
  973. TrackB2[4]=48;
  974. TrackB2[5]=48;
  975. TrackB2[6]=48;
  976. TrackB2[7]=48;
  977. TrackB2[8]=48;
  978.   memcpy(TrackB2+9,tempx, 1);
  979.   
  980. }
  981.   
  982.   
  983.   
  984.   
  985.   
  986. if(jingduxuweitiao){
  987. jingduxuweitiao=0;
  988.   
  989. if(jingduzhanwei==3){
  990.   
  991.   
  992.    for(int col=0;col<16;col++)tempx[col]=0;
  993. tempx[0]=45;
  994. memcpy(tempx+1,TrackB2, 10);
  995. memcpy(TrackB2,tempx, 11);
  996.   
  997. }else if(jingduzhanwei==2){
  998.   
  999.   TrackB2[0]=45;
  1000.   
  1001. }else{
  1002.   
  1003. TrackB2[1]=45;
  1004. }
  1005.   
  1006. }
  1007.   
  1008.   
  1009. }
  1010.   
  1011.   
  1012. void hangxiangyusudu()
  1013. {
  1014.   
  1015. for(int col=0;col<16;col++)tempx[col]=0;
  1016. memcpy(tempx,GPRMC+jsq5+4,jsq4-jsq5-3);
  1017.   
  1018.   
  1019. for(int col=0;col<jsq4-jsq5-3;col++){
  1020.   
  1021. if(tempx[col]==46){
  1022.   
  1023.   
  1024.   
  1025.   
  1026. if(col==1)fentodu=(tempx[0]-48)*1000;
  1027. else if(col==2)fentodu=(tempx[0]-48)*10000+(tempx[1]-48)*1000;
  1028. else if(col==3)fentodu=(tempx[0]-48)*100000+(tempx[1]-48)*10000+(tempx[2]-48)*1000;
  1029.   
  1030.   
  1031.   
  1032.   
  1033.   
  1034. if(jsq4-jsq5-col==5)fentodu=fentodu+(tempx[col+1]-48)*100;
  1035. else if(jsq4-jsq5-col==6)fentodu=fentodu+(tempx[col+1]-48)*100+(tempx[col+2]-48)*10;
  1036. else if(jsq4-jsq5-col==7)fentodu=fentodu+(tempx[col+1]-48)*100+(tempx[col+2]-48)*10+tempx[col+3]-48;
  1037.   
  1038.   
  1039.   
  1040.   
  1041.   
  1042. fentodu=fentodu*1852*0.0001;
  1043. col=jsq4-jsq5-3;
  1044.   
  1045. }
  1046.   
  1047. }
  1048.   
  1049.   
  1050. sudu=fentodu*0.01;
  1051. for(int col=0;col<8;col++)TrackB5[col]=0;
  1052. dtostrf(sudu,1,2,TrackB5);
  1053.   
  1054.   
  1055. for(int col=0;col<7;col++)TrackB6[col]=0;
  1056. memcpy(TrackB6,GPRMC+jsq4+2, jsq3-jsq4-2);
  1057.   
  1058.   
  1059. }
  1060.   
  1061.   
  1062. void printDateTime(DateTime dateTime) {
  1063.   
  1064. nian=dateTime.year(), DEC;
  1065.   
  1066.   
  1067.     yue=dateTime.month(), DEC;
  1068.   
  1069. ri=dateTime.day(), DEC;
  1070.   
  1071. shi=dateTime.hour(), DEC;
  1072.   
  1073. fen=dateTime.minute(), DEC;
  1074.   
  1075. miao=dateTime.second(), DEC;
  1076.   
  1077.   
  1078. }
  1079.   
  1080.   
  1081. void DS1307tiquxiaodui()
  1082. {
  1083.   
  1084.   
  1085.   
  1086.   
  1087.     DateTime now = RTC.now();
  1088.   
  1089.       printDateTime(now);
  1090.   
  1091.   
  1092. if(dingweiok){
  1093. if(yue != utc8y) shoushiqidong=1;
  1094. if(ri != utc8r) shoushiqidong=1;
  1095. if(shi != utc8s) shoushiqidong=1;
  1096. if(fen != utc8f) shoushiqidong=1;
  1097. if(miao-utc8m>2 || utc8m-miao>2) shoushiqidong=1;
  1098. if(shoushiqidong){
  1099. shoushiqidong=0;
  1100.   
  1101.    RTC.set(RTC_YEAR, utc8n);
  1102.   
  1103.    RTC.set(RTC_MONTH, utc8y);
  1104.   
  1105.    RTC.set(RTC_DAY, utc8r);
  1106.   
  1107.    RTC.set(RTC_HOUR, utc8s);
  1108.   
  1109. RTC.set(RTC_MINUTE, utc8f);
  1110.   
  1111.    RTC.set(RTC_SECOND, utc8m);
  1112. }
  1113. }
  1114. /*
  1115. jsq3=nian*0.1;
  1116.   jsq3=jsq3%10;
  1117.   temp2[7] = jsq3+48, DEC;
  1118.   jsq3=nian%10;
  1119.   temp2[8] = jsq3+48, DEC;
  1120.   
  1121.    
  1122.   jsq3=yue*0.1;
  1123.   temp2[10] = jsq3+48, DEC;
  1124.   jsq3=yue%10;
  1125.   temp2[11] = jsq3+48, DEC;
  1126.   
  1127.   
  1128.     jsq3=ri*0.1;
  1129.   temp2[13] = jsq3+48, DEC;
  1130.   jsq3=ri%10;
  1131.   temp2[14] = jsq3+48, DEC;
  1132.    
  1133.    */
  1134.   
  1135.   jsq3=shi*0.1;
  1136.   tempx[8] = jsq3+48, DEC;
  1137.   jsq3=shi%10;
  1138.   tempx[9] = jsq3+48, DEC;
  1139.   tempx[10]=':';
  1140.   
  1141.   jsq3=fen*0.1;
  1142.   tempx[11] = jsq3+48, DEC;
  1143.   jsq3=fen%10;
  1144.   tempx[12] = jsq3+48, DEC;
  1145.   tempx[13]=':';
  1146.   
  1147.   
  1148.   jsq3=miao*0.1;
  1149.   tempx[14]= jsq3+48, DEC;
  1150.   jsq3=miao%10;
  1151.   tempx[15] = jsq3+48, DEC;
  1152. }
  1153.   
  1154.   
  1155. void now1602out()
  1156. {
  1157.   
  1158.   
  1159. /*GPS模式下的显示情况说明
  1160.   
  1161. char TrackB0[8]="-0000.0";
  1162. char TrackB1[11]="00.0000000";
  1163. char TrackB2[12]="000.0000000";
  1164. char TrackB4[20]="2000-00-00T00:00:00";
  1165. char TrackB5[8]="0000.00";
  1166. char TrackB6[7]="000.00";
  1167.   
  1168.   
  1169. lcdxianshi=0显示内容如下
  1170.   
  1171. LAT.  00.0000000
  1172. 0123456789012345
  1173. LON. 000.0000000
  1174.   
  1175. lcdxianshi=1显示内容如下
  1176.   
  1177. ASL(G): -0000.0M
  1178. 0123456789012345
  1179. YAW(GPS): 000.00
  1180.   
  1181.   
  1182. lcdxianshi=2显示内容如下
  1183.   
  1184.   00-00T00:00:00
  1185. 0123456789012345
  1186. speed:000.00km/h
  1187.   
  1188.   
  1189. I'm so sorry but
  1190. 0123456789012345
  1191. now the GPS lost
  1192.   
  1193. */
  1194.   
  1195. if(!dingweiok && lcdxianshi<3){
  1196.   
  1197. lcd.setCursor(0, 0);
  1198. lcd.print("I'm so sorry but");
  1199. lcd.setCursor(0, 1);
  1200. lcd.print("now the GPS lost");
  1201.   
  1202. }else{
  1203.   
  1204. if(lcdxianshi==0){
  1205.   
  1206. lcd.setCursor(0, 0);
  1207. lcd.print("LAT.  ");
  1208. lcd.print(TrackB1);
  1209.   
  1210. lcd.setCursor(0, 1);
  1211. lcd.print("LON. ");
  1212. lcd.print(TrackB2);
  1213.   
  1214.   
  1215. }else if(lcdxianshi==1){
  1216.   
  1217. lcd.setCursor(0, 0);
  1218. lcd.print("ASL(G): ");
  1219. lcd.print(TrackB0);
  1220. lcd.print("M    ");
  1221. lcd.setCursor(0, 1);
  1222. lcd.print("YAW(GPS): ");
  1223. lcd.print(TrackB6);
  1224. lcd.print("        ");
  1225.   
  1226.   
  1227. }else if(lcdxianshi==2){
  1228.   
  1229. for(int col=0;col<16;col++)tempx[col]=0;
  1230. for(int col=0;col<14;col++)tempx[col]=TrackB4[col+5];
  1231. lcd.setCursor(0, 0);
  1232. lcd.print("  ");
  1233. lcd.print(tempx);
  1234. lcd.setCursor(0, 1);
  1235. lcd.print("speed:");
  1236. lcd.print(TrackB5);
  1237. lcd.print("km/h    ");
  1238.   
  1239. }else if(lcdxianshi==3){
  1240.   
  1241.   
  1242.   
  1243.   lcd.setCursor(0, 1);
  1244.   
  1245.   
  1246.   
  1247. lcd.print(tempx);
  1248.   
  1249.   
  1250. }
  1251.   
  1252. }
  1253.   
  1254.   
  1255. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-10-28 02:08:43 | 显示全部楼层
千多行的程式, 很多重覆的地方, 而且 一行一個字符的設定, 看得很累呢.  

另外, 想問問, GP2Y1010 是直接 analog 輸出測量給果的嗎?  看似比 PPD42NS 方便得多了.
回复 支持 反对

使用道具 举报

发表于 2014-10-28 08:47:19 | 显示全部楼层
楼主辛苦了  代码优化下自己写的也轻松点 别人读也轻松了。
字符数组没必要这样赋值吧。

不过 楼主写出来了 总算是给大家学习的机会~~~ 赞楼主~~~~~~
回复 支持 反对

使用道具 举报

发表于 2014-10-28 09:27:30 | 显示全部楼层
程序真长……………………………………………………
回复 支持 反对

使用道具 举报

发表于 2014-10-28 14:10:48 | 显示全部楼层
嗯,这程序乱的和我的有一拼。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-29 23:31:54 | 显示全部楼层
本帖最后由 hi55234 于 2014-11-12 22:41 编辑

修改·············
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-12 22:49:43 | 显示全部楼层
牙疼ing~~~~~~~~~~~~~
回复 支持 反对

使用道具 举报

发表于 2014-12-25 19:27:24 | 显示全部楼层
谢谢分享,程序太长了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 09:34 , Processed in 0.064735 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表