stm32 library function learning chapter general timer input capture function

Publisher:TranquilMindLatest update time:2016-05-25 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Function: PA8 can drive the LED light to flash with random delay, and connect PA8 to PA7 port with Dupont line. PA7 is the 2nd channel of general timer TIM3. In the TIM3_CH2 trigger interrupt program, the LED light connected to PD2 port is inverted to indicate the interrupt program is running, and the polarity of trigger capture is changed after each interrupt. The two LED lights will flash alternately.

 

First, it is necessary to understand the input trigger module of the stm32 timer, as shown below:

 

stm32 library function learning chapter general timer input capture function

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.

 

Keywords:stm32 Reference address:stm32 library function learning chapter general timer input capture function

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号