Pilotare un modulo ad ultrasuoni con un PIC16F877

PILOTARE UN MODULO AD ULTRASUONI CON UN PIC16F877

Il modulo SRF04 è un dispositivo ad ultrasuoni che consente di rilevare la vicinanza di oggetti fino a 3 metri. Il sensore invia un segnale ad ultrasuoni e capta l’eco generato dagli oggetti vicini. Per determinare la distanza di un oggetto  dal sensore è dunque necessario  misurare il ritardo che intercorre tra l’invio del segnale e la ricezione dell’eco. Tale tempo, moltiplicato per la velocità del suono (che può  essere considerata  costante  e pari  a 347m/s) consente di ricavare la distanza dell’oggetto. Nel listato 1 la funzione che calcola la distanza dal sensore.

Void monitor_distance_sensor(void) {
  /* First, toggle the ULTRASONIC_TX pin to generate echo signal. */
  /* Pull ultrasonic TX pin high */
  output_high(ULTRASONIC_TX);
  /* Get the sensor the chance to see this */
  delay_us(INPUT_TRIGGER_DELAY);
  output_low(ULTRASONIC_TX);
  /* Wait for pulse on ULTRASONIC_RX pin. Distance is measured by
    using time it takes for ultrasound to echo, which is
    calculated according to length of echo pulse on ULTRASONIC_RX
    pin. */
  /* Wait for low to high transition on ULTRASONIC_RX pin. */
  for ( ; !input(ULTRASONIC_RX); )
   ;
  /* Detected a low to high transition */
  /* Reset timer. */
  set_timer1(0);
  /* Wait for high to low transition on ULTRASONIC_RX pin. */
  for ( ; input(ULTRASONIC_RX); )
   ;
  /* Detected a high to low transition */
  echo_delay = get_timer1();
#ifdef 20_MHZ_CLOCK
  if (echo_delay >= 44000) {
#else
  if (echo_delay >= 35000) {
#endif
    distance = DISTANCE_INFINITE;
  }
  else {
    distance = GET_DISTANCE(echo_delay);
  }
  /* Make sure there is an appropriate delay between end of echo and
     trigger pulse. If the delay between calls to monitor_distance_sensor
     is greater than 10 ms, then this delay can be removed. */
     delay_ms(DELAY_BETWEEN_ECHO_PULSE);
} /* monitor_distance_sensor */
Listato 1
Scarica subito una copia gratis

5 Commenti

  1. Avatar photo Marcello.Colozzo 1 16 Luglio 2016
  2. Avatar photo Longflyer 13 Novembre 2016
    • Avatar photo Maurizio 13 Novembre 2016

Scrivi un commento

Seguici anche sul tuo Social Network preferito!

Send this to a friend