代码使用到IO口有 PA8,PA9,PA10,PA13,PA14,PA15 库版本:V1.0.0, firmware for GD32F30x 实验现象:
void gpio_config(void) { rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_GPIOB); rcu_periph_clock_enable(RCU_AF);
/*configure PA8 PA9 PA10(TIMER0 CH0 CH1CH2) as alternate function*/ gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8); gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9); gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10);
/*configure PB13 PB14 PB15(TIMER0 CH0N CH1NCH2N) as alternate function*/ gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_13); gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_14); gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_15); }
voidtimer0_config(void) {
timer_oc_parameter_struct timer_ocintpara; timer_parameter_struct timer_initpara; timer_break_parameter_structtimer_breakpara;
rcu_periph_clock_enable(RCU_TIMER0);
timer_deinit(TIMER0);
/* TIMER0 configuration */ timer_initpara.prescaler = 119; timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = 1000; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_initpara.repetitioncounter = 0; timer_init(TIMER0,&timer_initpara);
/* CH0/CH0N,CH1/CH1N and CH2/CH2Nconfiguration in timing mode */ timer_ocintpara.outputstate = TIMER_CCX_ENABLE; timer_ocintpara.outputnstate =TIMER_CCXN_ENABLE;
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH; timer_ocintpara.ocnidlestate =TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara); timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara); timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,299); timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,299); timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,299); timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
/* TIMER0 primaryoutput function enable */ timer_primary_output_config(TIMER0,ENABLE);
/* TIMER0 counter enable */ timer_enable(TIMER0); }
int main(void) { gpio_config(); timer0_config(); while (1); }
资料来源于微信公众号“life_and_family”,更多相关资料可以关注下该公众号
|