Timer F4 timer Tim (1) Timer control output [use library function]

Source: Internet
Author: User

Introduction to advanced clock control timer tim1 & tim8:

The advanced control timer of ikef4 contains an automatic reload counter. The input of the counter is a system clock that is pre-divided.

This timer can be used for multiple purposes, including vehicle input signal length (input capture mode) or waveform output (output capture, PWM, complementary PWM output with dead zone insertion)

The pulse length and waveform cycle can be adjusted within several microseconds by the timer or RCC pre-divider.

The advanced control timer and general timer are completely independent and do not share any resources.

Advanced clock control timer tim1 & tim8 features:

1. 16-bit up/down/two-way automatic reload Counter 2. 16-bit pre-divider, the Division value ranges from 1 to 655353, and 4 independent channels. 4. Complementary output with dead output. 5. synchronous circuit for controlling external signals. 6. Brake input. 7. Interrupt generation and DMA force. 8, externally triggered

And so on ..

The TIM timer is really powerful. As for how to use it, ST's Manual is not surprisingly ugly and completely unorganized. I did not understand what I was talking about yesterday. The supporting Firmware Library is also the introduction of various functions. The function name struct definition has no logic at all. Therefore, we can only refer to the introduction of netizens to get started from the most basic part.

For more information, see section general timer

[Experiment 1. tim1 timing function]

[Experiment description]

The technical functions of tim1 are used to generate 2Hz interruptions. led1 is reversed at each interruption, and the led1 is reversed at 1Hz.

According to the clock configuration, the system clock is 168 MHz, and the apb2 clock is 84 MHZ. Tim1 is mounted to apb2, so the apb2 clock is 84 MHZ.

Therefore, the pre-division coefficient is set to 10000, that is, 0x2710, and the auto-Reload counter Arr (tim_period) is set to 4200, that is, 0x1068. An interruption occurs when the count is full.

Interruption Frequency: F = 84 MHZ/4200/10000 = 2Hz


1. Enable tim1 clock first

Rcc_apb2periphclockcmd (rcc_apb2periph_tim1, enable );

2. Time Base unit Initialization

Rcc_apb2periphclockcmd (rcc_apb2periph_tim1, enable );
Tim_timebaseinitstructure.tim_clockdivision = tim_ckd_div1;
Tim_timebaseinitstructure.tim_countermode = tim_countermode_up;
Tim_timebaseinitstructure.tim_period = 0x1068;
Tim_timebaseinitstructure.tim_prescaler = 0x2710;
Tim_timebaseinitstructure.tim_repetitioncounter = 0x00;

Tim_timebaseinit (tim1, & tim_timebaseinitstructure );

Tim_timebaseinit (tim1, & tim_timebaseinitstructure );
Tim_clearflag (tim1, tim_flag_update); // The update flag generated during configuration must be cleared first.
Tim_itconfig (tim1, tim_it_update, enable); // enable interruption. the interruption event is a timer wage event.
Tim_cmd (tim1, enable); // enable the timer

3. Interrupt Processing functions

There is nothing to say, just reverse the LED lamp. Each interruption is reversed once, and a 2Hz interruption generates a 1Hz flash.

The interrupt name is defined in the database and is shared with tim10 Global interrupt.

Void tim1_up_tim10_irqhandler (void)
Tim_clearflag (tim1, tim_flag_update); // clear the update flag before the interruption.
Ledtog (led1 );

Then we can see that the LED is blinking at a frequency of about 1Hz.

[Experiment 2. Forced output mode Experiment]

Definition of Baidu's forced output mode: InProgramIn programming, I/O ports can generally be used as input and output. Some data must be executed when it is executed, so it must be output forcibly. This is the only thing I/O port can do.

After reading it, I still feel confused.

Simply put, no matter what I/O output at that time, it can be set to 0 or 1.

[Experiment description]

For the convenience of the experiment, this experiment uses the force output function of tim4 to light up the led3 that is connected to the gpiod pin13 pin. For the force output function, the advanced timer and the general timer are exactly the same.

The CH2 of tim4 is reused in the pin13 of gpiod. Therefore, the output can be forced to be high to light the LED.

[Code implementation]

1. First, initialize gpio as the AF reuse function.

The pin multiplexing function of cm4 is different from that of cm3, so pay special attention to it. There will be no output according to the cm3 statement.
Void tim4_gpio_config (void)
Gpio_inittypedef gpio_initstructure;
Rcc_ahb1periphclockcmd (rcc_ahb1periph_gpiod, enable );

Gpio_initstructure.gpio_mode = gpio_mode_af;
Gpio_initstructure.gpio_otype = gpio_otype_pp;
Gpio_initstructure.gpio_pin = gpio_pin_12 | gpio_pin_13 | gpio_pin_14 | gpio_pin_15;
Gpio_initstructure.gpio_pupd = gpio_pupd_nopull;
Gpio_initstructure.gpio_speed = gpio_speed_100mhz;
Gpio_init (gpiod, & gpio_initstructure );

