- 配置GPIO端口(GD32F4)
void gpio_config(void) { rcu_periph_clock_enable(RCU_GPIOB); /*configure PB3(TIMER1 CH1) as alternate function*/ gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3); //PB,复用,无上下拉,pin3 gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3); //PB,推挽,引脚速度50MHz,pin3 gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_3); //复用功能 }
(From:DataSheet Table 2-4. GD32F450Zx LQFP144 pin definitions )
(From:DataSheet Table 2-6. Port A alternate functions summary)
- 配置定时器外设
void timer_config(void) { /* --------------------------------------------------------------------------- TIMER1 configuration: output compare toggle mode: TIMER1CLK = systemcoreclock / 10000=20K, CH1 update rate = TIMER1 counter clock / CH1VAL = 20000/4000 = 5 Hz ----------------------------------------------------------------------------*/ timer_oc_parameter_struct timer_ocintpara; timer_parameter_struct timer_initpara; rcu_periph_clock_enable(RCU_TIMER1); rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4); //配置TIMER时钟预分频 timer_deinit(TIMER1); //复位外设TIMERx /* TIMER1 configuration */ timer_initpara.prescaler = 9999; timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = 3999; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_initpara.repetitioncounter = 0; timer_init(TIMER1,&timer_initpara); /* CH1 configuration in OC TOGGLE mode */ timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; timer_ocintpara.outputstate = TIMER_CCX_ENABLE; timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE; timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW; timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW; timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara); timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,3999); //配置外设TIMER1的通道1输出比较值 timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_TOGGLE); //匹配时翻转 timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE); //配置TIMERx通道输出比较影子寄存器功能 /* auto-reload preload enable */ timer_auto_reload_shadow_enable(TIMER1); //TIMERx自动重载影子使能 timer_enable(TIMER1); }
- 主函数
int main(void) { gpio_config(); timer_config(); while (1); }