void timer_config(void)
{
// PB8 具有 TIMER15 的通道0 功能
timer_parameter_struct Timer_ParamType;
timer_oc_parameter_struct Timer_OC_ParamType;
rcu_periph_clock_enable(RCU_GPIOB);//启用外设时钟功能
rcu_periph_clock_enable(RCU_TIMER15);
gpio_af_set(GPIOB,GPIO_AF_2,GPIO_PIN_8);//端口复用为TIMER15_CH0模式
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
gpio_bit_set(GPIOB, GPIO_PIN_8);
timer_internal_clock_config(TIMER15);
timer_deinit(TIMER15);
timer_channel_output_mode_config(TIMER15,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_auto_reload_shadow_enable(TIMER15);
Timer_OC_ParamType.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;//空闲时通道输出极性
Timer_OC_ParamType.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;//空闲时互补通道输出极性
Timer_OC_ParamType.ocnpolarity = TIMER_OCN_POLARITY_HIGH;//互补通道输出极性
Timer_OC_ParamType.ocpolarity = TIMER_OC_POLARITY_HIGH;//通道输出极性
Timer_OC_ParamType.outputnstate = TIMER_CCXN_DISABLE;//禁用互补通道输出
Timer_OC_ParamType.outputstate = TIMER_CCX_ENABLE;//使能通道输出
timer_channel_output_config(TIMER15,TIMER_CH_0,&Timer_OC_ParamType);
Timer_ParamType.alignedmode = TIMER_COUNTER_EDGE;//配置为边沿对其模式
Timer_ParamType.clockdivision = TIMER_CKDIV_DIV2;//配置死区时间和采样时钟(DTS)之间的分频系数
Timer_ParamType.counterdirection = TIMER_COUNTER_UP;//配置计数方向为向上计数
Timer_ParamType.period = 1000-1;//周期
Timer_ParamType.prescaler = 72-1;//预分频
Timer_ParamType.repetitioncounter = 1;
timer_init(TIMER15,&Timer_ParamType);
timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, 500-1);//比较寄存器的数值
timer_enable(TIMER15);
}
int main(void)
{
int16_t i = 0;
FlagStatus breathe_flag = SET;
/* configure the GPIO ports */
gpio_config();
/* configure the TIMER peripheral */
timer_config();
/* configure systick */
systick_config();
while (1){
/* delay a time in milliseconds */
delay_1ms(2);
if (SET == breathe_flag){
i = i + 10;
}else{
i = i - 10;
}
if(500 < i){
breathe_flag = RESET;
}
if(0 >= i){
breathe_flag = SET;
}
/* configure TIMER channel output pulse value */
timer_channel_output_pulse_value_config(TIMER15,TIMER_CH_0,i);
}
}
GD32f310c8 Timre13 这样配置是可以的,