#defineClk_freq (10000)#defineCore_freq (168000000)Static voidTim_gpio_config (void) {gpio_inittypedef gpio_initstructure; //Enable Gpioa Clock__hal_rcc_gpioa_clk_enable (); //Configure PA8 pin as CLK output-to-CK InputGpio_initstructure.mode =gpio_mode_af_pp; Gpio_initstructure.pull=Gpio_nopull; Gpio_initstructure.pin=Gpio_pin_8; Gpio_initstructure.speed=Gpio_speed_high; Gpio_initstructure.alternate=gpio_af1_tim1; Hal_gpio_init (Gpioa,&gpio_initstructure); //Enable GPIOC Clock__hal_rcc_gpioc_clk_enable (); //Configure PC7 pin as CK input--from CLK Output
//Can not uses GPIO_MODE_INPUT:AF = 0 Gpio_initstructure.mode = gpio_mode_af_pp; //AF = 3 Gpio_initstructure.pull=Gpio_nopull; Gpio_initstructure.pin=gpio_pin_7; Gpio_initstructure.speed=Gpio_speed_high; Gpio_initstructure.alternate=GPIO_AF3_TIM8; Hal_gpio_init (GPIOC,&gpio_initstructure); //Configure PC6 pin as CK output--to oscilloscope DisplayGpio_initstructure.mode =gpio_mode_af_pp; Gpio_initstructure.pull=Gpio_nopull; Gpio_initstructure.pin=Gpio_pin_6; Gpio_initstructure.speed=Gpio_speed_high; Gpio_initstructure.alternate=GPIO_AF3_TIM8; Hal_gpio_init (GPIOC,&gpio_initstructure);} Tim_handletypedef Timhandle; Tim_masterconfigtypedef Smasterconfig; Tim_slaveconfigtypedef Sslaveconfig; Tim_oc_inittypedef Sconfig;Static voidError_Handler (void ){ while(1 ) { }}voidTim_clk_output (void ){__hal_rcc_tim1_clk_enable (); Timhandle.instance=TIM1; TimHandle.Init.Period=1; TimHandle.Init.Prescaler= Core_freq/clk_freq-1; TimHandle.Init.ClockDivision=0; TimHandle.Init.CounterMode=tim_countermode_up; if(Hal_tim_pwm_init (&timhandle)! =HAL_OK) Error_Handler (); Sconfig.ocmode=tim_ocmode_pwm1; Sconfig.ocpolarity=Tim_ocpolarity_high; Sconfig.ocfastmode=tim_ocfast_disable; Sconfig.pulse=1; if(Hal_tim_pwm_configchannel (&timhandle, &Sconfig, Tim_channel_1)!=HAL_OK) Error_Handler (); if(Hal_tim_pwm_start (&timhandle, tim_channel_1)! =HAL_OK) Error_Handler ();}voidTim_ck_input (void) {__hal_rcc_tim8_clk_enable (); Timhandle.instance=TIM8; Tim_ic_inittypedef Ic_inittypedef; Ic_inittypedef.icfilter=0; Ic_inittypedef.icpolarity=Tim_icpolarity_bothedge; Ic_inittypedef.icprescaler=Tim_icpsc_div1; Ic_inittypedef.icselection= Tim_icselection_directti;//TIM8_CH2:PC7Hal_tim_ic_configchannel (&timhandle, &ic_inittypedef, tim_channel_2); Tim_clockconfigtypedef Clockconfig; Clockconfig.clockfilter=0; Clockconfig.clockpolarity=Tim_clockpolarity_bothedge; Clockconfig.clockprescaler=Tim_clockprescaler_div1; Clockconfig.clocksource= Tim_clocksource_ti2;//TIM8_CH2:PC7Hal_tim_configclocksource (&timhandle, &clockconfig); TimHandle.Init.Period=1; TimHandle.Init.Prescaler=0; TimHandle.Init.ClockDivision=0; TimHandle.Init.CounterMode=tim_countermode_up; if(Hal_tim_base_init (&timhandle)! =HAL_OK) Error_Handler (); if(Hal_tim_base_start (&timhandle)! =HAL_OK) Error_Handler ();}voidTim_ck_output (void) {timhandle.instance=TIM8; Sconfig.ocmode=tim_ocmode_pwm1; Sconfig.ocpolarity= tim_ocpolarity_low;//inverted ck_input sconfig.ocfastmode=tim_ocfast_disable; Sconfig.pulse=1; if(Hal_tim_pwm_configchannel (&timhandle, &Sconfig, Tim_channel_1)!=HAL_OK) Error_Handler (); if(Hal_tim_pwm_start (&timhandle, tim_channel_1)! =HAL_OK) Error_Handler ();}voidTimer_demo (void) {tim_gpio_config (); Tim_clk_output (); //To Ck_input 5 KHz Tim_ck_input (); //From Clk_output 5 KHz Tim_ck_output (); //PWM Output 5 KHz--inverted cl_output with Delay while(1 ) { }}
STM32F4 Timer External Clock TI2 Both Edges Demo