(1) Using IO trigger for at least 10us high level signal,
(2) The Module automatically sends eight 40 kHz and detect whether there is a
pulse signal back.
(3) IF the signal back, through high level , time of high output IO duration is
the time from sending ultrasonic to returning.
Test distance = (high level time×velocity of sound (340M/S) / 2
I'm interested only to know the differents beetween start and end time.
this is the code,
in the main function:
-the first 3 lines send high level signal at 10us
-the first while loop read if there is an high level signal
-the second while loop read if the singnal is gone down
It doesn't work. What is the problem?
Code: Select all
#include "ch.h"
#include "hal.h"
#include "pwm.h"
#include "pwm_lld.h"
#include "chvt.h"
void delay(int n) {
chThdSleepMilliseconds(n);
}
void blink1(void) {
palSetPad(GPIOA, GPIOA_LED_GREEN);
delay(1000);
palClearPad(GPIOA, GPIOA_LED_GREEN);
}
int main(void) {
//GPIOA_PIN8 (PA8) trigger, GPIOA_PIN9 (PA9) echo
palSetPad(GPIOA, GPIOA_PIN8);
delay(100);
palClearPad(GPIOA, GPIOA_PIN8);
//ST2MS converts from ticks to milliseconds and chVTGetSystemTime Returns the number of system ticks since the chSysInit() invocation
while (!palReadPad(GPIOA, GPIOA_PIN9)) ;
int start = ST2MS(chVTGetSystemTime());
while (palReadPad(GPIOA, GPIOA_PIN9)) ;
int end = ST2MS(chVTGetSystemTime());
int time = end-start;
if(time < 38000)
blink1();
else break;
delay(1500);
}
Thanks for your help.