GD32F103定时器输出PWM

#define TIMER								TIMER3
#define TIMER_CH							TIMER_CH_2
#define RCC_TIMER							RCU_TIMER3
#define TIMER_PORT							GPIOB
#define TIMER_BG_PIN						GPIO_PIN_8		//1:亮 		 0:不亮	

 


/* Timer3_CH3 PWM输出 */
void Timer3_Init(void)
{
	/* 使用SW下载,不使用JTAG下载,管脚用作其它功能 */
	gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);
	rcu_periph_clock_enable(RCC_TIMER);
	rcu_periph_clock_enable(RCU_GPIOB);
	/* LCD_BG使用Timer3_CH3 PWM输出控制亮度(10kHz) */
	gpio_init(TIMER_PORT,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,TIMER_BG_PIN);
	timer_parameter_struct timer3_init;
	timer_oc_parameter_struct timer3_ocintpara;
	
	timer_deinit(TIMER);
	
	/* TIMER3 configuration */
	timer3_init.prescaler         = 107;
	timer3_init.alignedmode       = TIMER_COUNTER_EDGE;
	timer3_init.counterdirection  = TIMER_COUNTER_UP;
	timer3_init.period            = 99;							//10kHz
	timer3_init.clockdivision     = TIMER_CKDIV_DIV1;
	timer3_init.repetitioncounter = 0;
	timer_init(TIMER3,&timer3_init);
	
	/* CH3 configuration in PWM mode */
	timer3_ocintpara.outputstate  = TIMER_CCX_ENABLE;
	timer3_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
	timer3_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
	timer3_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
	timer3_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
	timer3_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

	timer_channel_output_config(TIMER,TIMER_CH,&timer3_ocintpara);
	timer_primary_output_config(TIMER,ENABLE);
	/* CH3 configuration in PWM mode0,duty cycle 30% */
	timer_channel_output_pulse_value_config(TIMER,TIMER_CH,90);
	timer_channel_output_mode_config(TIMER,TIMER_CH,TIMER_OC_MODE_PWM0);
	timer_channel_output_shadow_config(TIMER,TIMER_CH,TIMER_OC_SHADOW_DISABLE);
	
	/* auto-reload preload enable */
	timer_auto_reload_shadow_enable(TIMER);
	
	/* enable TIMER3 */
	timer_enable(TIMER);
}

你可能感兴趣的:(嵌入式,c语言,开发语言,后端)