GD32F407输出占空比和频率可调的代码

//首先检测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进行了参数配置,其他没有使用....

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值