Everyone knows that stm32f4 has rich internal resources and powerful external peripherals. It has two 32-bit counters, which is very good for many projects that need to count pulses. It not only saves the expansion of external counters and saves PCB space, but also makes program design much simpler. Since I have just come into contact with the f4 series of microcontrollers, I just think it is very powerful. I also encountered problems in counting pulses with 32-bit counters. I also searched a lot of information on the Internet, but there are no specific routines for reference. I don’t have a deep understanding of registers, but I like library functions. Finally, after several days of hard thinking and many practices, I finally succeeded. The configuration file is posted as follows:
void TIM2_Int_Init(u32 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); /
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//PA0ÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
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_DOWN;//
GPIO_Init(GPIOA, &GPIO_InitStructure);/
GPIO_PinAFConfig(GPIOA,GPIO_PinSource15,GPIO_AF_TIM2); //
TIM_TimeBaseInitStructure.TIM_Period = arr; //
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//
TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
TIM_CounterModeConfig(TIM2,TIM_CounterMode_Up);
TIM_SetCounter(TIM2,0);//
TIM_Cmd(TIM2,ENABLE); //
}
The following is a function that sets timer 4 to read the count value for 1 second and prints it through the serial port:
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)==SET) /
{
counter=TIM_GetCounter(TIM2);//Read the count value
printf("Fre:%32d ",counter);
TIM_SetCounter(TIM2,0);//Set the initial count value
counter=TIM_GetCounter(TIM2);//Confirm that the initial count value is 0
printf("Fre:%32d ",counter);
flag_1s=1;
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //
}
But in the end, I found that the counter counts inaccurately during actual testing. The larger the frequency, the greater the deviation. The value printed at 100KHz is about 99K, and at 1KHz it prints about 995. However, the count value can exceed 750KHz (actually measured). This is related to the automatic reload value and pre-scaling when the timer is initialized. I hope this helps you.
Previous article:STM32CubeMX: ETR external pulse counter
Next article:Use of STM32 counter
- Popular Resources
- Popular amplifiers
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
- [National Technology Low Power Series N32L43x Review] 08. Software and Hardware I2C Driver 1.5-inch 16-color grayscale OLED display
- Free application: Support Linux RISC-V development board, Sipeed LicheeRV 86
- Please advise on MOS tube driver issues
- Today at 10:00 AM, there will be a prize live broadcast: [ADI will meet you for TOF (Time Of Flight) technology introduction and product applications]
- Second-order filter, how to ensure that the data does not overflow when performing fixed-point operations?
- GD32E231 DIY Competition (7) - Human infrared sensor module and music IC driver completed
- How to improve the sampling accuracy of GD32 on-chip AD
- Deadbeat Control Inverter Based on DSP
- Found a free IoT platform that supports access to Tmall Genie
- About the problem of data transmission between AD7768 evaluation board and sdp-h1 board