1. Clock part, the clock frequency of TIMER2 is initialized to 36M.
RCC_PCLK1Config(RCC_HCLK_Div2); //Set low speed AHB clock = system clock/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // Frequency multiplication factor is 9 PLLCLK="8"*9=72
2.TIMER initialization
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 18;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV4;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //Reset mode to slave mode
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); //Enable master-slave mode
TIM_Cmd(TIM2, ENABLE); //Enable TIM2 counter
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); //Enable CC2 interrupt request
3. TIMER interrupt processing
void TIM2_IRQHandler(void)
{
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
/* Get the Input Capture value */
IC2_Value = TIM_GetCapture2(TIM2);
}
Note:
I don't know why, but after the STM32 TIMER clock is divided, the accuracy error of the clock increases with the increase of the division value. Without division, I detected the 1KHz PWM input signal, and the error was 0.5us. With 18 division, the error is unbearable. When measuring 100Hz, it is calculated based on the captured value to be 105Hz.
It is indeed as my STM32 friend said. I misunderstood the frequency division. The frequency division value = TIM_TimeBaseStructure.TIM_Prescaler + 1. So if you want to divide by 18, just set TIM_Prescaler = 17. After this modification, the measurement error is within 5/10,000. Thank you STM32 friend for your reminder!! (I will not delete the above misunderstanding, but keep it as a warning for the future, haha!)
Previous article:How to debug STM32 in ram and flash under ulink-Jlink
Next article:STM32 study notes: Building FWlib 3.0 project under IAR
Recommended ReadingLatest update time:2024-11-16 16:17
- 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
- DSP C language IIR filter program source code
- 【Smart network desk lamp】8. Maix bit driver RGB
- How does GD32 replace STM32?
- Starting from 0, learn the 10 terms you need to know about PCB
- [Evaluation of domestic FPGA Gaoyun GW1N-4 series development board]——11. Click on the LCD1602 screen
- Back to Basics: Exploring the Benefits of Affordable Bluetooth Low Energy
- Based on TMS570/RM4X LaunchPad GIO simulate SPI to light up OLED
- Weekly review information~
- RF Circuit Engineering Design
- Vivado 2018.3 Software Download