First, it is necessary to understand the input trigger module of the stm32 timer, as shown below:
It should be noted that at first glance, a timer seems to have 8 channels, four on the left and four on the right, but in fact the left and right share the same IO pins, so the name labels are exactly the same. In other words, each general timer has only four independent channels. When a channel is used as an input trigger function, it can no longer be used as an output match function. We can also find confirmation of this from other places. For example, in the TIM_ITConfig() function:
void TIM_ITConfig |
( |
TIM_TypeDef * |
TIMx, |
|
|
uint16_t |
TIM_IT, |
|
|
FunctionalState |
NewState |
|
) |
|
|
Enables or disables the specified TIM interrupts.
Parameters:
TIMx,: |
where x can be 1 to 17 to select the TIMx peripheral. |
TIM_IT,: |
specifies the TIM interrupts sources to be enabled or disabled. This parameter can be any combination of the following values: TIM_IT_Update: TIM update Interrupt source TIM_IT_CC1: TIM Capture Compare 1 Interrupt source TIM_IT_CC2: TIM Capture Compare 2 Interrupt source TIM_IT_CC3: TIM Capture Compare 3 Interrupt source TIM_IT_CC4: TIM Capture Compare 4 Interrupt source TIM_IT_COM: TIM Commutation Interrupt source TIM_IT_Trigger: TIM Trigger Interrupt source TIM_IT_Break: TIM Break Interrupt source |
We can see that the value range of the TIM_IT parameter of this function is as follows:
TIM_IT_Update: TIM update Interrupt source
TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
TIM_IT_COM: TIM Commutation Interrupt source
TIM_IT_Trigger: TIM Trigger Interrupt source
TIM_IT_Break: TIM Break Interrupt source
That is to say, the capture and comparison functions of each channel share one interrupt flag.
The input trigger function of stm32 timer is actually quite simple, almost the same as that of AVR microcontroller. Once a valid edge appears on the microcontroller pin (can be configured to trigger rising, falling, or both rising and falling), the value in the timer counter CNT will be saved by the corresponding Capture/Compare X Register. Here X can be any of 1, 2, 3, and 4. And the interrupt flag is set. However, at this time, the count register CNT of TIM will continue to count regardless of the occurrence of this event. This function can be used to measure the pulse width or period of external signals.
I will not explain the time base unit TIM_TimeBaseStructure of the timer, as it is specifically introduced in my previous article. Now I will focus on the input trigger unit TIM_ICInitStructure.
First, let's look at the definition of the substructure prototype as follows:
typedef struct
{
uint16_t TIM_Channel; /*!< Specifies the TIM channel.
This parameter can be a value of @ref TIM_Channel */
uint16_t TIM_ICPolarity; /*!< Specifies the active edge of the input signal.
This parameter can be a value of @ref TIM_Input_Capture_Polarity */
uint16_t TIM_ICSelection; /*!< Specifies the input.
This parameter can be a value of @ref TIM_Input_Capture_Selection */
uint16_t TIM_ICPrescaler; /*!< Specifies the Input Capture Prescaler.
This parameter can be a value of @ref TIM_Input_Capture_Prescaler */
uint16_t TIM_ICFilter; /*!< Specifies the input capture filter.
This parameter can be a number between 0x0 and 0xF */
} TIM_ICInitTypeDef;
It has a total of 5 members. The specific functions of the 5 members can be clearly seen by looking at the description of the 3.5 version firmware library.
uint16_t TIM_ICInitTypeDef::TIM_Channel |
Specifies the TIM channel. This parameter can be a value of TIM_Channel
The value range of TIM_Channel is as follows:
TIM_Channel_1.
TIM_Channel_2
TIM_Channel_3
TIM_Channel_4
uint16_t TIM_ICInitTypeDef::TIM_ICFilter |
Specifies the input capture filter. This parameter can be a number between 0x0 and 0xF
To be honest, I don't have a deep understanding of the specific role of this member. I only know that it serves as a filter for the input signal. I guess it allows the user to set the number of sampling clocks to determine the final input signal, which plays a filtering role and avoids interference from high-frequency signals. Anyway, I don't care about it.
uint16_t TIM_ICInitTypeDef::TIM_ICPolarity |
Specifies the active edge of the input signal. This parameter can be a value of TIM_Input_Capture_Polarity
This is the polarity selection of the trigger edge, and the value range is as follows:
TIM_ICPolarity_BothEdge
TIM_ICPolarity_Rising
TIM_ICPolarity_Falling
uint16_t TIM_ICInitTypeDef::TIM_ICPrescaler |
Specifies the Input Capture Prescaler. This parameter can be a value of TIM_Input_Capture_Prescaler
This member is used to divide the external signal, that is, to set the Prescaler in the above figure, which can be set to 1/2/4/8 division.
uint16_t TIM_ICInitTypeDef::TIM_ICSelection |
Specifies the input. This parameter can be a value of TIM_Input_Capture_Selection
The function of this member must be understood by referring to the above diagram. Looking carefully at the above diagram, you can find that the 4 channels of the timer are not completely independent, but 1 and 2 are in a group, 3 and 4 are in a group, and the channels in the same group are connected. In other words, cross-triggering can occur. And TIM_ICSelection is to choose whether to use cross-triggering. If you don't understand, you can read the firmware library's documentation. The following is the value range of this structure member:
#define TIM_ICSelection_DirectTI ((uint16_t)0x0001) |
TIM Input 1, 2, 3 or 4 is selected to be connected to IC1, IC2, IC3 or IC4, respectively
#define TIM_ICSelection_IndirectTI ((uint16_t)0x0002) |
TIM Input 1, 2, 3 or 4 is selected to be connected to IC2, IC1, IC4 or IC3, respectively.
#define TIM_ICSelection_TRC ((uint16_t)0x0003) |
TIM Input 1, 2, 3 or 4 is selected to be connected to TRC.
In other words, depending on the different values, the trigger signal of the external pin can be connected to different internal units, which makes the microcontroller more flexible.
Below is the main.c file
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
void delay()
{
u32 i,j;
for(i=0;i<1000;i++)
for(j=0;j<5000;j++)
;
}
void rcc_cfg()
{
;
}
void gpio_cfg()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Arbitrary delay LED inversion, and use PA8 as the trigger timer level
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //When the interrupt is triggered, invert PD2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* TIM3 channel 2 pin (PA.07) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void nvic_cfg()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preempt priority level 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //From priority level 3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel is enabled
NVIC_Init(&NVIC_InitStructure); //Initialize peripheral NVIC registers according to the parameters specified in NVIC_InitStruct
}
void tim3_cfg()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_DeInit(TIM3);
TIM_InternalClockConfig(TIM3);
//The pre-scaling factor is 36000-1, so the counter clock is 72MHz/36000 = 2kHz
TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1;
//Set the clock division
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
//Set the counter mode to up counting mode
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//Set the count overflow size, and generate an update event every 2000 counts
TIM_TimeBaseStructure.TIM_Period = 2000 - 1;
//Apply the configuration to TIM2
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
//Disable ARR preload buffer
TIM_ARRPreloadConfig(TIM3, DISABLE);
// Below is the configuration of TIM_ICInitStructure
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0; /*Select input comparison filter, filter settings, and determine the waveform is stable after several cycles of jumps 0x0~0xF*/
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// Enable TIM2 interrupt
TIM_ClearFlag(TIM3, TIM_IT_CC2);
TIM_ITConfig(TIM3,TIM_IT_CC2,ENABLE);
TIM_Cmd(TIM3, ENABLE); //Enable TIMx peripherals
}
/
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
rcc_cfg();
gpio_cfg();
nvic_cfg();
tim3_cfg();
while (1)
{
/* Set PA8 */
GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
delay();
/* Reset PA8 */
GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_RESET);
delay();
}
}
Note that the Timer 3 Channel 2 pin is set to a floating input.
Below is the stm32f10x_it.c file
#include "stm32f10x_it.h"
u8 flag=0;
extern TIM_ICInitTypeDef TIM_ICInitStructure;
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET)
{
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
//Change the capture trigger mode every time an interrupt is entered, and flip the level of PD2
if(flag==0)
{
flag=1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_RESET);
}
else
{
flag=0;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_SET);
}
}
}
After the program runs, you can see the two LED lights on the board flashing alternately.
I didn't do anything with the captured value because I was just testing whether the program could successfully enter the capture interrupt.
Previous article:STM32F4 - Timer principle and application interrupt, input capture, PWM output
Next article:The difference between STM32 advanced timer, general timer TIMx, basic timer TIM6 and TIM7
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Introduction and function of bridge in EMC design
- TI Embedded Processing Month Live Broadcast Replay | Automotive and millimeter wave radar, low-power WiFi MCU, AM57X platform
- (Event ends) 2021 STM32 National Tour Seminar!
- Selection of low-power domestic MCU
- Implementing emmc5.0 boot under Android
- AVR MCU Literacy
- Share your Alipay points to redeem small commodities, etc.
- ESP32-C3 porting has begun
- 【TI recommended course】#Lecture on Basic Knowledge of Electronic Circuits - Power MOSFET#
- How to get 9V/12V from PD charger to power products for fast charging? Overview of Lederui PD power chip