-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic.c
67 lines (57 loc) · 2.26 KB
/
ultrasonic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*****************************************************************************************************************************
********************************** Author : Ehab Magdy Abdullah *************************************
********************************** Linkedin: https://www.linkedin.com/in/ehabmagdyy/ *************************************
********************************** Youtube : https://www.youtube.com/@EhabMagdyy *************************************
******************************************************************************************************************************/
#include "ultrasonic.h"
extern TIM_HandleTypeDef htim2;
extern uint16_t distance;
extern uint8_t isReadingFinished;
static volatile uint8_t isRisingCaptured = 0;
static volatile uint32_t IC_Value1 = 0;
static volatile uint32_t IC_Value2 = 0;
static volatile uint32_t IC_Difference = 0;
static void Delay_10US(void)
{
__HAL_TIM_SET_COUNTER(ULTRASONIC_ECHO_PIN_IC, 0);
/* TIMER Tick time = 1us */
while(__HAL_TIM_GET_COUNTER(ULTRASONIC_ECHO_PIN_IC) < 10);
}
void Ultrasonic_Get_Distance(void)
{
/* Send Trigger Signal to the Ultrasonic Trigger Pin */
HAL_GPIO_WritePin(ULTRASONUC_TRIGGER_PORT, ULTRASONIC_TRIGGER_PIN, GPIO_PIN_SET);
Delay_10US();
HAL_GPIO_WritePin(ULTRASONUC_TRIGGER_PORT, ULTRASONIC_TRIGGER_PIN, GPIO_PIN_RESET);
__HAL_TIM_ENABLE_IT(ULTRASONIC_ECHO_PIN_IC, TIM_IT_CC1);
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* Capture rising edge */
if(0 == isRisingCaptured)
{
IC_Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
isRisingCaptured = 1;
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);
}
/* Capture falling edge */
else if(1 == isRisingCaptured)
{
IC_Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
__HAL_TIM_SET_COUNTER(htim, 0);
if(IC_Value2 > IC_Value1)
{
IC_Difference = IC_Value2 - IC_Value1;
}
else if(IC_Value1 > IC_Value2)
{
IC_Difference = (0xFFFF - IC_Value1) + IC_Value2;
}
distance = IC_Difference * 0.0173;
isReadingFinished = 1;
isRisingCaptured = 0;
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
__HAL_TIM_DISABLE_IT(&htim2, TIM_IT_CC1);
}
else{ /* Nothing */ }
}