WathchDog

Dependencies:   mbed WDT

Revision:
4:cb6587253326
Parent:
3:b082ed02d08c
Child:
5:efffadf6d0e0
--- a/main.cpp	Sun Feb 24 08:57:06 2019 +0000
+++ b/main.cpp	Sun Mar 03 18:30:14 2019 +0000
@@ -1,9 +1,9 @@
 #include "mbed.h"
 
-const int _ID = 4;
+//const int _ID = 4;
 
 void irqrecv();
-int rs485_recv(float ph);
+int rs485_recv(float ,float ,float ,float ,float);
 char rsgetc();
 int rslen();
 
@@ -16,19 +16,27 @@
 
 DigitalOut led(LED1);
 DigitalOut sokutei(D3);
-DigitalOut commndout(D4);
+DigitalOut pos1(D4);
+DigitalOut pos2(D5);
 DigitalIn in1(D9,PullNone);
 DigitalIn in2(D10,PullNone);
 DigitalIn kansyutu(D6,PullNone);
 DigitalIn commandin(D7,PullNone);
 DigitalIn jmp(D2,PullUp);
+DigitalIn pulse1(D9,PullNone);
+DigitalIn pulse2(D10,PullNone);
+
+Timer pulse_watcher;
 
 
-#define bufsize 64
+#define bufsize 1024
 char RxBuff[bufsize+1];
 int RxPtr;
 int RdPtr;
 
+int data[40];
+
+
 char rsgetc() {
 //    pc.printf("%c",RxBuff[RdPtr]);
 
@@ -57,7 +65,7 @@
     }
 }
 
-unsigned char buf485[32];
+unsigned char buf485[512];
 int cnt485;
 
 int main()
@@ -66,6 +74,7 @@
     float ad1,ad2,ad3,ad4;
     static float temp;//,turb,s;
     int i,cnt;
+    int pulse;
 
     modem.baud(38400);
     modem.format(8,Serial::None,1);    
@@ -76,16 +85,32 @@
     buf485[0] = '\0';
 
     pc.printf("\nSTM32 ADC internal channels reading example\r\n");
+    pos1 = 1;
+    pos2 = 1;
+
     temp = 0;
 //    turb = 0;
 //    s = 0;
-    
     sokutei = 1;
+    pulse_watcher.start();
+    pulse = pulse1 || pulse2;
+
+    for (i = 0;i<40;i++) data[i] = 0;
+
     
     while(1) {
+        if (pulse_watcher.read() > 100) pulse_watcher.reset();
         
-        if (jmp) sokutei = 1;
-        else sokutei = 0;
+//        if (jmp) sokutei = 1;
+//        else sokutei = 0;
+
+//        if (jmp) {
+//            pos1 = 1;
+//            pos2 = 1;
+//        } else {
+//            pos1 = 0;
+//            pos2 = 0;
+//        }
         
         sum1 = 0;
         sum2 = 0;
@@ -93,7 +118,7 @@
         sum4 = 0;
         cnt = 0;
         for (i=0;i<1000;i++) {
-            rs485_recv(temp);
+            rs485_recv(temp,ad1,ad2,ad3,ad4);
 
             sum1 += adc_1.read();
             sum2 += adc_2.read();
@@ -107,6 +132,19 @@
 //                wait(0.01);
 //                modem.printf("=04,PVAL,%01.04f,0,\r",temp);
 //            }
+            // pulse watch
+            if ( (pulse1 || pulse2) != pulse ) {
+                pulse_watcher.reset();
+                pos1 = 0;
+                pos2 = 0;
+            }
+            pulse = pulse1 || pulse2;
+            if ( pulse_watcher.read() > 10) {
+                pos1 = 1;
+                pos2 = 1;
+            }
+            
+            // pulsewatch
         }
         if(cnt != 0) {
             ad1 = sum1/cnt;
@@ -127,54 +165,65 @@
 //        pc.printf("ADC = %f  %f  %f  %f %f \r\n", ad1,ad2,ad3,ad4,temp);
 //        s = 0;
         
+        // data set
+        data[8] = ad1 * 0xffff;
+        data[9] = ad2 * 0xffff;
+        data[10] = ad3 * 0xffff;
+        data[12] = ad4 * 0xffff;
     }
 }
 
 
