//首先检测TIMER挂载在那个时钟下边(可挂载的时钟有CK_APB1,CK_APB2,CK_AHB),若不知道可通过
//tcu_clock_freq_get(CK_APB1)查看
uint32_t presc=100;//欲分频值
uint32_t period=100; //计数 //200 000 000 /presc/period=freq
//输出频率为f=200M/(100*100)=20K;==>200 000 000 /100*100=20K
void PWM_Init(uint16_t Dutycycle)//f=1k
{
//初始化TIMER外借引脚
rcu_periph_clock_enable(RCU_GPIOA);
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_8);
gpio_af_set(GPIOA,GPIO_AF_1,GPIO_PIN_8);
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_9);
gpio_af_set(GPIOA,GPIO_AF_1,GPIO_PIN_9);
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_10);
gpio_af_set(GPIOA,GPIO_AF_1,GPIO_PIN_10);
/* -----------------------------------------------------------------------
经查看本例程使用的时APB2 时钟为200M
----------------------------------------------------------------------- */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
uint16_t temp;
if((Dutycycle>=0)&&(Dutycycle<=100))temp=Dutycycle;//保证在0---100之间
rcu_periph_clock_enable(RCU_TIMER0);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);//
//TIMER0时钟为200M
//输出频率为200M/presc*period=200 000 000 /(100*2000)=1000
/* TIMER1 configuration */
timer_initpara.prescaler = presc-1;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period =period-1;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER0,&timer_initpara);
/* CH1,CH2 and CH3 configuration in PWM 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(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);
/* CH1 configuration in PWM mode1,duty cycle 25% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,25*160);//PA8
timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
/* CH2 configuration in PWM mode1,duty cycle 50% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,temp*(period/presc));//PA9 //8000=>50%
timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
/* CH3 configuration in PWM mode1,duty cycle 75% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,75*160); //PA10
timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
//timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, CCR);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER0);
/* auto-reload preload enable */
timer_primary_output_config(TIMER0,ENABLE);
timer_enable(TIMER0);
}
void main(){
PWM_Init(50);//设置占空比
timer_disable(TIMER0);
timer_autoreload_value_config (TIMER0, 200-1); //修改计数值
timer_prescaler_config(TIMER0, 100-1, TIMER_PSC_RELOAD_NOW);//重新预分频
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, (uint32_t)data*(period/presc));//修改占空比 data为需要的占空比,period=200,presc=100
timer_enable(TIMER0);
Delay_us(100);
}
以上为测试代码,经过验证有效.本例程只使用了TIMER0_CH1进行了参数配置,其他没有使用....