回答

收藏

GD32方案分享⑧:PWM实验

GD32 GD32 4383 人阅读 | 0 人回复 | 2017-05-27

本帖最后由 何昌昕 于 2017-5-27 23:47 编辑

因为我们需要驱动电机,所以我们需要输出PWM波,参照例程中的PWM例子进行修改

PWM频率 = (当前主频(200Mhz)/(period+1))/(prescaler+1)
通道占空比 = (duty/period)*100%


分别输出了 50% 10Khz的PWM波 90% 10Khz的PWM波

  1. #include "PWM.h"

  2. void PWM_Init(void)
  3. {
  4.    rcu_periph_clock_enable(RCU_GPIOB);
  5.    
  6.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
  7.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);

  8.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
  9.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);

  10.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
  11.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);

  12.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_3);
  13.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
  14.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
  15.     timer_oc_parameter_struct timer_ocintpara;
  16.     timer_parameter_struct timer_initpara;

  17.     rcu_periph_clock_enable(RCU_TIMER1);
  18.     rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);

  19.     timer_deinit(TIMER1);

  20.     /* TIMER1 configuration */
  21.     timer_initpara.prescaler         = 1;
  22.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  23.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  24.     timer_initpara.period            = 9999;
  25.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  26.     timer_initpara.repetitioncounter = 0;
  27.     timer_init(TIMER1,&timer_initpara);

  28.     /* CH1,CH2 and CH3 configuration in PWM mode1 */
  29.     timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;
  30.     timer_ocintpara.outputstate = TIMER_CCX_ENABLE;

  31.     timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
  32.     timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
  33.     timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);

  34.     /* CH1 configuration in PWM mode1,duty cycle 25% */
  35.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,1999);
  36.     timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  37.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

  38.     /* CH2 configuration in PWM mode1,duty cycle 50% */
  39.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,4999);
  40.     timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  41.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

  42.     /* CH3 configuration in PWM mode1,duty cycle 75% */
  43.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,8999);
  44.     timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
  45.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);

  46.     /* auto-reload preload enable */
  47.     timer_auto_reload_shadow_enable(TIMER1);
  48.     /* auto-reload preload enable */
  49.     timer_enable(TIMER1);
  50. }
复制代码
明天开始搭建小车硬件 附件:
myGD32 - 副本.rar (7.42 MB, 下载次数: 16)
  1. #include "include.h"

  2. //extern int LDC_val;

  3. int main(void)
  4. {
  5.     systick_config();
  6.         
  7.           UART_Init(Debug_UART,115200);
  8.     LED_Init();         
  9.           //LDC1000_Init();
  10.         
  11.           PWM_Init();
  12.           while(1)
  13.          {
  14.                           LED_Troggle();
  15.                     delay_1ms(100);
  16.                     //LDC_val=LDC_Read_Avr()/10;//采样滤波
  17.                     //printf("当前通道值:%d\r\n",LDC_val);
  18.     }
  19. }
复制代码
分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条