a) Purpose: Basic PWM input is also called capture, and interrupt coordination application. Use the output pin PB1 (pin 19) in the previous chapter, directly use a jumper to connect the input PA3 (pin 13), configure it as TIM2_CH4, and conduct experiments.
b) For simple PWM input applications, there is no need to consider the difference in the advanced functions of TIM1 for the time being. According to my current application goal, I only need to collect the high-level width, and I don’t need to know the period, so I don’t use the PWM input mode, but the ordinary pulse width capture mode.
c) Initialization function definition:
void TIM_Configuration(void); //Define TIM initialization function
d) Initialization function call:
TIM_Configuration(); //TIM initialization function call
e) Initialization function, different from the previous module, TIM's CAP initialization is divided into three parts - timer basic initialization, channel initialization and clock start initialization:
void TIM_Configuration(void) //TIM2's CAP initialization function
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //Timer initialization structure
TIM_ICInitTypeDef TIM_ICInitStructure; //Channel input initialization structure
//TIM2 output initialization
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //Period 0~FFFF
TIM_TimeBaseStructure.TIM_Prescaler = 5; //Clock division
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Clock division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Mode
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //Basic initialization
//Capture initialization of TIM2 channel
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; //Channel selection
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //Falling edge
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //Correspondence between pins and registers
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //Frequency divider
TIM_ICInitStructure.TIM_ICFilter = 0x4; //Filter setting, after several cycles of jumps, it is determined that the waveform is stable at 0x0~0xF
TIM_ICInit(TIM2, &TIM_ICInitStructure); //InitializeTIM_SelectInputTrigger
(TIM2, TIM_TS_TI2FP2); //Select the clock trigger sourceTIM_SelectSlaveMode
(TIM2, TIM_SlaveMode_Reset); //Trigger modeTIM_SelectMasterSlaveMode
(TIM2, TIM_MasterSlaveMode_Enable); //Start the passive trigger of the
timerTIM_ITConfig(TIM2, TIM_IT_CC4, ENABLE); //Open the interruptTIM_Cmd
(TIM2, ENABLE); //Start TIM2
}
f) Add TIM clock to the RCC initialization function:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM3, ENABLE);
g) Set the input and output pin modes in GPIO. IN_FLOATING, 50MHz.
h) If using interrupts, add the following code in NVIC:
// Enable TIM interrupts (same as in the previous chapter)
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio rity = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
i) Simple application:
variable = TIM_GetCapture4(TIM2);
j) Notes:
i. Since my needs are only related to the high level width, I avoided using the PWM input mode, so that each pin can capture one signal. If the PWM mode is used, each channel needs to occupy two registers, so a timer can only use two PWM inputs at the same time.
ii. Since the capture needs to trigger the start timer, it is not easy to implement PWM output and capture on the same TIM channel. If necessary, only the relevant code for count overflow can be added.
iii. Some programs omit the initialization code of the capture channel, which is wrongiv
. In the basic timer initialization code, pay attention to selecting the appropriate counter length. It is best to make the waveform length not longer than one counting cycle, otherwise it will be troublesome to add overflow code. The calculation of the length of a counting cycle is related to the following parameters:
(1) RCC_PCLKxConfig in the RCC initialization code, which is the relationship between the basic clock source of the TIM and the system clock.
(2) TIM_Period initialized by TIM, which is the value of the counting cycle
(3) TIM_Prescaler initialized by TIM, which is a multiplier counter of the counting cycle, equivalent to adjusting the counting cycle, which can make TIM_Period as large as possible and improve the counting accuracy.
Keywords:STM32
Reference address:STM32 Note 11: Capture wonderful moments, pulse square wave length capture
b)
c)
void TIM_Configuration(void);
d)
TIM_Configuration();
e)
void TIM_Configuration(void) //TIM2's CAP initialization function
{
//TIM2 output initialization
//Capture initialization of TIM2 channel
}
f)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM3, ENABLE);
g)
h)
// Enable TIM interrupts (same as in the previous chapter)
i)
variable = TIM_GetCapture4(TIM2);
j)
i.
ii.
iii.
.
(1)
(2)
(3)
Previous article:sw's STM32 Notes 10: Work, PWM output
Next article:STM32 Note 12: The clock never stops working, systic clock application
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- 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)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
Guess you like
- Antenna basic element
- ATA-2021B high voltage amplifier, the current monitoring/voltage bias you want is here!
- Cisco was reported to have laid off employees in China, and the average compensation for laid-off employees was more than 1 million
- How to build and run the RFID design platform
- Reverse
- Understanding the Rail-to-Rail Characteristics of Op Amps
- Married electricians, do you give your salary to your wife or manage it yourself?
- Ek314 How to upgrade Ubuntu 12.04 to 14.04?
- Say goodbye to quartz? One of the main characters in next week's TI live broadcast, CC2652RB, is a wireless MCU that does not require an external crystal oscillator
- How to calculate the input impedance of this full-bridge rectifier?