Stm32 Study Notes 3 (TIM input capture)

Source: Internet
Author: User

PWM input capture mode <xmlnamespace prefix = "O" NS = "urn: Schemas-Microsoft-com: Office: office"/>

1. Concepts

Special case of input Capture Mode in PWM input Capture Mode

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

2. Two Ti outputs Map 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 the 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 captured in the capture register n)

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 clock cycle and duty cycle of PWM 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;

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. Program Design

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 period 01 rcc_apb1periphclockcmd (rcc_apb1periph_tim3, enable); // clock Configuration
02 rcc_app2periphclockcmd (rcc_apb2periph_gpioa, enable );
03
04 gpio_initstructure.gpio_pin = gpio_pin_7; // configure gpio
05 gpio_initstructure.gpio_mode = gpio_mode_in_floating;
06 gpio_initstructure.gpio_speed = gpio_speed_50mhz;
07 gpio_init (gpioa, & gpio_initstructure );
08
09 nvic_initstructure.nvic_irqchannel = tim3_irqn; // configure Objective C
10 nvic_initstructure.nvic_irqchannepreemptionpriority = 0;
11 nvic_initstructure.nvic_irqchannelsubpriority = 1;
12 nvic_initstructure.nvic_irqchannelcmd = Enable;
13 nvic_init (& nvic_initstructure );
14
15 tim_icinitstructure.tim_channel = tim_channel_2; // select a channel
16 tim_icinitstructure.tim_icpolarity = tim_icpolarity_rising; // triggered by the rising edge
17 tim_icinitstructure.tim_icselection = tim_icselection_directti; // correspondence between the pins and registers
18 tim_icinitstructure.tim_icpsc_div1; // input pre-division, which means to control the number of input cycles for a capture. If the input signal frequency does not change, the measured cycle will not change, for example, if we select a four-frequency division, we do not have four input cycles for one capture. This can reduce the number of times that the software is continuously interrupted when frequent input signals occur.
19
20 tim_icinitstructure.tim_icfiter = 0x0; // filter settings. After several cycle hops, the waveform is determined to be stable 0x0-0xf;
21 tim_pwmiconfig (tim3, & tim_icinitstructure); // configure the TIM peripherals according to the parameters.
22 tim_selectinputtrigger (tim3, tim_ts_ti2fp2 );
23 tim_selectmastersalvemode (tim3, tim_masterslavemode_enable );
24 tim_cmd (tim3, enable );
25 tim_itconfig (tim3, tim_it_cc2, enable );
26
27 void tim3_irqhandler (void)
28 {
29 tim_clearitpendingbit (tim3, tim_it_cc3 );
30 ic2value = tim_getcapture2 (tim3 );
31 if (ic2value! = 0)
32 {
33 dutycyle = (tim_getcapture1 (tim3) * 100)/ic2value;
34 frequency = 72000000/ic2value;
35}
36 else
37 {
38 dutycycle = 0;
39 frequency = 0;
40}
41
42}
43
44 Note (1): If you want to change the measurement's PWM frequency range, you can divide the TIM clock frequency.
45
46 tim_timebasestructure.tim_period = 0 xFFFF; // period 0 ~ FFFF
47 tim_timebasestructure.tim_prescaler = 5; // clock frequency division, which is 5 + 1 or 6
48 tim_timebasestructure.tim_clockdivision = 0; // Time Division
49 tim_timebasestructure.tim_countermode = tim_countermode_up; // Mode
50 tim_timebaseinit (tim2, & tim_timebasestructure); // basic Initialization
51 Note (2): The timer Tim's Multiplier X1 or X2. When APB is divided into 1, the multiplier value is 1, otherwise it is 2

 

 

Definition of icinitstructre struct

 

01 typedef struct
02 {
03 2010tim_icmode;
04 b2tim_channel;
05 2010tim_icpolarity;
06 02tim_icselection;
07 02tim_icprescaler;
08. 16.02tim_icfilter;
09} tim_icinittypedef;
10
11
12
13 tim_icmode Definition
14 tim_icmode_icap Tim uses the input Capture Mode
15 tim_icmode_pwm1 Tim uses the input PWM mode
16
17 tim_channel
18 timchannel select Channel
19 tim_channel_1 use Tim Channel 1
20 tim_channel_2 use Tim Channel 2
21 tim_channel_3 use Tim Channel 3
22 tim_channel_4 use Tim Channel 4
23
24 tim_icpolarity
25 tim_icpolarity
26 tim_icpolarity_rising Tim input capture rising edge
27 tim_icpolarity_falling Tim input capture descent edge
28
29 tim_icselection
30 tim_icselection select input
31 tim_icselection_directti Tim input 2, 3, or 4 select the corresponding IC1, ic2, IC3, or ic4 to connect
32 tim_icselection_indirectti the input 2, 3, or 4 corresponds to ic2, IC1, ic4, or IC3
33 tim_icselection_trc Tim input 2, 3 or 3 select to connect to TRC
34
35 tim_icprescaler
36 tim_icpsc_div1 Tim capture each time an edge is detected in the capture input line.
37 tim_icps_div2 Tim capture every two events and execute them once
38 tim_icps_div3 Tim captures every three events and executes them once.
39 tim_icps_div4 Tim capture every four events and execute them once

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.