想使用定时器的比较反转模式,按照例程进行配置,发现比较模式不起作用,io的改变只是因为定时器的溢出进行的io反转,求各位大神指导!
void gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_AF);
/*configure PA1(TIMER1 CH1) as alternate function*/
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
}
/**
\brief configure the timer peripheral
\param[in] none
\param[out] none
\retval none
*/
void timer_config(void)
{
/* ---------------------------------------------------------------------------
TIMER1 configuration: output compare toggle mode:
TIMER1CLK = systemcoreclock / 5400 = 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);
timer_deinit(TIMER1);
/* TIMER1 configuration */
timer_initpara.prescaler = 108-1;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 9999;
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,4999);
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);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER1);
timer_enable(TIMER1);
}
int main(void)
{
int i=1;
rcu_apb2_clock_config(RCU_APB2_CKAHB_DIV1);
rcu_periph_clock_enable(RCU_GPIOC);
systick_config();
gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
USART0_init(115200U);
printf("system running -------\r\n");
gpio_config();
timer_config();
while(1)
{
TIMER_CAR(TIMER1) = 3000;
TIMER_CH1CV(TIMER1) = 2000;
printf("key press\r\n");
delay_ms(1000);
gpio_bit_set(GPIOC, GPIO_PIN_13);
delay_ms(1000);
gpio_bit_reset(GPIOC, GPIO_PIN_13);
}
return 0;
}
|