- void timer0_gpio_config(void)
- {
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_GPIOB);
- /*configure PA8/PA9/PA10(TIMER0/CH0/CH1/CH2) as alternate function*/
- gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
- gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
- gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
-
- gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
- gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_8);
- gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_9);
- gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_10);
- gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_11);
- /*configure PB13/PB14/PB15(TIMER0/CH0N/CH1N/CH2N) as alternate function*/
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_14);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_14);
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_15);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_15);
- gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_13);
- gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_14);
- gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_15);
- /*configure PB12(TIMER0 BKIN) as alternate function*/
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);
- gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_12);
- }
- void timer0_config(void)
- {
- /* -----------------------------------------------------------------------
- TIMER0 configuration:
- generate 3 complementary PWM signal.
- TIMER0CLK is fixed to systemcoreclock, the TIMER0 prescaler is equal to 1
- so the TIMER0 counter clock used is 72MHz.
- insert a dead time equal to 164/systemcoreclock = 2.28us
- configure the break feature, active at low level, and using the automatic
- output enable feature.
- use the locking parameters level 0.
- ----------------------------------------------------------------------- */
- timer_oc_parameter_struct timer_ocinitpara;
- timer_parameter_struct timer_initpara;
- timer_break_parameter_struct timer_breakpara;
- /* enable the TIMER clock */
- rcu_periph_clock_enable(RCU_TIMER0);
- /* deinit a TIMER */
- timer_deinit(TIMER0);
- /* initialize TIMER init parameter struct */
- timer_struct_para_init(&timer_initpara);
- /* TIMER0 configuration */
- timer_initpara.prescaler = 72 - 1;
- timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 1500 - 1;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER0, &timer_initpara);
- /* initialize TIMER channel output parameter struct */
- timer_channel_output_struct_para_init(&timer_ocinitpara);
- /* CH0/CH0N, CH1/CH1N and CH2/CH2N configuration in timing mode */
- timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
- timer_ocinitpara.outputnstate = TIMER_CCXN_ENABLE;
- timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocinitpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
- timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
- timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
- timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocinitpara);
- timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocinitpara);
- timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocinitpara);
-
- timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
- timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocinitpara);
- /* configure TIMER channel 0 */
- timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, TIM0_DUTY_INIT - 1);
- timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM1); //PWM输出模式1
- timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
-
- /* configure TIMER channel 1 */
- timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, TIM0_DUTY_INIT - 1);
- timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM1);
- timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
- /* configure TIMER channel 2 */
- timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, TIM0_DUTY_INIT - 1);
- timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM1);
- timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
-
- /* configure TIMER channel 3 */
- timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, 4000);
- timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_PWM1);
- timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
- /* initialize TIMER break parameter struct */
- timer_break_struct_para_init(&timer_breakpara);
- /* automatic output enable, break, dead time and lock configuration*/
- timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
- timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE;
- timer_breakpara.deadtime = DEAD_TIME;
- timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
- timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
- timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF; //互补寄存器写保护模式
- timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
- timer_break_config(TIMER0, &timer_breakpara);
-
- /* TIMER0 primary output function enable */
- timer_primary_output_config(TIMER0, ENABLE);
- /* TIMER0 break/channel interrupt enable */
- timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_BRK);
- timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_CH3);
- timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);
- timer_interrupt_enable(TIMER0, TIMER_INT_BRK);
- timer_interrupt_enable(TIMER0, TIMER_INT_CH3);
- timer_interrupt_enable(TIMER0, TIMER_INT_UP);
-
- /* TIMER0 counter enable */
- timer_enable(TIMER0)
- }
配置通道3的比较器值为4000,大于计数周期1499,硬件Debug截图如下:
大图
小图
具体结果为:当通道3的比较器值设为4000,也会触发进入比较中断!且在调试句柄执行完标志清零语句后,通道中断标志位CH3IF仍然处于置位状态!!(可以参考图中红框标记)更改通道3的输出比较方式、有效电平极性产生的现象均相同!!只有在比较器值设为0的时候才不会进入中断!!!很迷惑,新人请教,希望大家帮帮忙解答!!!