This chapter talks about the use of stm32 timer TIM_GetCounter (TIMx). This has been used in previous projects to obtain the duration of high and low levels, and has been used in ultrasonic ranging and infrared remote control decoding. Except for 1 and 8, the other timers of stm32 are general timers. How to use this function to capture the duration of the level? First, let's look at the initialization content, taking TIM4 as an example
void Timer4_CFG()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitStructure.GPIO_Pin = IR_LED_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(IR_LED_PORT, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 3599; //TIM_CKD_DIV1
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseStructure.TIM_Period = 200; // ARR
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DI V1;
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//TIM_PrescalerConfig(TIM4,3599,TIM_PSCReloadMode_Immediate);//72MHz/(3599+1)=20 000 HZ 50uS
TIM_ARRPreloadConf ig(TIM4,DISABLE);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM4,TIM_IT_Trigger,ENABLE);
TIM_Cmd(TIM4, DISABLE);
}
APB1 is a 36M clock, which generates a 20KHZ 50us period waveform after frequency division. TIM->CNT is a counter. There is a certain rule when calculating the value of TIM_TimeBaseStructure.TIM_Period here. That is, you need to know the maximum high and low levels you collected. For example, the longest level in the waveform I collected is 4ms, and the base timer is 50us. Then I have to calculate 80 to reach 4ms, so the value of TIM_TimeBaseStructure.TIM_Period must be greater than 80, otherwise it will go wrong. Here I want to remind everyone that when the value of TIM-CNT reaches the value of TIM_TimeBaseStructure.TIM_Period, it will be cleared and counted again as shown in the figure below. This is the register configuration method for obtaining high and low levels using the TIMX-CNT counter of the timer. Of course, cnt=TIM_GetCounter(TIMx); cnt must be multiplied by 50us to get the real time. If you don't use a counter, and want to use a global variable in the interrupt to represent the number of interrupts to calculate the duration of high and low levels, please note that this method may be inaccurate due to the large number of program functions. You can try it when there are fewer functions. Then the configuration of the timer must be changed again. The benchmark is still 50us. TIM_TimeBaseStructure.TIM_Prescaler =3599; //TIM_CKD_DIV1 At this time, the maximum value cannot be considered, but a minimum common divisor should be considered. For example, how many high and low levels do I want to collect? There are 8ms, 4ms, 1ms, and 500us. Then the value of TIM_TimeBaseStructure.TIM_Period has requirements. As long as it is a multiple of 50us and is less than 500us or its divisor after multiplying with 50us, it will be fine. For example, TIM_TimeBaseStructure.TIM_Prescaler = 3599; // TIM_TimeBaseStructure.TIM_Period=1; // These two sentences mean that an interrupt occurs every 50us.
Previous article:How to write data to the flash of stm32 and use it as eerom
Next article:stm32 timer 2 outputs 10KHZ waveform
Recommended ReadingLatest update time:2024-11-16 13:55
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
- CC2640 CC1310 high and low temperature test
- Detailed explanation of TL494 switching power supply circuit
- STM32MP157A-DK1 Evaluation + IIO and ADC (6)
- 【Qinheng RISC-V core CH582】 5 Bluetooth routine initial evaluation and environment construction
- RISC-V MCU IDE MRS (MounRiver Studio) development: Solve the problem of RAM usage showing 100% after compilation
- FPGA Learning - Introduction to Verilog
- Let's take a look at the price increase rhythm of 10 major chip manufacturers, starting a new round of price increases
- Share a learning material: Comic Semiconductor
- Are POE switches or ordinary switches more commonly used in surveillance and security?
- Would you dare to sit in a car without a steering wheel? The United States allows self-driving cars without steering wheels