summaryrefslogtreecommitdiffstats
path: root/source/AVR_Studio/Soccer/hal/board.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/AVR_Studio/Soccer/hal/board.c')
-rwxr-xr-xsource/AVR_Studio/Soccer/hal/board.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/source/AVR_Studio/Soccer/hal/board.c b/source/AVR_Studio/Soccer/hal/board.c
index 0731886..b01d79c 100755
--- a/source/AVR_Studio/Soccer/hal/board.c
+++ b/source/AVR_Studio/Soccer/hal/board.c
@@ -92,6 +92,29 @@ int Board::GetADC(uint8_t channel) {
return result;
}
+// Gibt den Wert vom Abstandsensor zurück
+int Board::GetAbstand(int i) {
+ int result = -1;
+ if((i < 0) || (i > 3)) return result; // Ungültige Nummern rausfiltern
+
+ // Sende zunächst einen Impuls aus
+ ABSTAND_DDR |= (1 << i); // Konfiguriere Pin als Ausgang
+ ABSTAND_PORT |= (1 << i); // Und setze ihn auf High
+ usleep(10); // Warte jetzt 10us
+ ABSTAND_PORT &= ~(1 << i); // Und setze den Pin wieder auf Low
+ ABSTAND_DDR &= ~(1 << i); // Konfiguriere Pin als Eingang
+
+ // Jetzt warten wir auf die Antwort vom Sensor
+ while(!(ABSTAND_PIN & i)) {} // während er low ist nichts machen
+ while(ABSTAND_PIN & i) { // Und während er high ist
+ result++; //schleifendurchläufe zähenlen (unsauber, ich weiß)
+ asm volatile("nop"); // ein ganz bisschen warten
+ }
+
+ // Die Zahl der Schleifendurchläufe geben wir dann zurück
+ return result;
+}
+
void Board::beep(int freq) {
beepFreq = freq;
}