Input capture, as a function of the timer, is widely used in industrial speed measurement. Some timers of STM32 have four external channels, and one timer can be used to collect the frequency of four external pulses, saving hardware resources and software code.
If you need to measure the frequency of one or more external square wave pulses, which is lower than the operating frequency of the microcontroller, you can do the following: (TIM4 is used as an example)
Initialization: (omit GPIO configuration, configure the four channel pins of TIM4 as pull-up or floating input, omit timer RCC configuration, and omit interrupt NVIC configuration)
void TIM_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // TIM4 time base
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = 0xffff; //Automatically reload value
TIM_TimeBaseStructure.TIM_Prescaler =719; //Prescaler value, make TIMx_CLK=1MHz
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //Input clock is not divided
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up; //Count up
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);//TIM4_TimeBase
// TIM_ICInitStructure.TIM_ICMode =TIM_ICMode_ICAP; //Input capture mode
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //|TIM_Channel_2; //Input channel
TIM_ICInitStructure.TIM_ICPolarity =TIM_ICPolarity_Rising; //Capture rising edge
TIM_ICInitStructure.TIM_ICSelection =TIM_ICSelection_DirectTI; //Capture interrupt
TIM_ICInitStructure.TIM_ICPrescaler =TIM_ICPSC_DIV1; //Capture without frequency division
TIM_ICInitStructure.TIM_ICFilter =0x0; //Capture input without filtering
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2 ;//|TIM_Channel_2; //Input channel
TIM_ICInitStructure.TIM_ICPolarity =TIM_ICPolarity_Rising; //Capture rising edge
TIM_ICInitStructure.TIM_ICSelection =TIM_ICSelection_DirectTI; //Capture interrupt
TIM_ICInitStructure.TIM_ICPrescaler =TIM_ICPSC_DIV1; //Capture without frequency division
TIM_ICInitStructure.TIM_ICFilter =0x0; //Capture input without filtering
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3 ;//|TIM_Channel_2; //Input channel
TIM_ICInitStructure.TIM_ICPolarity =TIM_ICPolarity_Rising; //Capture rising edge
TIM_ICInitStructure.TIM_ICSelection =TIM_ICSelection_DirectTI; //Capture interrupt
TIM_ICInitStructure.TIM_ICPrescaler =TIM_ICPSC_DIV1; //Capture without frequency division
TIM_ICInitStructure.TIM_ICFilter =0x0; //Capture input without filtering
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4 ;//|TIM_Channel_2; //Input channel
TIM_ICInitStructure.TIM_ICPolarity =TIM_ICPolarity_Rising; //Capture rising edge
TIM_ICInitStructure.TIM_ICSelection =TIM_ICSelection_DirectTI; //Capture interrupt
TIM_ICInitStructure.TIM_ICPrescaler =TIM_ICPSC_DIV1; //Capture without frequency division
TIM_ICInitStructure.TIM_ICFilter =0x0; //Capture input without filtering
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_Cmd(TIM4, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE);
}
in:
TIM_TimeBaseStructure.TIM_Period = 0xffff; is the automatic reload value, the same as ordinary microcontrollers
TIM_TimeBaseStructure.TIM_Prescaler = 719; pre-division value, make TIMx_CLK = 100KHz, the system clock runs at 72M and 720 division, the timer runs at 100KHZ, that is, 10us per division
TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP; This sentence selects the timer as input capture mode, but it is not defined in my library function, so it is commented out and does not affect program execution.
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; Configure channel 1
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; rising edge capture
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; Capture interrupt
TIM_ICInitStructure.TIM_ICFilter = 0x0; no filtering
TIM_ICInit(TIM4, &TIM_ICInitStructure); will configure the application
The entire configuration needs to be rewritten for each channel above. Using '|' is invalid.
TIM_Cmd(TIM4, ENABLE); Enable timer 4
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE); Enable the capture interrupt of four channels
The above completes the TIM configuration. The following is the code in the interrupt:
void
TIM4_IRQHandler(void)
{
//Frequency buffer count
static u16 this_time_CH1 = 0;
static u16 last_time_CH1 = 0;
static u8 capture_number_CH1 = 0;
vu16 tmp16_CH1;
static u16 this_time_CH2 = 0;
static u16 last_time_CH2 = 0;
static u8 capture_number_CH2 = 0;
vu16 tmp16_CH2;
static u16 this_time_CH3 = 0;
static u16 last_time_CH3 = 0;
static u8 capture_number_CH3 = 0;
vu16 tmp16_CH3;
static u16 this_time_CH4 = 0;
static u16 last_time_CH4 = 0;
static u8 capture_number_CH4 = 0;
vu16 tmp16_CH4;
if(TIM_GetITStatus(TIM4, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
if(capture_number_CH1 == 0)
{
capture_number_CH1 = 1;
last_time_CH1 = TIM_GetCapture1(TIM4);
}
else if(capture_number_CH1 == 1)
{
capture_number_CH1 = 0;
this_time_CH1 = TIM_GetCapture1(TIM4);
if(this_time_CH1 > last_time_CH1)
{
tmp16_CH1 = (this_time_CH1 - last_time_CH1);
}
else
{
tmp16_CH1 = ((0xFFFF - last_time_CH1) + this_time_CH1);
}
//TIM2 counter clock = 1MHz
//
FreqBuf[cnt] = (1000000L * 100) / tmp16; //*100 is to expand the display range
Freq_Value[0]=tmp16_CH1;
}
}
if(TIM_GetITStatus(TIM4, TIM_IT_CC2) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
if(capture_number_CH2 == 0)
{
capture_number_CH2 = 1;
last_time_CH2 = TIM_GetCapture2(TIM4);
}
else if(capture_number_CH2 == 1)
{
capture_number_CH2 = 0;
this_time_CH2 = TIM_GetCapture2(TIM4);
if(this_time_CH2 > last_time_CH2)
{
tmp16_CH2 = (this_time_CH2 - last_time_CH2);
}
else
{
tmp16_CH2 = ((0xFFFF - last_time_CH2) + this_time_CH2);
}
//TIM2 counter clock = 1MHz
//
FreqBuf[cnt] = (1000000L * 100) / tmp16; //*100 is to expand the display range
Freq_Value[1]=tmp16_CH2;
}
}
if(TIM_GetITStatus(TIM4, TIM_IT_CC3) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3);
if(capture_number_CH3 == 0)
{
capture_number_CH3 = 1;
last_time_CH3 = TIM_GetCapture3(TIM4);
}
else if(capture_number_CH3 == 1)
{
capture_number_CH3 = 0;
this_time_CH3 = TIM_GetCapture3(TIM4);
if(this_time_CH3 > last_time_CH3)
{
tmp16_CH3 = (this_time_CH3 - last_time_CH3);
}
else
{
tmp16_CH3 = ((0xFFFF - last_time_CH3) + this_time_CH3);
}
//TIM2 counter clock = 1MHz //
FreqBuf[cnt] = (1000000L * 100) / tmp16; //*100 is to expand the display range
Freq_Value[2]=tmp16_CH3;
}
}
if(TIM_GetITStatus(TIM4, TIM_IT_CC4) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC4);
if(capture_number_CH4 == 0)
{
capture_number_CH4 = 1;
last_time_CH4 = TIM_GetCapture4(TIM4);
}
else if(capture_number_CH4 == 1)
{
capture_number_CH4 = 0;
this_time_CH4 = TIM_GetCapture4(TIM4);
if(this_time_CH4 > last_time_CH4)
{
tmp16_CH4 = (this_time_CH4 - last_time_CH4);
}
else
{
tmp16_CH4 = ((0xFFFF - last_time_CH4) + this_time_CH4);
}
//TIM2 counter clock = 1MHz //
FreqBuf[cnt] = (1000000L * 100) / tmp16; //*100 is to expand the display range
Freq_Value[3]=tmp16_CH4;
}
}//
GPIO_WriteBit(GPIOC, GPIO_Pin_13,(BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13))));
}
The four parts of the interrupt code are exactly the same. Only one part of the input capture is analyzed.
The timer counts normally. When an external pulse arrives, the timer count value is stored. When the next pulse arrives, the difference between the two count values is calculated, which is the period of the two pulses.
For example,
The timer counts to 10, an external pulse arrives, and last_time_CH1 is used to store 10.
When the next pulse arrives, the timer count value runs to 110, and this_time_CH1 is used to store 110.
After that, the difference is taken and tmp16_CH1 stores the difference value 100. Since the timer runs at 100KHZ, the count value increases once every 10us, so the pulse period is 100*10=1000us=1ms, which is 1KHZ.
Of course, the timer will overflow and reload, and the difference compensation operation needs to be performed at this time, tmp16_CH1 = ((0xFFFF - last_time_CH1) + this_time_CH1);
The measurable range depends on the frequency at which the timer runs. If the external frequency is so slow that the timer is not triggered twice after a full count, an overflow will occur and the count value will be inaccurate.
Therefore, the timer clock configuration depends on the external pulse frequency and should be configured properly so that the pulse frequency range does not overflow.
Since each external pulse will trigger an interrupt, especially for four channels, using the interrupt method will slightly occupy CPU resources. Using DMA can solve this problem.
After obtaining the pulse period, the external frequency can be obtained through calculation and then the speed can be measured.
Previous article:STM32 uses a structure to define the address of FSMC
Next article:Differences between eight IO port modes of STM32
- 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
- EEWORLD University Hall----Application of TI DLP? Technology in 3D Machine Vision and Automated Optical Inspection
- Oscilloscope measurement and analysis of Honda Fit transmission waveform
- CCS3.3 cannot connect to DM642
- About using a single chip microcomputer to read external voltage ADC impedance matching
- Are there any alternative products to domestic high-frequency PCBs? Has anyone used them?
- DSP/BIOS Thread
- Is the performance so severely compressed when not plugged in?
- Talk about Wi-Fi Alliance and the future of Wi-Fi
- 10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication)
- EEWORLD University Hall ---- Introduction to Microelectronics Technology