test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Revision:
6:a760ce6defbe
Parent:
5:97117a837d2c
Child:
7:2f218add711e
--- a/main.cpp	Tue Jul 21 12:18:14 2020 +0000
+++ b/main.cpp	Wed Jul 22 08:31:28 2020 +0000
@@ -1,21 +1,44 @@
 #include <global.h>
 
 Serial pc(UART1_TX, UART1_RX);
+RS485 RS485(UART2_TX,UART2_RX,DE_TXD_2); // Tx, Rx , !RE and DE MAX485 pin
+ 
 //Serial pc(USBTX, USBRX);
 Timer timer;
+typedef uint8_t byte;
 
+DigitalOut select(DE_TXD_2);
+byte full_value[9];
+byte base_data[4] = {0xAA,0x55,0x03,0x01};
+byte data[9] = {0x01,0x04,0x00,0x48,0x00,0x02,0xf1,0xdd};//your data
+
+char test_buffer[10]; 
+
+const unsigned char CRC7_POLY = 0x91;
+ 
+unsigned char getCRC(unsigned char message[], unsigned char length)
+{
+  unsigned char i, j, crc = 0;
+ 
+  for (i = 0; i < length; i++)
+  {
+    crc ^= message[i];
+    for (j = 0; j < 8; j++)
+    {
+      if (crc & 1)
+        crc ^= CRC7_POLY;
+      crc >>= 1;
+    }
+  }
+  return crc;
+}
+ 
+ 
 int main()
 {
-//
     DigitalOut led(DEBUG_LED);
     DigitalOut led2(DE_TXD_1, 1); // activate transmitting rs485-1
-//   led = false;
-//   while (true) {
-//        led = !led;
-//        wait_ms(BLINKING_RATE);
-//        pc.printf("Distance:");
-//    }
-//
+
 
 #if TEST_ULTRASONIC
     int time_to_check_us = 1000;
@@ -33,9 +56,10 @@
         //while (sensor1.isNewDataReady() == false && timer.read_ms() < time_to_check_us) {
         //pc.printf("Distance1: %5.1f mm -- Distance2: %5.1f mm -- -- Distance3: %5.1f mm \r\n", sensor1.getDistance_cm()), sensor2.getDistance_cm(), sensor3.getDistance_cm());
         pc.printf("Distance1: %5.1f mm \r\n", sensor1.getDistance_cm());
+        
 
         timer.stop();
-        wait_ms(10);
+        wait_ms(2);
 
     }
 
@@ -77,5 +101,43 @@
 
 #endif
 
+# if DEMO_CODE
+
+    JSN_SR04 sensor1(TIM1_CH2, TRIG_PA8_OUT);
+    E18_D80NK infared1 (IR1_PB12_OUT);
+    E18_D80NK infared2 (IR2_PB13_OUT);
+    
+    sensor1.setRanges(30, 200);
+    //full_value[0] = base_data[0]
+//    full_value[1] = base_data[1]
+//    full_value[2] = base_data[2]
+//    full_value[3] = base_data[3] 
+//    full_value[4] = base_data[4]
+    
+    while(true) {
+        timer.reset();
+        timer.start();
+        sensor1.startMeasurement();
+        pc.printf("Distance1: %5.1f mm  ----- %d ---- %d  \r\n", sensor1.getDistance_cm() , infared1.checkObstacle(),infared2.checkObstacle());
+        
+        //full_value[5] = base_data
+//        full_value[6] = base_data
+//        full_value[7] = base_data
+//        full_value[8] = base_data
+        
+        sprintf(test_buffer, "%5.1f_%d_%d ", sensor1.getDistance_cm() , infared1.checkObstacle(),infared2.checkObstacle()); 
+        
+        select = 1 ;   // Enable sending on MAX485
+        RS485.sendMsg(data,sizeof(data));
+        wait_ms(100);
+        select = 0 ;  // Enable receiving on MAX485
+        timer.stop();
+        wait_ms(2);
+
+    }
+
+
+#endif
+
 }