想问下 GD32F407VE 怎么配置定时器发出来PWM,单次触发模式,就是每次发出来固定数量的方波
rcu_periph_clock_enable(RCU_GPIOE);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_GPIOA);
//ch0
gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_9);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, GPIO_PIN_9);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
//ch1
gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_11);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, GPIO_PIN_11);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
//ch2
gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_13);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, GPIO_PIN_13);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
//ch3
gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_14);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, GPIO_PIN_14);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_14);
timer_parameter_struct timer_initpara;
timer_oc_parameter_struct timer_ocintpara;
rcu_periph_clock_enable(RCU_TIMER0);
timer_internal_clock_config(TIMER0);
/* 定时器0 配置 */
timer_initpara.prescaler = 167; // 预分频器配置
timer_initpara.alignedmode = TIMER_COUNTER_EDGE; // 边沿对齐模式
timer_initpara.period = 999; // 溢出值
timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER0, &timer_initpara); // 定时器0 初始化
//PWM输出配置
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_LOW;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_LOW;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_counter_value_config(TIMER0, 0);
/* 对PWM模式中的通道0 进行配置 */
timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
/* 通道0 的占空比设置 */
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, 499);// 50%占空比
timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
/* 对PWM模式中的通道1 进行配置 */
timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
/* 通道1 的占空比设置 */
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, 499);// 50%占空比
timer_channel_output_mode_config(TIMER0, TIMER_CH_1,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0, TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
/* 对PWM模式中的通道2 进行配置 */
timer_channel_output_config(TIMER0,TIMER_CH_2, &timer_ocintpara);
/* 通道2 的占空比设置 */
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, 499);// 50%占空比
timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
/* 对PWM模式中的通道3 进行配置 */
timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocintpara);
/* 通道3 的占空比设置 */
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, 499);// 50%占空比
timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
timer_single_pulse_mode_config(TIMER0, TIMER_SP_MODE_SINGLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER0);
timer_autoreload_value_config(TIMER0, cycle);
timer_repetition_value_config(TIMER0, pulse_num);
timer_prescaler_config(TIMER0, psc, TIMER_PSC_RELOAD_NOW);
/* 通道0 的占空比设置 */
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, ch0_duty);// 50%占空比
/* 通道1 的占空比设置 */
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, ch1_duty);// 50%占空比
/* 通道2 的占空比设置 */
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, ch2_duty);// 50%占空比
/* 通道3 的占空比设置 */
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, ch3_duty);// 50%占空比
timer_single_pulse_mode_config(TIMER0, TIMER_SP_MODE_SINGLE);
timer_enable(TIMER0); // 使能定时器0
程序配置如上,不能发出波形
|
![](//uc2.21ic.com/avatar.php?uid=1055054&size=small) 共1人点赞
|