589
/* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stdio.h" #include "string.h" /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define TRIG_PIN GPIO_PIN_10 #define TRIG_PORT GPIOA /* USER CODE END PD */ /* Private variables ---------------------------------------------------------*/ TIM_HandleTypeDef htim1; UART_HandleTypeDef huart1; DMA_HandleTypeDef hdma_usart1_tx; /* USER CODE BEGIN PV */ uint32_t IC_Val1 = 0; uint32_t IC_Val2 = 0; uint32_t Difference = 0; uint8_t Is_First_Captured = 0; // is the first value captured ? uint8_t Distance = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_DMA_Init(void); static void MX_TIM1_Init(void); static void MX_USART1_UART_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) // if the interrupt source is channel1 { if (Is_First_Captured==0) // if the first value is not captured { IC_Val1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); // read the first value Is_First_Captured = 1; // set the first captured as true // Now change the polarity to falling edge __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_FALLING); } else if (Is_First_Captured==1) // if the first is already captured { IC_Val2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); // read second value __HAL_TIM_SET_COUNTER(htim, 0); // reset the counter if (IC_Val2 > IC_Val1) { Difference = IC_Val2-IC_Val1; } else if (IC_Val1 > IC_Val2) { Difference = (0xffff - IC_Val1) + IC_Val2; } Distance = Difference * (0.034/2); Is_First_Captured = 0; // set it back to false // set polarity to rising edge __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_RISING); __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_CC2); } } } void HCSR04_Read (void) { HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET); // pull the TRIG pin HIGH HAL_Delay(10); // wait for 10 us HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET); // pull the TRIG pin low __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_CC2); } /* USER CODE END 0 */ int main(void) { /* USER CODE BEGIN 1 */ char MSG[40]; /* USER CODE END 1 */ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_TIM1_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_2); /* USER CODE END 2 */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HCSR04_Read(); sprintf(MSG,"Distance : %d cm\n\r",Distance); HAL_UART_Transmit(&huart1,(uint8_t*)MSG, strlen(MSG),100); HAL_Delay(500); } /* USER CODE END 3 */ }