#include "cj.h"
#include "stm32f10x.h"
#include "delay.h"
#include "usart.h"
unsigned int overcount=0;
void NVIC_Config(void)//priority setting
{
NVIC_InitTypeDef NVIC_InitStructer;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructer.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructer.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructer.NVIC_IRQChannel=TIM2_IRQn;//
NVIC_InitStructer.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructer);
}
void CH_SR04_Init(void) //Ultrasonic module initialization
{
GPIO_InitTypeDef GPIO_InitStructer;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructer;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructer.GPIO_Pin=GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructer); //Set PB8 as the trigger pin
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructer.GPIO_Pin=GPIO_Pin_9;
GPIO_Init(GPIOB,&GPIO_InitStructer); //Set PB9 as the receiving pin
TIM_DeInit(TIM2);
TIM_TimeBaseInitStructer.TIM_Period=65534;
TIM_TimeBaseInitStructer.TIM_Prescaler=71; //After pre-scaling, the value of the counter increases by one every 0.5us.
TIM_TimeBaseInitStructer.TIM_ClockDivision=TIM_CKD_DIV1;//
TIM_TimeBaseInitStructer.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructer);
//TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC1,ENABLE);//
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//
NVIC_Config();
TIM_Cmd(TIM2,DISABLE);//
}
float Senor_Using(void) //Distance measurement function
{
float length=0,sum=0;
u16 tim;
unsigned int i=0;
while(i!=5)
{
PBout(8)=1; //
delay_us(20); //
PBout(8)=0;
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==RESET);
TIM_Cmd(TIM2,ENABLE);//
i+=1;
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==SET);//
TIM_Cmd(TIM2,DISABLE);//
tim = TIM_GetCounter (TIM2); //
length=(tim+overcount*1000)/58.0;//
sum=length+sum;
TIM2->CNT=0; //
overcount=0; //
delay_ms(100);
}
length=sum/5;
return length;//
}
void TIM2_IRQHandler(void) //
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//
overcount++;
}
}
Since it is copied and pasted, only some parts are commented. In fact, it is very simple. It is to output a high level of more than 10us as a pulse signal through a pin, and set a pin as a receiving pin. When a high level is received, the timer counts, and when a low level is detected, the count stops. Take out the value and calculate the distance through the formula. Finally, the serial port output, directly #include "stdio.h" and then use pirntf. But don't forget to configure the compiler before that. Please Baidu it yourself. It's very simple.
Previous article:About the programming of matrix buttons of STM32F103 model
Next article:PWM frequency and duty cycle setting in stm32
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- The Bluetooth signal problem has finally come to an end.
- [HC32F460 Development Board Review] (Part 1) First acquaintance with the HC32F460 Development Board
- EEWORLD University ----TI DLP? Labs - Technology
- 【Development related】Detection principles and advantages of laser gas sensors
- IAR STM8 software has been updated to a new version, and the following prompt appears. Does anyone know how to solve it?
- [Bluesight AB32VG1 RISC-V board "meets" RTT] Audio starts
- Why Python is the programming language of the future
- [SAMR21 new gameplay] 7. Use webusb
- How does the Zigbee module connect to third-party gateways such as Philips Hue, Amazon alexa, etc. What is the standard protocol?
- 【Infineon XENSIV PAS CO2 sensor】Arduino routine test