From d90c7a9e47b2f10bffa98004232df784eb235056 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Fri, 21 Oct 2022 16:06:49 +0200 Subject: [PATCH] added ICU & ultrasonic drivers --- HC-SRO4 - Ultrasonic/.cproject | 115 ++++++++++++++++ HC-SRO4 - Ultrasonic/.project | 27 ++++ .../.settings/de.innot.avreclipse.core.prefs | 7 + .../org.eclipse.core.resources.prefs | 2 + HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.c | 120 +++++++++++++++++ HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.h | 66 ++++++++++ ICU/.cproject | 124 ++++++++++++++++++ ICU/.project | 27 ++++ ICU/.settings/de.innot.avreclipse.core.prefs | 7 + .../org.eclipse.core.resources.prefs | 2 + ICU/src/icu.c | 123 +++++++++++++++++ ICU/src/icu.h | 72 ++++++++++ 12 files changed, 692 insertions(+) create mode 100644 HC-SRO4 - Ultrasonic/.cproject create mode 100644 HC-SRO4 - Ultrasonic/.project create mode 100644 HC-SRO4 - Ultrasonic/.settings/de.innot.avreclipse.core.prefs create mode 100644 HC-SRO4 - Ultrasonic/.settings/org.eclipse.core.resources.prefs create mode 100644 HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.c create mode 100644 HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.h create mode 100644 ICU/.cproject create mode 100644 ICU/.project create mode 100644 ICU/.settings/de.innot.avreclipse.core.prefs create mode 100644 ICU/.settings/org.eclipse.core.resources.prefs create mode 100644 ICU/src/icu.c create mode 100644 ICU/src/icu.h diff --git a/HC-SRO4 - Ultrasonic/.cproject b/HC-SRO4 - Ultrasonic/.cproject new file mode 100644 index 0000000..5bd7af4 --- /dev/null +++ b/HC-SRO4 - Ultrasonic/.cproject @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/HC-SRO4 - Ultrasonic/.project b/HC-SRO4 - Ultrasonic/.project new file mode 100644 index 0000000..ed38e55 --- /dev/null +++ b/HC-SRO4 - Ultrasonic/.project @@ -0,0 +1,27 @@ + + + HC-SRO4 - Ultrasonic + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + de.innot.avreclipse.core.avrnature + + diff --git a/HC-SRO4 - Ultrasonic/.settings/de.innot.avreclipse.core.prefs b/HC-SRO4 - Ultrasonic/.settings/de.innot.avreclipse.core.prefs new file mode 100644 index 0000000..6139929 --- /dev/null +++ b/HC-SRO4 - Ultrasonic/.settings/de.innot.avreclipse.core.prefs @@ -0,0 +1,7 @@ +avrtarget/ClockFrequency=1000000 +avrtarget/ExtRAMSize=0 +avrtarget/ExtendedRAM=false +avrtarget/MCUType=atmega32 +avrtarget/UseEEPROM=false +avrtarget/UseExtendedRAMforHeap=true +eclipse.preferences.version=1 diff --git a/HC-SRO4 - Ultrasonic/.settings/org.eclipse.core.resources.prefs b/HC-SRO4 - Ultrasonic/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/HC-SRO4 - Ultrasonic/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.c b/HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.c new file mode 100644 index 0000000..853b0af --- /dev/null +++ b/HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.c @@ -0,0 +1,120 @@ + /****************************************************************************** + * + * Module: HC-SRO4 Ultrasonic + * File Name: HC-SRO4_ultrasonic.c + * Description: Source file for the HC-SR04 Ultrasonic driver + * Author: Ibrahim Mohamed + * + *******************************************************************************/ + +#include +#include "gpio.h" +#include "common_macros.h" +#include "HC-SRO4_ultrasonic.h" +#include "icu.h" + + +/******************************************************************************* + * Global Functions * + *******************************************************************************/ + + +static uint16 g_echo = 0; +static uint8 g_countOfEdges = 0; + +/******************************************************************************* + * Functions Definition * + *******************************************************************************/ + + +/* + * Description : + * Initialize the ICU driver. + * Setup the ICU call back function. + * Setup the direction for the trigger pin as output pin through the GPIO driver. + */ +void Ultrasonic_init(void){ + + /* + * set up the configurations to the ICU unit to work with: + * - the processor frequency over 8 + * - capture the rising edge of the pluse + * + */ + Icu_ConfigType ICU_configuration = { + F_CPU_8, + RISING + }; + + + /* initialize the ICU with configurations */ + Icu_init(&ICU_configuration); + + /* set the callback function which will calculate the time */ + Icu_setCallBack(Ultrasonic_edgeProcessing); + + /* set up the trigger port to be output */ + GPIO_setupPinDirection(ULTRASONIC_TRIGGER_PORT, ULTRASONIC_TRIGGER_PIN, PIN_OUTPUT); +} + +/* + * Description : + * Send the Trigger pulse to the ultrasonic. + */ +void Ultrasonic_Trigger(void){ + + GPIO_writePin(ULTRASONIC_TRIGGER_PORT, ULTRASONIC_TRIGGER_PIN, LOGIC_HIGH); + _delay_ms(1); + GPIO_writePin(ULTRASONIC_TRIGGER_PORT, ULTRASONIC_TRIGGER_PIN, LOGIC_LOW); + +} + +/* + * Description : + * Send the trigger pulse by using Ultrasonic_Trigger function. + * Start the measurements by the ICU from this moment. + */ +uint16 Ultrasonic_readDistance(void){ + + /* send trigger signal */ + Ultrasonic_Trigger(); + + /* polling */ + while(g_countOfEdges != 2); + + g_countOfEdges = 0; + return g_echo/58.8; /* return the distance */ + +} + +/* + * Description : + * This is the call back function called by the ICU driver. + * This is used to calculate the high time (pulse time) generated by the ultrasonic sensor. + */ +void Ultrasonic_edgeProcessing(void){ + + g_countOfEdges++; + + if(g_countOfEdges == 1){ + + /* Clear the timer counter register */ + Icu_clearTimerValue(); + /* change the edge detection to be falling edge */ + Icu_setEdgeDetectionType(FALLING); + } + + else if(g_countOfEdges == 2){ + + /* get the high time */ + g_echo = Icu_getInputCaptureValue(); + + /* Clear the timer counter register */ + Icu_clearTimerValue(); + + /* change the edge detection to be rising edge */ + Icu_setEdgeDetectionType(RISING); + + } +} + diff --git a/HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.h b/HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.h new file mode 100644 index 0000000..1cfbfcf --- /dev/null +++ b/HC-SRO4 - Ultrasonic/src/HC-SRO4_ultrasonic.h @@ -0,0 +1,66 @@ + /****************************************************************************** + * + * Module: HC-SRO4 Ultrasonic + * File Name: HC-SRO4_ultrasonic.h + * Description: Header file for the HC-SR04 Ultrasonic driver + * Author: Ibrahim Mohamed + * + *******************************************************************************/ + + +#ifndef SRC_DRIVERS_HC_SRO4_ULTRASONIC_H_ +#define SRC_DRIVERS_HC_SRO4_ULTRASONIC_H_ + + +#include "std_types.h" + +/******************************************************************************* + * Configurations * + *******************************************************************************/ + +#define ULTRASONIC_TRIGGER_PORT PORTB_ID +#define ULTRASONIC_TRIGGER_PIN PIN5_ID + +#define ULTRASONIC_ECHO_PORT PORTD_ID +#define ULTASONIC_EHCO_PIN PIN6_ID + + +/******************************************************************************* + * Types Declaration * + *******************************************************************************/ + + +/******************************************************************************* + * Functions Prototypes * + *******************************************************************************/ + +/* + * Description : + * Initialize the ICU driver. + * Setup the ICU call back function. + * Setup the direction for the trigger pin as output pin through the GPIO driver. + */ +void Ultrasonic_init(void); + +/* + * Description : + * Send the Trigger pulse to the ultrasonic. + */ +void Ultrasonic_Trigger(void); + +/* + * Description : + * Send the trigger pulse by using Ultrasonic_Trigger function. + * Start the measurements by the ICU from this moment. + */ +uint16 Ultrasonic_readDistance(void); + +/* + * Description : + * This is the call back function called by the ICU driver. + * This is used to calculate the high time (pulse time) generated by the ultrasonic sensor. + */ +void Ultrasonic_edgeProcessing(void); + + +#endif /* SRC_DRIVERS_HC_SRO4_ULTRASONIC_H_ */ diff --git a/ICU/.cproject b/ICU/.cproject new file mode 100644 index 0000000..163afdf --- /dev/null +++ b/ICU/.cproject @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ICU/.project b/ICU/.project new file mode 100644 index 0000000..038fe8d --- /dev/null +++ b/ICU/.project @@ -0,0 +1,27 @@ + + + ICU + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + de.innot.avreclipse.core.avrnature + + diff --git a/ICU/.settings/de.innot.avreclipse.core.prefs b/ICU/.settings/de.innot.avreclipse.core.prefs new file mode 100644 index 0000000..6139929 --- /dev/null +++ b/ICU/.settings/de.innot.avreclipse.core.prefs @@ -0,0 +1,7 @@ +avrtarget/ClockFrequency=1000000 +avrtarget/ExtRAMSize=0 +avrtarget/ExtendedRAM=false +avrtarget/MCUType=atmega32 +avrtarget/UseEEPROM=false +avrtarget/UseExtendedRAMforHeap=true +eclipse.preferences.version=1 diff --git a/ICU/.settings/org.eclipse.core.resources.prefs b/ICU/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/ICU/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/ICU/src/icu.c b/ICU/src/icu.c new file mode 100644 index 0000000..d790471 --- /dev/null +++ b/ICU/src/icu.c @@ -0,0 +1,123 @@ + /****************************************************************************** + * + * Module: ICU + * File Name: icu.c + * Description: Source file for the AVR ICU driver + * Author: Ibrahim Mohamed + * + *******************************************************************************/ + +#include "icu.h" +#include "common_macros.h" +#include +#include + +/******************************************************************************* + * Global Variables * + *******************************************************************************/ + +/* Global variables to hold the address of the call back function in the application */ +static volatile void (*g_callBackPtr)(void) = NULL_PTR; + +/******************************************************************************* + * Interrupt Service Routines * + *******************************************************************************/ + +ISR(TIMER1_CAPT_vect) +{ + if(g_callBackPtr != NULL_PTR) + { + /* Call the Call Back function in the application after the edge is detected */ + (*g_callBackPtr)(); + } +} + +/******************************************************************************* + * Functions Definitions * + *******************************************************************************/ +/* + * Description : Function to initialize the ICU driver + * 1. Set the required clock. + * 2. Set the required edge detection. + * 3. Enable the Input Capture Interrupt. + * 4. Initialize Timer1 Registers + */ +void Icu_init(const Icu_ConfigType * Config_Ptr) +{ + /* Configure ICP1/PD6 as i/p pin */ + DDRD &= ~(1<clock); + /* + * insert the required edge type in ICES1 bit in TCCR1B Register + */ + TCCR1B = (TCCR1B & 0xBF) | ((Config_Ptr->edge)<<6); + + /* Initial Value for Timer1 */ + TCNT1 = 0; + + /* Initial Value for the input capture register */ + ICR1 = 0; + + /* Enable the Input Capture interrupt to generate an interrupt when edge is detected on ICP1/PD6 pin */ + TIMSK |= (1<