#define BLUE_LED_PIN GPIO_PIN_15
#define BLUE_LED_GPIO_PORT GPIOA
#define BLUE_LED_GPIO_CLK RCU_GPIOA
void led_config(void)
{
//pwm
rcu_periph_clock_enable(BLUE_LED_GPIO_CLK);
gpio_mode_set(BLUE_LED_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, BLUE_LED_PIN);
gpio_output_options_set(BLUE_LED_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,BLUE_LED_PIN);
gpio_af_set(GPIO_MODE_OUTPUT, GPIO_AF_2, BLUE_LED_PIN);
led_pwm_config(100,50);//10kHz, 50% duty_cycle占空比
}
void led_pwm_config(uint32_t freq_t,uint8_t duty_cycle)
{
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 = 72-1; //72分频=72M/72=1KHz时钟频率
timer_initpara.alignedmode = TIMER_COUNTER_EDGE; //边沿对齐
timer_initpara.counterdirection = TIMER_COUNTER_UP;//向上计数
timer_initpara.period = freq_t-1;//频率1KHz=72MHz/(71+1)/(999+1), so 0.1KHz=9999
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;//时钟分频1
timer_initpara.repetitioncounter = 0;//重复计数0
timer_init(TIMER1,&timer_initpara);//初始化配置
/* CH0 configuration in PWM mode1 */
timer_ocintpara.outputstate = TIMER_CCX_ENABLE; //使能输出比较通道
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;//输出极性高
timer_channel_output_config(TIMER1,TIMER_CH_0,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_0,duty_cycle-1);
timer_channel_output_mode_config(TIMER1,TIMER_CH_0,TIMER_OC_MODE_PWM1);
timer_channel_output_shadow_config(TIMER1,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER1);
/* auto-reload preload enable */
timer_enable(TIMER1);
}
使用:duty_val=1-101;
timer_disable(TIMER1);
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_0,duty_val);
timer_enable(TIMER1);
又是3天3夜,没搞定,有来请教了,哪里搞错了呢?
PA15百度说不支持PWM, 手册里也没看到说不支持。
我就换PA5 结果也不行。
|