TA的每日心情 | 衰 2017-11-27 16:33 |
---|
签到天数: 7 天 连续签到: 1 天 [LV.3]偶尔看看II
|
本帖最后由 何昌昕 于 2017-5-27 23:47 编辑
因为我们需要驱动电机,所以我们需要输出PWM波,参照例程中的PWM例子进行修改
PWM频率 = (当前主频(200Mhz)/(period+1))/(prescaler+1)
通道占空比 = (duty/period)*100%
分别输出了 50% 10Khz的PWM波 90% 10Khz的PWM波
- #include "PWM.h"
- void PWM_Init(void)
- {
- rcu_periph_clock_enable(RCU_GPIOB);
-
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_3);
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
- rcu_periph_clock_enable(RCU_TIMER1);
- rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
- timer_deinit(TIMER1);
- /* TIMER1 configuration */
- timer_initpara.prescaler = 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,CH2 and CH3 configuration in PWM mode1 */
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
- timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
- timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);
- /* CH1 configuration in PWM mode1,duty cycle 25% */
- timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,1999);
- timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
- /* CH2 configuration in PWM mode1,duty cycle 50% */
- timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,4999);
- timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
- /* CH3 configuration in PWM mode1,duty cycle 75% */
- timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,8999);
- timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER1);
- /* auto-reload preload enable */
- timer_enable(TIMER1);
- }
复制代码 明天开始搭建小车硬件 附件:
myGD32 - 副本.rar
(7.42 MB, 下载次数: 16)
|
|