1. Title
Ultra Sonic Sensor using UART, GPIO
2. Category
"STM32", "Firmware"
3. Key Concepts
"Ultrasonic Sensor"
To use an ultrasonic sensor with STM32, you need "Trigger", "ECHO" Pin. Trigger generates a toggling pulse for 10 microseconds. ECHO captures the rising and falling edges of the signal from Ultrasonic sensor.
The HAL_TIM_IC_CaptureCallback function captures the rising and falling edges of the ECHO signal using input capture on timer, storing the pulse width in the distance variable.
The distance is then calculated by converting the pulse width to centimeters. The ultrasonic_processing function periodically triggers the sensor and processes the captured distance when a valid measurement is completed.
4. Setup
5. Code Review
void make_trigger(void)
{
HAL_GPIO_WritePin(TRIG_PIN_GPIO_Port, TRIG_PIN_Pin, GPIO_PIN_RESET);
delay_us(2);
HAL_GPIO_WritePin(TRIG_PIN_GPIO_Port, TRIG_PIN_Pin, GPIO_PIN_SET);
delay_us(10);
HAL_GPIO_WritePin(TRIG_PIN_GPIO_Port, TRIG_PIN_Pin, GPIO_PIN_RESET);
}
make_trigger function generates a 10-microsecond pulse on the TRIG_PIN by first setting it low, then high for 10 microseconds, and finally setting it low again to trigger the ultrasonic sensor.
volatile uint8_t is_first_capture = 0; // 0 : RISING EDGE, 1: FALLING EDGE
volatile int distance = 0; // 거리 측정한 PULSE 갯수를 저장하는 변수.
volatile int ic_cpt_finish_flag = 0; // 초음파 거리 측정 완료 되었는지 확인하기 위한 FLAG
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) // ECHO의 RISING EDGE, FALLING EDGE 시, 이 함수로 진입한다.
{
if(htim->Instance == TIM1)
{
if(is_first_capture == 0) // RISING EDGE?
{
__HAL_TIM_SET_COUNTER(htim, 0); // COUNTER 초기화
is_first_capture = 1; // RISING EDGE를 만났다는 FLAG 변수를 SET.
}
else if(is_first_capture == 1) // FALLING EDGE?
{
is_first_capture = 0; // 다음 ECHO PULSE를 COUNT 하기 위해 CLEAR.
distance = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
ic_cpt_finish_flag = 1; // 초음파 거리 측정 완료
}
}
}
HAL_TIM_IC_CaptureCallback function captures the rising and falling edges of the ECHO pulse from the ultrasonic sensor.
It uses a flag (is_first_capture) to differentiate between the rising edge (where the timer counter is reset) and the falling edge (where the distance is calculated by reading the captured timer value).
After measuring the pulse width, the ic_cpt_finish_flag is set to indicate the completion of the ultrasonic distance measurement.
void ultrasonic_processing(void)
{
int dis = 0;
char lcd_buff[20];
if(TIM11_Ultrasonic >= 1000)
{
TIM11_Ultrasonic = 0;
make_trigger();
if(ic_cpt_finish_flag)
{
ic_cpt_finish_flag = 0;
dis = distance;
dis = dis * 0.034 / 2; // 초음파는 1us당 0.034sec를 이동한다. ppt 참조.
printf("DISTANCE : %d cm\n\n", dis);
}
}
}
ultrasonic_processing function checks the TIM11_Ultrasonic variable to determine if enough time has passed to trigger the ultrasonic sensor. (1ms between TRIGGER)
It then calls make_trigger to send the ultrasonic pulse and waits for the captured distance to be calculated.
If the distance measurement is complete (ic_cpt_finish_flag is set), it converts the pulse duration into a distance in centimeters, prints it to the console.
6. Testing and Debugging
* Testing Tools: STM32 CUBE IDE, NUCLEO-F411RE, Putty(UART)
'AI SOC COURSE > FIRMWARE' 카테고리의 다른 글
Firmware Course Review - 05. Bluetooth (UART) (0) | 2025.01.09 |
---|---|
Firmware Course Review - 04. PWM (DC Motor) (0) | 2025.01.09 |
Firmware Course Review - 03.1. I2C (0) | 2025.01.09 |
Firmware Course Review - 02.3. One-Wire Communication (DHT-11) (0) | 2025.01.09 |
Firmware Course Review - 02.2. UART (0) | 2025.01.09 |