hcsr04

HC-SR04 Ultrasonic Ranging Module Library.

Author Rob Jansen, Copyright © 2023..2023, all rights reserved.
Adapted-by
Compiler 2.5r8

Description

Library for the HC-SR04 Ultrasonic Ranging Module. 
             The library can be configured to use either the external
             interrupt or an IOC interrupt to measure the echo signal 
             from the module using Timer 1.
             The default is using an external interrupt. In order to switch 
             to using an IOC interrupt the user must initialize the IOC
             to be active on both the rising and falling edge and must
             define the following alias (example for IOC on pin A2):
             -) alias hcsr04_ioc_flag is IOCAF_IOCAF2 


Sources

Datasheet Ultrasonic ranging module : HC-SR04.


Notes

The echo signal ranges from about 150 us to 25 ms. In case
             there is no object, the echo signal is about 38 ms. If
             no echo signal is received, the library will timeout after
             65 ms when Timer 1 runs at 1 MHz.


Dependencies

No dependency found



Summary

Global variables/contants

Procedures

Private

Functions

Private

API details

Global variables/contants

Procedures

Private
  • _hcsr04_timer1_interrupt()

    Timer1 interrupt. Occurs when the timer overflows after 65 ms (1 MHz clock)
    which means that no measurement could be done. We need to trigger the 
    interrupt to activate the state machine and handle the overflow.
    
    

  • _hcsr04_interrupt()

    Interrupt routine, triggerd by INT or IOC.
    
    

  • _hcsr04_timer1_reset()

    Reset Timer 1 and start the timer.
    
    


Functions

Private
  • _hcsr04_get_timer1_value() return word

     ----------------------------------------------------------------------------
    Stop Timer 1 and return the value of Timer 1. We return the Timer value if 
    it has not overflown otherwise this function returns the overflow value.
    
    

  • _hcsr04_handle_interrupt_flag() return bit

    This procedure handles the interupt flag. Reason for this procedure is to be
    able to use two differnt interrupt types, IOC and external.
    
    



Related samples

Here are the list of samples which use this library:

16f182516f1825_hcsr04_led.jal
16f182516f1825_hcsr04_serial.jal