PWM input mode

Source: Internet
Author: User

1. Concepts

The PWM input capture mode is a special case of the input capture mode,

Input capture is the capture device that immediately copies the current Count value to another register when a level change occurs on the Pin connected to the timer. You can enable capture interrupt and read the saved Count value in the interrupt handler.

Unlike input capture, the PWM input mode connects the same input signal (ti1 or ti2) to two Capture Devices (IC1 and ic2 ). The two capturing devices capture the rising edge and capture the falling edge. One of ti1fp1 and ti2fp2 is selected as the trigger input and the slave mode controller is configured as the reset mode.

Understand as follows:

1. Each timer has four input capture channels IC1, ic2, IC3, and ic4. And IC1 ic2, IC3 ic4. And set the correspondence between pins and registers.

2. The same tix input maps two icx signals.

3. The two icx signals are valid at the opposite polar edge.

4. One of the two edge signals is selected as the trigger signal, and the slave mode controller is set to reset mode.

5. when the trigger signal comes, it is set as the capture register to trigger the input signal and capture "one PWM cycle (two consecutive rising edges or falling edges )", it is equal to the number of times that contain the TIM clock cycle (that is, the number of times the TIM is counted in the capture register ).

6. Similarly, another capture channel captures the count of the trigger signal and the next edge signal of the opposite polarity (that is, the cycle of the high or low level)

7. The PWM clock cycle and duty cycle can be calculated.

   Frequency = f (TIM clock frequency)/n.

   Duty Cycle = (high count/N ),

   If M is the number of high-level counters, duty cycle = M/N

   If M is the number of low-level counters, duty cycle = (n-M)/n

Note: Because the counter is 16 bits, a maximum of 65535 counters can be counted in a cycle. Therefore, the minimum measured frequency is Tim clock frequency/65535.

Ii. configuration instructions

The PWM input 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.
● The two icx signals are valid for the edge, but the polarity is opposite.
● One Of The tixfp signals is used as the trigger input signal, while the slave mode controller is configured as the reset mode.
For example, you need to measure the length of the PWM signal input to ti1 (timx_ccr1 register) and the duty cycle (timx_ccr2
Register), the specific steps are as follows (depending on the ck_int frequency and the pre-divider value)
● Select valid timx_ccr1 input: Set the timx_ccmr1 register cc1s = 01 (select ti1 ).
● Select the effective polarity of ti1fp1 (used to capture data to timx_ccr1 and clear the counter): Set cc1p = 0 (rising edge
Valid ).
● Select valid timx_ccr2 input: Set the timx_ccmr1 register cc2s = 10 (select ti1 ).
● Select the effective polarity of ti1fp2 (capture data to timx_ccr2): Set cc2p = 1 (the descent edge is valid ).
● Select a valid trigger input signal: Set Ts = 101 in the timx_smcr register (select ti1fp1 ).
● Set slave mode controller to reset mode: Set SMS = 100 in timx_smcr

● Enable Capture: Set cc1e = 1 and cc2e = 1 in the timx_ccer register.

Figure pwmpwm input mode Sequence

Since only ti1fp1 and ti2fp2 are connected to the slave mode controller, the PWM input mode can only use timx_133/timx_ch2 signals.

Description of three program instances

1. Program Overview: tim3 is selected as the PWM input capture. Ic2 is set as the rising edge and as a valid trigger input signal. So the capture register of ic2 captures the PWM cycle,

                  The capture register of IC1 captures the high cycle of PWM.

2. The program code is as follows:

 Rcc_apb1periphclockcmd (rcc_apb1periph_tim3, enable );    // Clock Configuration

 Rcc_apb2periphclockcmd (rcc_apb2periph_gpioa, enable );



 Gpio_initstructure.gpio_pin = gpio_pin_7;                             // Configure gpio
 Pio_initstructure.gpio_mode = gpio_mode_in_floating;
 Gpio_initstructure.gpio_speed = gpio_speed_50mhz;

 Gpio_init (gpioa, & gpio_initstructure );




 Nvic_initstructure.nvic_irqchannel = tim3_irqn;                    // Configure Objective C
 Nvic_initstructure.nvic_irqchannelpreemptionprioRity = 0;
 Nvic_initstructure.nvic_irqchannelsubpriority = 1;
 Nvic_initstructure.nvic_irqchannelcmd = Enable;
 Nvic_init (& nvic_initstructure );




 Tim_icinitstructure.tim_channel = tim_channel_2;                 // Select a channel
 Tim_icinitstructure.tim_icpolarity = tim_icpolarity_rising;     // Triggered by the rising edge
 Tim_icinitstructure.tim_icselection = tim_icselection_directti;  // Ing between pins and registers
 Tim_icinitstructure.tim_icprescaler = tim_icpsc_div1;          // Input pre-division. It means to control the number of input cycles for one capture. If

// The input signal frequency does not change, and the measured period does not change. For example, if the frequency is 4, a capture is performed once every four input cycles. In this way, when the input signal does not change frequently,

// Reduce the number of times the software is continuously interrupted.
 Tim_icinitstructure.tim_icfilter = 0x0;                          // Filter settings. After several cycle hops, the waveform is determined to be stable 0x0 ~ 0xf

 Tim_pwmiconfig (tim3, & tim_icinitstructure );               // Configure the TIM peripherals according to the parameters.

 Tim_selectinputtrigger (tim3, tim_ts_ti2fp2 );              // Select ic2 as the always-triggered source.

 Tim_selectslavemode (tim3, tim_slavemode_reset); // Tim slave mode: the rising edge of the trigger signal reinitializes the counter and triggers register update events.

 Tim_selectmasterslavemode (tim3, tim_masterslavemode_enable); // passively triggers the timer.

 Tim_cmd (tim3, enable );                               // Start tim2

 Tim_itconfig (tim3, tim_it_cc2, enable );   // Open the interrupt


Interrupt Processing Function


Void tim3_irqhandler (void)
 Tim_clearitpendingbit (tim3, tim_it_cc2 );              // Clear the waiting position for Tim's interruption

 Ic2value = tim_getcapture2 (tim3 );                       // Read the value of the ic2 capture register, that is, the Count value of the PWM cycle.

 If (ic2value! = 0)
   Dutycycle = (tim_getcapture1 (tim3) * 100)/ic2value;       // Read the value of IC1 capture register and calculate the duty cycle

   Frequency = 72000000/ic2value;                                        // Calculate the PWM frequency.
   Dutycycle = 0;
   Frequency = 0;



Note (1): If you want to change the measurement's PWM frequency range, you can divide the TIM clock frequency into different frequencies.

 Tim_timebasestructure.tim_period = 0 xFFFF;  // Period 0 ~ FFFF
  Tim_timebasestructure.tim_prescaler = 5; // Clock frequency division. The frequency division is 5 + 1, that is, 6.
  Tim_timebasestructure.tim_clockdivision = 0;// Time Division
  Tim_timebasestructure.tim_countermode = tim_countermode_up; // Mode
  Tim_timebaseinit (tim2, & tim_timebasestructure); // basic Initialization
Note (2): The Multiplier X1 or X2 of the timer Tim. When APB is divided into 1, the multiplier value is 1, otherwise it is 2.

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: 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.