Hi
@Vogon: mit diesen Files funktionniert die Sache leider auch nicht. Ich erhalte (mit den Files, die du auf Google gefunden hast) immer einene Wert um 550 herum, obwohl die Distanz nur ca. 1.2 Meter wäre.
Wenn ich ein Buch davor halte, ändert sich nichts am Wert.
Etwa einmal auf 20 Messungen kommt ein viel kleinerer Wert heraus, der eher als Distanz passen würde, aber auch nicht genau stimmt.
Das ist der Code:
Code:
/* Code for Parrallax "PING". 3-legged UltraSonic sensor to Handyboard.
Modified by Stefan Ericsson, HIS, Sweden
Connect signal to digital(7)+ 1K-Ohm resistor from digital(7) to "SPI" SS/PD5.
Enjoy!! */
void sonar_init() {
// CHR: adresserna 0x0000 till 0x10FF ar till 68HC11 CPU.
bit_set(0x1009, 0x30); // ddrd - CHR: activates dig-out.
bit_set(0x1021, 2); /* at tctl2, */
bit_clear(0x1021, 1); /* set tic3 for rising edge */
//
printf("sonar initiated!\n"); // CHR - Take away! Only for testing.
//
}
float sonar_sample() {
/*
note on variable names
The first part indicates its use, the second the units.
st is start time, end is end time, dur is duration, dst is distance.
ms is millisec, us is micro sec, cm is centimeters.
*/
int st_us,end_us;
long st_ms,end_ms,dur_ms,abort_ms;
int dst_cm;
/* trigger the pulse */
bit_set(0x1008, 0x20); // activates dig-out
/* clear trigger and BINH */
bit_clear(0x1008, 0x20);
// capture the start time CHR: st_ms is start time in milliseconds.
st_ms = mseconds();
/* calc the abort time = 70ms is about 12 meters */
abort_ms = st_ms + 70L;
st_us = peekword(0x100e); // CHR: st_us is start time in microseconds. peekword reads memory as Long from location 0x100e & 0x100f.
poke(0x1023, 1); // clear tic3 flag
/* while the echo has not come in */
while ((peek(0x1000) & 0x1)) { // CHR: & means bitwise comparison. Echo comes in at adress 0x1000.
/* if we've been waiting too long (70ms = 12 meters) */
if (mseconds() > abort_ms) {
return(-1.0);
}
defer();
}
/* save the end times */
end_us = peekword(0x1014);
dur_ms = mseconds() - st_ms;
/* divide timers by number of cycles required to travel 1cm */
st_us = st_us / 117;
end_us = end_us / 117;
/* find distance difference */
dst_cm = end_us - st_us;
/* if distance is less than zero, add distance of 65536 cycles */
if (dst_cm < 0) dst_cm = dst_cm + 558;
/* if the millisecond counter indicates that the integer counter has cycled */
if (dur_ms > 32L) dst_cm = dst_cm + 558;
if (dur_ms > 64L) dst_cm = dst_cm + 558;
/* my unit reads about 5cm too high - yours may differ */
return ((float)dst_cm); //(dst_cm - 5);
}
void main(){ // sonar_display()
int distance;
sonar_init();
while (1) {
float result;
result= sonar_sample();
if (result != -1.0) printf("%f\n", result); // f.d. result
else printf("*******\n");
msleep(300L);
}
}
mfg bekoeppel
Lesezeichen