Gpio_pinafconfig (gpiod, gpio_pinsource12, gpio_af_tim4 );
Gpio_pinafconfig (gpiod, gpio_pinsource13, gpio_af_tim4 );
Gpio_pinafconfig (gpiod, gpio_pinsource14, gpio_af_tim4 );
Gpio_pinafconfig (gpiod, gpio_pinsource15, gpio_af_tim4 );

2. tim4 Initialization

I have not calculated the clock here, because this experiment is not very concerned about this.

Void tim4_config1 (void)
Tim4_gpio_config ();
Rcc_apb1periphclockcmd (rcc_apb1periph_tim4, enable );

Tim_timebaseinitstructure.tim_clockdivision = tim_ckd_div1;
Tim_timebaseinitstructure.tim_countermode = tim_countermode_up;
Tim_timebaseinitstructure.tim_period = 0x1068;
Tim_timebaseinitstructure.tim_prescaler = 0x2710;
Tim_timebaseinitstructure.tim_repetitioncounter = 0x00;
Tim_timebaseinit (tim4, & tim_timebaseinitstructure );
Tim_arrpreloadconfig (tim4, enable );

Tim_ocinitstructure.tim_ocmode = tim_ocmode_active; // you can set any mode.
Tim_ocinitstructure.tim_pulse = 1000;
Tim_ocinitstructure.tim_ocpolarity = tim_ocpolarity_high;
Tim_ocinitstructure.tim_outputstate = tim_outputstate_enable;
Tim_oc2init (tim4, & tim_ocinitstructure );

Tim_cmd (tim4, enable );

3. Force output in the main function.

After initialization, you can force the pin level at any time. You only need one function:

Tim_forcedoc2config (tim4, tim_forcedaction_active );
This function sets the ocxm bit of Tim's ccmr1 to 101 or 100 to achieve the pulling or lowering of the output.

[Experiment 3. Comparison and output]
I have never understood which two items are being compared here. The following sentence was found in Reference Manual of the St reference manual today:

When the ocxm bit is 000: the comparison between the output compare register timx_ccr1 and the counter timx_cnt has no effect on the outputs.

Here, the comparison is the comparison between timx_ccr1 and timx_cnt. An event is triggered when two values are equal. When this event occurs, Tim outputs data based on the ocxm bit of the ccmrx register.

The following table lists the functions of different ocxm settings:

Ocxm [2 .. 0] value Function
000 No effect on output
001 When the values are equal, the output value must be 1.
010 When equal, the output value is forced to 0.
011 Output Inversion
100 Whether equal or not, Force 0
101 Whether equal or not, Force 1
110 PWM mode 1 (positive and negative)
111 PWM mode 2 (first negative and then positive)

In combination with the definition in the database, you can easily change the output mode:

# Define tim_ocmode_timing (uint16_t) 0x0000)
# Define tim_ocmode_active (uint16_t) 0x0010)
# Define tim_ocmode_inactive (uint16_t) 0x0020)
# Define tim_ocmode_toggle (uint16_t) 0x0030)
# Define tim_ocmode_pwm1 (uint16_t) 0x0060)
# Define tim_ocmode_pwm2 (uint16_t) 0x0070)

[Experimental phenomenon]

Led periodic flickering

[Code implementation]

You only need to change tim_ocmode in the above Code to PWM.

Tim_ocinitstructure.tim_ocmode = tim_ocmode_toggle;

The tim_pulse parameter does not affect this mode. tim_ocpolarity only affects whether the output is low or high.

[Experiment 4. PWM output]

This is the traditional output part. The TIM example of all development boards is a PWM output.

The time base unit is ready. Set the output mode to reverse the timing (tim_pulse ). Enable the OC of the elder brother channel. For all the channels of each Tim, the duty cycle of each channel can only be changed because the time base configuration is the same.

Tim_ocinitstructure.tim_ocmode = tim_ocmode_pwm1; // pwm2
Tim_ocinitstructure.tim_pulse = 2000; // Cr, set the duty cycle. Invalid in reverse mode
Tim_ocinitstructure.tim_ocpolarity = tim_ocpolarity_high;
Tim_ocinitstructure.tim_outputstate = tim_outputstate_enable;
Tim_clearflag (tim4, tim_flag_cc2 );
Tim_oc2init (tim4, & tim_ocinitstructure );
Of course, pin Initialization is indispensable.

[Experiment 5. single pulse mode]

You only need to add one sentence after the above Code:

Tim_selectonepulsemode (tim4, tim_opmode_single );

In this case, a negative pulse is generated, and the result is that the LED remains always bright after being extinguished. If you want to enable the led to light up, the output Positive pulse also needs to change the polarity of the output:

Tim_ocinitstructure.tim_ocpolarity = tim_ocpolarity_low;

At this point, the experiment that Tim controls the output is basically done. Start the TIM control input part immediately, including the input capture and PWM input.



Technorati flag: 127f4, Tim, timer, timing, force output, PWM, single pulse output

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.