STM32 Note 11: Capture wonderful moments, pulse square wave length capture

Publisher:advancement3Latest update time:2015-09-14 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

Previous article:sw's STM32 Notes 10: Work, PWM output
Next article:STM32 Note 12: The clock never stops working, systic clock application

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号