text:  /******************************************************************************
   *
   * Module: HCSR04 Ultrasonic Sensor
   *
   * File Name: HCSR04_ultrasonic_sensor.c
   *
   * Description: Source file for the AVR HCSR04 Ultrasonic Sensor driver
   *
   * Author: Ahmed Alaa
   *
   *******************************************************************************/
  #include "HCSR04_ultrasonic_sensor.h"
  #include "icu.h"
  #include "gpio.h"
  #include "util/delay.h"
  #include "lcd.h"
  /*******************************************************************************
   *                         		Static Global Variables  		               *
   *******************************************************************************/
  static volatile uint16 distance = 0;
  /*******************************************************************************
   *                              		Functions  		                       *
   *******************************************************************************/
  /*
   * Description :
   * 1. Initialize the ICU driver as required.
   * 2. Setup the ICU call back function.
   * 3. Setup the direction for the trigger pin as output pin through the GPIO driver.
   * Inputs:
   * 		None
   * Outputs:
   * 		None
   */
  void Ultrasonic_init(void)
  {
  	Icu_ConfigType Config_Struct =
  	{
  			F_CPU_8,
  			FALLING
  	};
  
  	Icu_setCallBack(Ultrasonic_edgeProcessing);
  	Icu_init(&Config_Struct);
  	GPIO_setupPinDirection(PORTB_ID, PIN4_ID, PIN_OUTPUT);
  }
  
  /*
   * Description :
   * 1. Send the Trigger pulse to the ultrasonic.
   * Inputs:
   * 		None
   * Outputs:
   * 		None
   */
  static void Ultrasonic_Trigger(void)
  {
  	GPIO_writePin(PORTB_ID, PIN4_ID, LOGIC_HIGH);
  	_delay_us(10);
  	GPIO_writePin(PORTB_ID, PIN4_ID, LOGIC_LOW);
  
  	Icu_clearTimerValue();
  }
  
  /*
   * Description :
   * 1. Send the trigger pulse by using Ultrasonic_Trigger function.
   * 2. Start the measurements by the ICU from this moment.
   * Inputs:
   * 		None
   * Outputs:
   * 		The measured distance in Centimeter.
   */
  uint16 Ultrasonic_readDistance(void)
  {
  	Ultrasonic_Trigger();
  	return distance;
  }
  
  /*
   * Description :
   * 1. This is the call back function called by the ICU driver.
   * 2. This is used to calculate the high time (pulse time) generated by the ultrasonic sensor.
   * Inputs:
   * 		None
   * Outputs:
   * 		None
   */
  void Ultrasonic_edgeProcessing(void)
  {
  	distance = (uint16)(Icu_getInputCaptureValue() / 58.8);
  	Icu_clearTimerValue();
  }
  