Sfoglia il codice sorgente

synchronous ultrasound ping

subDesTagesMitExtraKaese 4 anni fa
parent
commit
473fe262a3
1 ha cambiato i file con 19 aggiunte e 17 eliminazioni
  1. 19 17
      arduino/src/ultrasonic.cpp

+ 19 - 17
arduino/src/ultrasonic.cpp

@@ -3,7 +3,7 @@
 struct usData_t usData[2];
 
 void us_0_isr() {
-  if(digitalRead(US_RX_0_PIN)) {
+  if(digitalRead(US_RX_0_PIN) && usData[0].rxPending) {
     usData[0].pulseStart = micros();
   } else if(usData[0].rxPending) {
     usData[0].duration   = micros() - usData[0].pulseStart;
@@ -13,7 +13,7 @@ void us_0_isr() {
 }
 
 void us_1_isr() {
-  if(digitalRead(US_RX_1_PIN)) {
+  if(digitalRead(US_RX_1_PIN) && usData[1].rxPending) {
     usData[1].pulseStart = micros();
   } else if(usData[1].rxPending) {
     usData[1].duration   = micros() - usData[1].pulseStart;
@@ -28,25 +28,27 @@ void us_init() {
 }
 
 void us_transmit() {
+  //testing: crashes 
+
   noInterrupts();
 
-  pinMode(US_RX_0_PIN, OUTPUT);
-  pinMode(US_RX_1_PIN, OUTPUT);
-  pinMode(US_TX_0_PIN, OUTPUT);
-  digitalWrite(US_RX_0_PIN, LOW);
-  digitalWrite(US_RX_1_PIN, LOW);
-  digitalWrite(US_TX_0_PIN, LOW);
+  //set pin 2, 3 and 4 to output
+  DDRD  |= 0b00011100;
+
+  //pull pin 2, 3 and 4 to 5V
+  //doing this seems to lead to power supply issues and the serial interface stopps working
+  //further testing needed!
+  PORTD |= 0b00011100;
+
   delayMicroseconds(2);
-  digitalWrite(US_RX_0_PIN, HIGH);
-  digitalWrite(US_RX_1_PIN, HIGH);
-  digitalWrite(US_TX_0_PIN, HIGH);
+
+  //pull pin 2, 3 and 4 to GND
+  PORTD &= 0b11100011;
+
   delayMicroseconds(5);
-  digitalWrite(US_RX_0_PIN, LOW);
-  digitalWrite(US_RX_1_PIN, LOW);
-  digitalWrite(US_TX_0_PIN, LOW);
-  pinMode(US_RX_0_PIN, INPUT);
-  pinMode(US_RX_1_PIN, INPUT);
-  pinMode(US_TX_0_PIN, INPUT);
+
+  //set pin 2, 3 and 4 back to input
+  DDRD  &= 0b11100011;
 
   interrupts();