STM32 Timer Relay

Source: Internet
Author: User

CORTEX-M3 's timer is only 16 bits, sometimes the count is greater than 16 bits, with two timers to make up 32 bits is very useful, and even 4 to receive 64-bit count.

The following code, in the Encoder 2 mode, the encoder is counted, TIM4 is a low 16-bit counter, TIM4 overflow once TIM3 add 1,tim3 set 2 times overflow. Keil this thing to the Chinese support is too poor, comments copied all can not see, but also simple, this just give the timer configuration, want to interrupt, but also with the interrupt.

void Tim4_external_clock_countingmode (void)
{
Gpio_inittypedef gpio_initstructure;
Tim_timebaseinittypedef tim_timebasestructure;
Rcc_apb2periphclockcmd (Rcc_apb2periph_gpiob, ENABLE);
/* Configure the PIN of the connector */
Gpio_initstructure.gpio_pin = gpio_pin_12| gpio_pin_13;
Gpio_initstructure.gpio_mode = gpio_mode_in_floating;
Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;
Gpio_init (Gpiod, &gpio_initstructure);

Rcc_apb1periphclockcmd (RCC_APB1PERIPH_TIM4, ENABLE);
Tim_deinit (TIM4);
Tim_timebasestructure.tim_period = 65535;
Tim_timebasestructure.tim_prescaler = 0x00;
Tim_timebasestructure.tim_clockdivision = 0x0;
Tim_timebasestructure.tim_countermode = tim_countermode_up;
Tim_timebaseinit (TIM4, &tim_timebasestructure);//Time Base configuration
Tim_encoderinterfaceconfig ( TIM4, tim_encodermode_ti12,tim_icpolarity_rising, tim_icpolarity_rising);//Encoder mode configuration
Tim_selectmasterslavemode ( TIM4, tim_masterslavemode_enable)//tim4 set to main mode, this can not be less
Tim_selectoutputtrigger (tim4,tim_trgosource_update); This is important, when setting the TIM4 overflow, the update event is generated, and the following TIM3 is the
Tim_clearflag (TIM4, tim_flag_update) with this update event as the clock source;//
Tim4->dier|=1 <<0; Allow interrupt
tim4->dier|=1<<6;/enable interrupt
Tim4->cnt=0;
Tim_cmd (tim4,disable);
}

void tim4_to_tim3 (void)//
{
Tim_timebaseinittypedef tim_timebasestructure;
Rcc_apb1periphclockcmd (RCC_APB1PERIPH_TIM3, ENABLE);
Tim_deinit (TIM3);//
Tim_itrxexternalclockconfig (TIM3, TIM_TS_ITR3);//select TIM4 as the trigger source
Tim_timebasestructure.tim_prescaler =0;//
Tim_timebasestructure.tim_clockdivision = tim_ckd_div1;//
Tim_timebasestructure.tim_countermode = tim_countermode_up;//
Tim_timebasestructure.tim_period =2-1;//
Tim_timebaseinit (tim3,&tim_timebasestructure);

Tim_selectslavemode (tim3,tim_slavemode_gated);//As Slave mode, passive trigger
Tim_setcounter (tim3,0);

Tim_clearflag (tim3,tim_flag_update);//
Tim_arrpreloadconfig (tim3,disable);//
Tim_itconfig (tim3,tim_it_update,enable);//

Tim_cmd (tim3,enable);
}

STM32 Timer Relay

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.