Use the STM32 timer input capture module to control 3-way ultrasonic sensors. The
ultrasonic sensor used this time is the common HC-SR04, which is often used in the obstacle avoidance system of small robots and smart cars.
In the above figure, 5v and GND provide power to the module, Trig is used to trigger the module to measure distance, and Echo is used to receive the return level signal.
Its operation timing diagram is as follows:
As shown in the above figure, STM32 gives the Trig pin a high level of more than 10us to enable the ranging circuit inside the module. The module will cyclically send out 8 40kHz pulses and emit ultrasonic waves. Then, by detecting the high level time of the Echo pin, the distance between the module and the obstacle can be measured. Its calculation formula can be expressed as follows:
distance=340∗Echo high level time 2distance=340∗Echo high level time 2
The module driver is as follows:
void HC_SR04_Init(void)
{
/*Initialize GPIO*/
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable GPIOA clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //Enable timer 2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6; //Trig:PA4,PA5,PA6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; //Echo: PA2,PA3 correspond to channels 3,4 of TIM2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Turn on pin multiplexing
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM2);
GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize GPIOA
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//Enable GPIOB's Clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PB3 refers to TIM2's channel 2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_TIM2);
GPIO_Init(GPIOB, &GPIO_InitStructure);//initialize GPIOB
/Initialize TIM2*/
TIM_TimeBaseStructure.TIM_Period = 0xffffffff; //The maximum detection distance of the sensor is 4000mm, which generally does not overflow
TIM_TimeBaseStructure.TIM_Prescaler =84-1; //Set TIM2 clock frequency to 1MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//Initialize TIM2 input capture
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //Both rising and falling trigger input capture
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
//Set up input capture interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);//使能中断
TIM_Cmd(TIM2,ENABLE);
dist.overflow=0;
}
It is worth noting that the edge polarity of the input capture here is set to capture both the rising and falling edges, which is more efficient. Some experts also start to use rising edge capture, and then set the capture memory to falling edge capture in the interrupt. I have tried this method, and the program is prone to jamming, so I did not use it.
The interrupt program can be written like this:
void TIM2_IRQHandler(void)
{
static uint16_t cnt_f,cnt_l,cnt_r;
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
{
dist.overflow++;
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
// printf("overflow\n");
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC2)!=RESET)
{
cnt_f++;
if(cnt_f%2==1)//A rising level is detected
{
TIM2->CNT=0; //Counter clear
}
else //A falling edge level is detected, and the distance value is read
{
dist.cnt=TIM2->CNT;
TIM2->CNT=0;
dist.f_distance=(dist.overflow*0xffffffff+dist.cnt)*0.17f;
printf("F_dis=%.2fmm\n",dist.f_distance);
dist.overflow=0;
}
if(cnt_f>65535)cnt_f=0;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC3)!=RESET)
{
cnt_l++;
if(cnt_l%2==1)//A rising level is detected
{
TIM2->CNT=0;
}
else
{
dist.cnt=TIM2->CNT;
TIM2->CNT=0;
dist.l_distance=(dist.overflow*0xffffffff+dist.cnt)*0.17f;
printf("L_dis=%.2fmm\n",dist.l_distance);
dist.overflow=0;
}
if(cnt_l>65535)cnt_l=0;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC4)!=RESET)
{
cnt_r++;
if(cnt_r%2==1)
{
TIM2->CNT=0;
}
else
{
dist.cnt=TIM2->CNT;
TIM2->CNT=0;
dist.r_distance=(dist.overflow*0xffffffff+dist.cnt)*0.17f;
printf("R_dis=%.2fmm\n",dist.r_distance);
dist.overflow=0;
}
if(cnt_r>65535)cnt_r=0;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
}
}
The advantage of this design is that the interrupt program design is simple and can ensure real-time performance.
The module is triggered to measure by giving the module's Trig pin a high-level signal of more than 10us to trigger the module to measure the distance. The program code is as follows:
/*
function: Start distance measurement
Choice: 0: Left ultrasonic module
1: Middle ultrasonic module
2: Right ultrasonic module
*/
void Get_Distance(uint8_t choice)
{
switch(choice)
{
case 0:
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
Delay_us(15);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
break;
case 1:
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
GPIO_SetBits(GPIOA,GPIO_Pin_5);
Delay_us(15);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
break;
case 2:
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
GPIO_SetBits(GPIOA,GPIO_Pin_6);
Delay_us(15);
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
break;
}
}
Previous article:stm32 startup mode + pull-up (pull-down) resistor two-in-one
Next article:The principle and function of crystal oscillator in STM32
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- [LSM6DSOX finite state machine routine learning 2]--How to use finite state machine programming
- FPGA Implementation of DMA Transfer Mode of PCI Interface
- The relationship between capacitance and frequency
- 【MYS-8MMX】Part 4: Bluetooth speaker plays music
- RS485 chip SN75LBC184 burned out
- Simple smart home system design based on Arduino and OneNET cloud platform
- Programmable Power Supply Technology and Application Guide (6) - Reducing Noise from Power Supply to DUT
- 【GD32L233C-START Review】- Summary of the initial review
- Design of heterogeneous pads in AD9
- Zigbee based on CC2430 enables terminal devices to access the network based on a specific PAN ID