STM32 Universal Timer Configuration

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

}

Stm32_timer Basic Timer configuration and realization of light flashing

There are 11 timers in the STM32, including 2 advanced control timers, 4 normal timers and 2 basic timers, as well as 2 watchdog timers and a system tick timer.

TIM1 and TIM8 are capable of producing 3 pairs of PWM complementary output of the high-level, is often used for three-phase motor drive, the clock is generated by the APB2 output, TIM2-TIM5 is a normal timer, TIM6 and TIM7 is the basic timer, its clock is generated by the APB1 output;


This experiment is to realize the function is: with the ordinary timer TIM2 every second an update event, into the Interrupt service program rollover LED1 state.


Pre-Knowledge:

The ①stm32 Universal Timer TIM2 is a 16-bit auto-reload counter.

② Up-counting mode: counting from 0 to 0 when the value in the Auto-load register (TIMX_ARR) is cleared, loop in turn.

Two questions that need to be clarified:

1. What is the counter count frequency?

This problem involves the RCC clock section, as shown in the following figure:

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.

Note: The peripherals hanging on the APB1 and APB2 are shown in the figure:

The timer count frequency has a formula:

TIMX_CLK = Ck_int/(Tim_prescaler + 1)

Where: The count frequency of the TIMX_CLK timer

Ck_int internal clock Source frequency (APB1-frequency multiplier to send clock)

Tim_prescaler user-defined Prescaler coefficient, the value range is 0~65535.

For example: RCC in Ahb=72mhz, Apb1=36mhz, Apb2=72mhz, then Ck_int=72mkz.

2. How to calculate the timing time.

In the above formula Tim_prescaler relates to register TIMX_PSC

If Tim_prescaler is set to 36000, it is indicated by the above formula:

The timer count frequency timx_clk = 72mkz/36000 = 2000HZ, the timer count period =1/2000hz=0.5ms.

If you want to schedule 1 seconds, you need to Count 2000 times, which is also the value of automatic reload. It also involves Timx_arr.

As long as these two problems are clear, the rest is to set the corresponding bit of register.

The LED hardware connection is shown in the following figure: High-level lit LEDs.

First step: Configure the system clock. See stm32f103x RCC Register configuration

In addition, the GPIO and TIM2 peripheral clocks need to be turned on.

/* Enable GPIOC clock */Rcc_apb2periphclockcmd (RCC_APB2PERIPH_GPIOC, enable); /* Enable TIM2 clock */Rcc_apb1periphclockcmd (rcc_apb1periph_tim2, enable);

Note: TIM2 is hanging on the APB1, do not write wrong when opening the clock, call rcc_apb1periphclockcmd function, not rcc_apb2periphclockcmd.


Step Two: Configure the interrupt vector table. See Stm32_exti (including Nvic) configuration and library function explanation

void nvic_configuration (void) {nvic_inittypedef nvic_initstructure; #ifdef vect_tab_ram/* Set the Vector Table base L Ocation at 0x20000000 */nvic_setvectortable (Nvic_vecttab_ram, 0x0); #else/* Vect_tab_flash */* Set the Vector Table base location at 0x08000000 */nvic_setvectortable (Nvic_vecttab_flash, 0x0); #endif/* Configure one bit for preemption priority */Nvic_prioritygroupconfig (nvic_prioritygroup_1); /* Enable the TIM2 Interrupt */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); }

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.