STM32 Universal Timer

Source: Internet
Author: User
STM32 Timer is a powerful module, the timer is also very high frequency, timer can do some basic timing, also can do PWM output or input capture function.

Clock source problem:

There are eight named Timx, of which TIM1 and TIM8 hang on the APB2 bus, while Tim2-tim7 hangs on

APB1 on the bus. Where TIM1&TIM8 is called the Advanced control timer. Their APB2 bus is also better than the APB1 bus. APB2 can work at 72MHz, while APB1 is 36MHz maximum.

The timer's clock does not come directly from the APB1 or APB2, but from a multiplier that is input to APB1 or APB2.

The following timer 2~7 the function of this multiplier: when the APB1 prescaler coefficient is 1 o'clock, the multiplier does not work, the clock frequency of the timer is equal to the frequency of APB1, when the APB1 prescaler coefficient is another value (that is, the prescaler coefficient is 2, 4, 8 or 16), the multiplier function, The clock frequency of the timer equals the frequency of APB1 twice times.

Assuming Ahb=36mhz, because the maximum allowable frequency of APB1 is 36MHz, so the prescaler coefficient of APB1 can take any value, when the prescaler factor = 1 o'clock, the apb1=36mhz,tim2~7 clock frequency =36mhz (multiplier does not work), when the prescaler coefficient = 2 o'clock, Apb1=18mhz, the tim2~7 clock frequency is =36mhz under the action of the multiplier.

Some people will ask, since the need tim2~7 clock frequency =36mhz, why not directly take the APB1 prescaler coefficient = 1. The answer is: APB1 not only to provide the clock for the tim2~7, but also to provide clocks for other peripherals, set this multiplier can be used to ensure that other peripherals using a lower clock frequency, tim2~7 can still get a higher clock frequency.

Another example: When Ahb=72mhz, the APB1 prescaler coefficient must be greater than 2, because the maximum frequency of APB1 can only be 36MHz. If the APB1 prescaler factor = 2, the tim2~7 can still get a 72MHz clock frequency because of the multiplier. The ability to use a higher clock frequency has undoubtedly increased the resolution of the timer, which is why the multiplier was designed.

Tim Universal Timer Configuration steps:

1. Configuring the Tim Clock

Rcc_apb1periphclockcmd (rcc_apb1periph_tim2, ENABLE);

Rcc_apb2periphclockcmd (RCC_APB2PERIPH_TIM1, ENABLE);

2. Timer BASIC Configuration

void Tim2_configuration (void)
{
Tim_timebaseinittypedef tim_timebasestructure;
Tim_ocinittypedef tim_ocinitstructure;
Tim_deinit (TIM2); Reset TIM2 Timer

/* TIM2 Configuration */
Tim_timebasestructure.tim_period = 5; 2.5ms
Tim_timebasestructure.tim_prescaler = 36000; Crossover 36000
Tim_timebasestructure.tim_clockdivision = TIM_CKD_DIV1; Clock divider
Tim_timebasestructure.tim_countermode = tim_countermode_up; Counting direction up counting
Tim_timebaseinit (TIM2, &tim_timebasestructure);

/* Clear TIM2 Update pending flag[clear TIM2 overflow Interrupt flag] */
Tim_clearflag (TIM2, tim_flag_update);

/* Enable TIM2 Update interrupt [TIM2 overflow interrupt allow]*/
Tim_itconfig (TIM2, Tim_it_update, ENABLE);

/* TIM2 enable counter [Allow TIM2 count]*/
Tim_cmd (TIM2, ENABLE);
}

Tim_period sets the value of the automatic reload register period for the next update event load activity. Its value must be between 0x0000 and 0xFFFF.

The Tim_prescaler sets the Prescaler value used as the divisor of the TIMX clock frequency. Its value must be between 0x0000 and 0xFFFF.

The role of Tim_clockdivision is to do a delay, generally in special occasions will be used, can not care.

Tim_countermode selected the counter mode.

Tim_countermode_up
Tim Up Counting mode
Tim_countermode_down
Tim Down counting mode
tim_countermode_centeraligned1 Tim Central Snap mode 1 count mode
TIM_COUNTERMODE_CENTERALIGNED2 Tim Central Snap mode 2 count mode
TIM_COUNTERMODE_CENTERALIGNED3 Tim Central Snap mode 3 count mode

Single chip microcomputer clock frequency 72MHZ,APB1 36MHz, so TIM2 automatic twice times to 72MHz, so the timer interrupt frequency is 72000000/36000/5=400hz

3. Enable timer interrupt Tim_cmd (TIM2, enable);

4. Configure Nvic.

Nvic_initstructure.nvic_irqchannel = Tim2_irqchannel;
nvic_initstructure.nvic_irqchannelpreemptionpriority = 0;
nvic_initstructure.nvic_irqchannelsubpriority = 4;
Nvic_initstructure.nvic_irqchannelcmd = ENABLE;
Nvic_init (&nvic_initstructure);

5. Write Interrupt function

void Tim2_irqhandler (void)

{

...//Interrupt handling

}

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.