STM32 single timer four-channel capture function implementation

Publisher:风清扬yxLatest update time:2017-10-30 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:STM32 Reference address:STM32 single timer four-channel capture function implementation

Previous article:STM32 uses a structure to define the address of FSMC
Next article:Differences between eight IO port modes of STM32

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号