"STM Library application" Stm32 TIM (detailed two-pulse width, periodic measurement)

Source: Internet
Author: User
Tags valid

The research has been done yesterday, but due to the patent application, there has been no time to upload, today to fill.
Today, mainly using TIM3 PWM input mode, the width of the pulse signal of the rectangular wave and its period of measurement, first look at a picture.

Figure 1 Tim Internal logic diagram
Let's take a look at what the datasheet says:
This mode is a special case of the input capture mode, except for the following differences, the operation is the same as the input capture mode:
Two icx signals are mapped to the same tix input.
These 2 icx signals are edge-effective, but opposite polarity.
One of the TIXFP signals is used as the trigger input signal, and the slave mode controller is configured to reset mode.
For example, you need to measure the length of the PWM signal input to the TI1 (TIMX_CCR1 register) and duty ratio (TIMX_CCR2
Register), the steps are as follows (depending on the frequency of the ck_int and the value of the Prescaler)
Select the valid input for TIMX_CCR1: The cc1s=01 of the TIMX_CCMR1 register (select TI1).
Select the effective polarity of the TI1FP1 (used to capture data to TIMX_CCR1 and purge counters): Set Cc1p=0 (rising edge
valid).
Select the valid input for TIMX_CCR2: The cc2s=10 of the TIMX_CCMR1 register (select TI1).
Select the effective polarity of the TI1FP2 (capture data to TIMX_CCR2): The Cc2p=1 (falling Edge is valid).
Select a valid trigger input signal: Place the ts=101 in the TIMX_SMCR register (select TI1FP1).
Configure the slave mode controller to reset mode: Sms=100 in the TIMX_SMCR.
Enable capture: Cc1e=1 and cc2e=1 in the Timx_ccer register.
We can see from Figure 1 that only TI1FP1 and TI2FP2 are connected to the slave mode controller, so the PWM input mode can only use the TIMX_CH1/TIMX_CH2 signal.

Figure 2 Ic1,ic2 capture signal graph
TI1 is the input PWM wave signal waveform, timx_cnt is the counter count value, when the first falling edge signal arrives, IC2 will first capture, then to the next rising edge, IC1 to capture. At the same time, IC1 and IC2 will have the value of CNT mapped to the corresponding CCR1, CCR2 registers, this value is the count value we need.
Then the period we need to measure is the value captured by the IC1, and the pulse width is the value of the IC2 measurement.
However, there is a bug, people who do not pay attention, it is easy to make mistakes. Our CCRX register is a 16bit register count range of 0~0XFFFF, that is 0~65535, but when the measurement of the rectangular wave period is too long, the pulse width is too wide, two of our counting is too fast, it is easy to appear ccrx overflow phenomenon, so, when we measure the low-frequency signal, The Tim clock must be divided, so that our count will not be so fast, and will not produce overflow phenomenon, when we count, and then divide the result of the frequency multiplier will be OK.
There is another place, maybe you just follow what I said above, and you will find that he is always with the actual frequency, or the cycle is a little bit worse. In fact, this is for a reason. Here's a look at Figure 3:

Fig. 3 Note
As shown in Figure 3, we can see that he is going to have three clocks in the beginning of the counting process, so we have to do that on a numerical basis. That's all there is. The following code is attached:
void Inpwm_init (void)
{
Tim_icinittypedef tim_icinitstructure;
/* TIM3 Clock Enable */
Rcc_apb1periphclockcmd (RCC_APB1PERIPH_TIM3, ENABLE);

/* GPIOA Clock Enable */
Rcc_apb2periphclockcmd (Rcc_apb2periph_gpioa, ENABLE);

/* NVIC Configuration */
Nvic_configuration ();

/* Configure the GPIO ports */
Gpio_configuration ();

/* TIM3 CONFIGURATION:PWM Input mode ————————
The external signal is connected to TIM3 CH2 pin (pa.07),
The Rising edge is used as active edge,
The TIM3 CCR2 is used to compute the frequency value
The TIM3 CCR1 is used to compute the duty cycle value
———————————————————— */

Tim_icinitstructure.tim_channel = Tim_channel_1;
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;

Tim_pwmiconfig (TIM3, &tim_icinitstructure);

/* Select the TIM3 Input TRIGGER:TI2FP2 */
Tim_selectinputtrigger (TIM3, TIM_TS_TI1FP1);

/* Select the slave mode:reset Mode */
Tim_selectslavemode (TIM3, Tim_slavemode_reset);

/* Enable the master/slave Mode * *
Tim_selectmasterslavemode (TIM3, tim_masterslavemode_enable);

/* TIM Enable counter */
Tim_cmd (TIM3, ENABLE);

/* Enable the CC2 Interrupt Request * *
Tim_itconfig (TIM3, tim_it_cc2| TIM_IT_CC1, ENABLE);
}

void Tim3_irqhandler (void)
{
if (Tim_getflagstatus (tim3,tim_flag_cc1) = = SET)
{
Dutycycle = Tim_getcapture1 (TIM3) +3;
Frequency = 72000000/dutycycle;
Tim_clearflag (TIM3,TIM_FLAG_CC1);
}
if (Tim_getflagstatus (tim3,tim_flag_cc2) = = SET)
{
Ic2value = Tim_getcapture2 (TIM3) +4;
Tim_clearflag (TIM3,TIM_FLAG_CC2);

}

}


Figure 4 Actual waveform

Figure 5 Measurement results
Blue Bridge Cup-embedded communication group 147520657

Original link: http://blog.csdn.net/ieczw/article/details/17220451

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.