There are many ways for STM32 to measure the frequency of external input signals:
Use the internal timer input capture function.
Use ordinary IO port to set external interrupt + timer to measure the frequency of PWM signal.
Of these two methods, the first one is recommended, as it uses internal resources to save CPU resources.
Of course, when the internal resources are insufficient, or when the corresponding pins are not connected during hardware circuit design, only the second method can be used.
Due to the inadequacy of hardware circuit design, the pins for measuring PWM input signals were not connected to the corresponding channels. The second method was used:
Note: The priority of the timer interrupt here is higher than the priority of the external interrupt
The idea is as follows:
Set the PWM input signal pin to external interrupt mode, and the trigger mode is GPIO_MODE_IT_RISING_FALLING. Both rising and falling edges can be triggered.
Next, enable a timer TIM4, and set the timer interrupt time according to the frequency you need to measure. (This time it is set to 2us –> the maximum measurable frequency is 50KHz)
In the external interrupt, when the rising edge arrives, the counter TIM4->CNT=0 is cleared, and the rising edge flag is set to 1, which represents the start of calculating the PWM time tim4_PWM_cnt++.
When a falling edge arrives, set a falling edge flag to 1.
When the next rising edge comes, check whether the previous one was a falling edge. If yes, it means that the time of one PWM cycle has arrived. Read the time. PWM_Cycle = timer4_PWM_cnt*2. And clear the count variable tim4_PWM_cnt = 0
Then the next falling edge comes, and it is determined whether the previous one was a rising edge. If it is, it means that a cycle of high level has ended, and the time is read, which is the pulse width PWM_Duty = timer4_PWM_cnt*2.
The specific C language implementation code is as follows:
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_11) != RESET)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11); //Call interrupt handling common function
if(PWM_EN()==1) // rising edge trigger
{
TIM4->CNT=0; // Clear TIM4 count here. Restart counting
if((PWM_FLAG_DOWM==1)&&(PWM_FLAG_UP==2))//Judge whether the last time was a falling edge
{
PWM_Cycle = timer4_PWM_cnt*2;
timer4_PWM_cnt=0;
}
PWM_FLAG_CNT = 1;
PWM_FLAG_UP = 1; // rising edge flag
PWM_FLAG_DOWM = 2; // rising edge flag
}
else //Falling edge trigger
{
if((PWM_FLAG_DOWM==2)&&(PWM_FLAG_UP == 1))//The falling edge determines whether the previous state is a rising edge
{
PWM_Duty = timer4_PWM_cnt*2;
}
PWM_FLAG_DOWM=1; //Falling edge flag
PWM_FLAG_UP=2; //Falling edge flag
}
void TIM4_Init(u16 arr,u16 psc)
{
TIM4_Handler.Instance=TIM4; //General timer 4
TIM4_Handler.Init.Prescaler=psc; //Frequency division coefficient
TIM4_Handler.Init.CounterMode=TIM_COUNTERMODE_UP; //Up counter
TIM4_Handler.Init.Period=arr; //Automatically load value
TIM4_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1; //Clock division factor
HAL_TIM_Base_Init(&TIM4_Handler);
HAL_TIM_Base_Start_IT(&TIM4_Handler); //Enable timer 4 and timer 4 update interrupt: TIM_IT_UPDATE
}
void TIM4_IRQHandler(void)
{
HAL_TIM_IRQHandler(&TIM4_Handler);
if(PWM_FLAG_CNT == 1) //PWM rising edge arrival flag
{
timer4_PWM_cnt++;
}
}
Previous article:Using STM32 general-purpose timer to realize output of two complementary PWM with adjustable duty cycle and frequency
Next article:STM32 Study Notes - PWM Waveform Output
Recommended ReadingLatest update time:2024-11-16 14:29
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Intelligent Control Technology of Permanent Magnet Synchronous Motor (Written by Wang Jun)
- usb_host_device_code
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [Open Source] Bluetooth module BT401 full set of information based on Bluetooth headset chip development ultra-low cost
- EEWORLD University Hall----Live Replay: ADI's Vital Signs Monitoring Solutions in Wearable Products
- Arrow Electronics' award-winning live broadcast starts at 10:00 this morning: Intel FPGA Deep Learning Acceleration Technology
- stm32f030R8 Chinese reference material
- Recruitment: Executive Vice President of Automotive Electronics (Executive Vice President of New Energy)
- ATmega4809 Curiosity Nano Review + Unboxing and Powering on
- Sell an unused Jiuxintaike oscilloscope and get a Guwei desktop multimeter for free
- MSP430F1232 external 4M crystal oscillator cannot start
- PCB connector connection method
- Using STM32F103 as Arduino