-int rs485_recv(float ph) {
+int rs485_recv(float temp,float ad1,float ad2,float ad3,float ad4) {
     char chr;    
-    char idbuf[8];
-
+//    char idbuf[8];
+    char sendbuf[512];
+//    int i;
+    
     if (rslen()) {
-//        pc.printf("%03d\r\n",rslen());
-//        wait(0.01);
-//        while(rslen()) rsgetc();
         
         chr = rsgetc();
         buf485[cnt485++] = chr;
         buf485[cnt485] = '\0';
         if (chr == '?') cnt485 = 0;
         
-//        if (chr == '?') pc.printf("?\r\n");
-//        if (chr == '0') pc.printf("0\r\n");
-
-
-
-        if (chr == 0x0d) {
-//           pc.printf("Recv CR:%s\r\n",buf485);
-            sprintf(idbuf,"%02d",_ID);
-//            idbuf[0] = '0';
-//            idbuf[1] = '4';
-//            idbuf[2] = '\0';
-//            if((idbuf[0] == buf485[1]) && (idbuf[1] == buf485[2])) {
+        if (chr == 0x0a) {
+        pc.printf("CR\r\n");
+            
+            if( ('C' == buf485[0]) ) {
+                if (kansyutu) data[0] = 1;
+                else data[0] = 0;
 
-            if((idbuf[0] == buf485[0]) && (idbuf[1] == buf485[1]) && (',' == buf485[2]) && ('P' == buf485[3])) {
-                // answer
-// konnkaiha modemunanode huyou                wait(0.02);
-//                RE = 1;//0;
-//                DE = 1;
-//                pc.printf("Ans:=%c%c,PVAL,%01.04f,0\r\n", 0x30 | idbuf[0],0x30 | idbuf[1],ph);
-                modem.printf("=%c%c,PVAL,%01.04f,0\r", 0x30 | idbuf[0],0x30 | idbuf[1],ph);
-// konkaiha modemunanode huyou                wait(0.01);
-//                RE = 0;//1;
-//                DE = 0;
-//                while(rslen()) rsgetc();
-                RxPtr = 0;
-                RdPtr = 0;
-                RxBuff[0] = '\0';
-                buf485[0] = '\0'; // clear
-                cnt485 = 0;
+                // sokutei on/off
+                if( buf485[1] & 0x05 ) {
+                    sokutei = 1;
+                    data[0] = data[0] | 64 | 128;
+                } else {
+                    sokutei = 0;
+                }
+                if( buf485[1] & 0x0a ) {
+                    wait(0.1);
+                    modem.printf("?00,WIPE,1,\n");
+                } else {
+                    // answer
+                    sprintf(sendbuf,"=%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x\r\n", 
+                     data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],
+                     data[10],data[11],data[12],data[13],data[14],data[15],data[16],data[17],data[18],data[19],
+                     data[20],data[21],data[22],data[23],data[24],data[25],data[26],data[27],data[28],data[29],
+                     data[30],data[31],data[32],data[33],data[34],data[35],data[36],data[37],data[38],data[39]
+                    );
+                    //modem.printf("=%c%c,PVAL,%01.04f,%01.04f,%01.04f,%01.04f,%01.04f,%01d\r", 0x30 | idbuf[0],0x30 | idbuf[1],temp,ad1,ad2,ad3,ad4,kan);
+                    
+                    wait(0.1);
+                    modem.printf("%s",sendbuf);
+                    pc.printf("%s",sendbuf);
+                }
             }
+            RxPtr = 0;
+            RdPtr = 0;
+            RxBuff[0] = '\0';
+            buf485[0] = '\0'; // clear
+            cnt485 = 0;
         }
    }
     return(0);