GD32F450高级定时器六步换相测试

[复制链接]
4557|23
 楼主| zeshoufx 发表于 2022-11-11 22:09 | 显示全部楼层 |阅读模式
GD32F450的高级定时器很适合用来驱动电机,比如直流无刷和永磁同步。六步换相在直流无刷驱动里运用广泛。

定时器设置函数:
  1. #include "six_step.h"

  2. vu32 step = 1;

  3. void six_step_init(void)
  4. {
  5.         timer_oc_parameter_struct timer_ocintpara;
  6.     timer_parameter_struct timer_initpara;
  7.     timer_break_parameter_struct timer_breakpara;
  8.          
  9.         rcu_periph_clock_enable(RCU_GPIOE);
  10.         rcu_periph_clock_enable(RCU_TIMER0);
  11.         rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
  12.        
  13.         gpio_mode_set(GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_8);
  14.         gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_MAX,GPIO_PIN_8);
  15.         gpio_af_set(GPIOE,GPIO_AF_1,GPIO_PIN_8);
  16.        
  17.         gpio_mode_set(GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_9);
  18.         gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_MAX,GPIO_PIN_9);
  19.         gpio_af_set(GPIOE,GPIO_AF_1,GPIO_PIN_9);
  20.        
  21.         gpio_mode_set(GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_10);
  22.         gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_MAX,GPIO_PIN_10);
  23.         gpio_af_set(GPIOE,GPIO_AF_1,GPIO_PIN_10);
  24.        
  25.         gpio_mode_set(GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_11);
  26.         gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_MAX,GPIO_PIN_11);
  27.         gpio_af_set(GPIOE,GPIO_AF_1,GPIO_PIN_11);
  28.        
  29.         gpio_mode_set(GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_12);
  30.         gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_MAX,GPIO_PIN_12);
  31.         gpio_af_set(GPIOE,GPIO_AF_1,GPIO_PIN_12);
  32.        
  33.         gpio_mode_set(GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_13);
  34.         gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_MAX,GPIO_PIN_13);
  35.         gpio_af_set(GPIOE,GPIO_AF_1,GPIO_PIN_13);
  36.        
  37.         gpio_mode_set(GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_14);
  38.         gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_MAX,GPIO_PIN_14);
  39.         gpio_af_set(GPIOE,GPIO_AF_1,GPIO_PIN_14);
  40.        
  41.         gpio_mode_set(GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_15);
  42.         gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_MAX,GPIO_PIN_15);
  43.         gpio_af_set(GPIOE,GPIO_AF_1,GPIO_PIN_15);
  44.        
  45.         timer_initpara.alignedmode=TIMER_COUNTER_EDGE;
  46.         timer_initpara.clockdivision=TIMER_CKDIV_DIV1;
  47.         timer_initpara.counterdirection=TIMER_COUNTER_UP;
  48.         timer_initpara.period=599;
  49.         timer_initpara.prescaler=199;
  50.         timer_initpara.repetitioncounter=0;
  51.         timer_init(TIMER0,&timer_initpara);
  52.        
  53.         timer_ocintpara.ocidlestate=TIMER_OC_IDLE_STATE_HIGH;
  54.         timer_ocintpara.ocnidlestate=TIMER_OCN_IDLE_STATE_HIGH;
  55.         timer_ocintpara.ocpolarity=TIMER_OC_POLARITY_HIGH;
  56.         timer_ocintpara.ocnpolarity=TIMER_OCN_POLARITY_HIGH;
  57.         timer_ocintpara.outputstate=TIMER_CCX_ENABLE;
  58.         timer_ocintpara.outputnstate=TIMER_CCXN_ENABLE;
  59.        
  60.         timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  61.     timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
  62.     timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
  63.        
  64.         timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,299);
  65.     timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_TIMING);
  66.     timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);

  67.     timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,299);
  68.     timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_TIMING);
  69.     timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_ENABLE);

  70.     timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,299);
  71.     timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_TIMING);
  72.     timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_ENABLE);
  73.        
  74.         timer_breakpara.runoffstate= TIMER_ROS_STATE_ENABLE;
  75.     timer_breakpara.ideloffstate= TIMER_IOS_STATE_ENABLE ;
  76.     timer_breakpara.deadtime= 164;
  77.     timer_breakpara.breakpolarity= TIMER_BREAK_POLARITY_LOW;
  78.     timer_breakpara.outputautostate= TIMER_OUTAUTO_ENABLE;
  79.     timer_breakpara.protectmode= TIMER_CCHP_PROT_OFF;
  80.     timer_breakpara.breakstate= TIMER_BREAK_ENABLE;
  81.     timer_break_config(TIMER0,&timer_breakpara);
  82.        
  83.         timer_primary_output_config(TIMER0,ENABLE);
  84.        
  85.         timer_interrupt_enable(TIMER0,TIMER_INT_CMT);
  86.         timer_interrupt_disable(TIMER0,TIMER_INT_BRK);
  87.         nvic_irq_enable(TIMER0_TRG_CMT_TIMER10_IRQn,1,1);
  88.        
  89.         timer_enable(TIMER0);
  90. }


  91. void TIMER0_TRG_CMT_TIMER10_IRQHandler(void)
  92. {
  93.     /* clear TIMER interrupt flag */
  94.     timer_interrupt_flag_clear(TIMER0,TIMER_INT_CMT);

  95.     switch(step){
  96.     /* next step: step 2 configuration .A-C` breakover---------------------------- */
  97.     case 1:
  98.         /*  channel0 configuration */
  99.         timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  100.         timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_ENABLE);
  101.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_DISABLE);

  102.         /*  channel1 configuration */
  103.         timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_DISABLE);
  104.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_DISABLE);

  105.         /*  channel2 configuration */
  106.         timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  107.         timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_DISABLE);
  108.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);

  109.         step++;
  110.         break;

  111.     /* next step: step 3 configuration .B-C` breakover---------------------------- */
  112.     case 2:
  113.         /*  channel0 configuration */
  114.         timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_DISABLE);
  115.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_DISABLE);

  116.         /*  channel1 configuration */
  117.         timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  118.         timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_ENABLE);
  119.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_DISABLE);

  120.         /*  channel2 configuration */
  121.         timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  122.         timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_DISABLE);
  123.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);

  124.         step++;
  125.         break;

  126.     /* next step: step 4 configuration .B-A` breakover---------------------------- */
  127.     case 3:
  128.         /*  channel0 configuration */
  129.         timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  130.         timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_DISABLE);
  131.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_ENABLE);

  132.         /*  channel1 configuration */
  133.         timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  134.         timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_ENABLE);
  135.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_DISABLE);

  136.         /*  channel2 configuration */
  137.         timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_DISABLE);
  138.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_DISABLE);

  139.         step++;
  140.         break;

  141.     /* next step: step 5 configuration .C-A` breakover---------------------------- */
  142.     case 4:
  143.         /*  channel0 configuration */
  144.         timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  145.         timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_DISABLE);
  146.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_ENABLE);

  147.         /*  channel1 configuration */      
  148.         timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_DISABLE);
  149.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_DISABLE);

  150.         /*  channel2 configuration */
  151.         timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  152.         timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_ENABLE);
  153.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_DISABLE);

  154.         step++;
  155.         break;

  156.     /* next step: step 6 configuration .C-B` breakover---------------------------- */
  157.     case 5:
  158.         /*  channel0 configuration */
  159.         timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_DISABLE);
  160.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_DISABLE);

  161.         /*  channel1 configuration */
  162.         timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  163.         timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_DISABLE);
  164.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_ENABLE);

  165.         /*  channel2 configuration */
  166.         timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  167.         timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_ENABLE);
  168.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_DISABLE);

  169.         step++;
  170.         break;
  171.   
  172.     /* next step: step 1 configuration .A-B` breakover---------------------------- */        
  173.     case 6:
  174.         /*  channel0 configuration */
  175.         timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  176.         timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_ENABLE);
  177.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_DISABLE);

  178.         /*  channel1 configuration */
  179.         timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  180.         timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_DISABLE);
  181.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_ENABLE);

  182.         /*  channel2 configuration */
  183.         timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_DISABLE);
  184.         timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_DISABLE);

  185.         step = 1;
  186.         break;
  187.     }
  188. }

  189. static u32 delay;

  190. void systick_config(void)
  191. {
  192.     /* setup systick timer for 1000Hz interrupts */
  193.     if (SysTick_Config(SystemCoreClock / 100U)){
  194.         /* capture error */
  195.         while (1){
  196.         }
  197.     }
  198.     /* configure the systick handler priority */
  199.     NVIC_SetPriority(SysTick_IRQn, 0x00U);
  200. }

  201. /*!
  202.     \brief      delay a time in milliseconds
  203.     \param[in]  count: count in milliseconds
  204.     \param[out] none
  205.     \retval     none
  206. */
  207. void delay_1ms(uint32_t count)
  208. {
  209.     delay = count;

  210.     while(0U != delay){
  211.     }
  212. }

  213. /*!
  214.     \brief      delay decrement
  215.     \param[in]  none
  216.     \param[out] none
  217.     \retval     none
  218. */
  219. void delay_decrement(void)
  220. {
  221.     if (0U != delay){
  222.         delay--;
  223.     }
  224. }

  225. void SysTick_Handler(void)
  226. {
  227.     timer_event_software_generate(TIMER0,TIMER_EVENT_SRC_CMTG);
  228. }
主函数:

  1. #include "bitband.h"
  2. #include "usart.h"
  3. #include "systick.h"
  4. #include "led.h"
  5. #include "six_step.h"

  6. /*!
  7.     \brief    main function
  8.     \param[in]  none
  9.     \param[out] none
  10.     \retval     none
  11. */




  12. int main(void)
  13. {
  14.         nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  15. //        systick_set(200);
  16. //        usart_init(9600);
  17. //        led_init();
  18. //        printf("hello c language...\r\n");
  19.         systick_config();
  20.         six_step_init();
  21.     while(1)
  22.         {
  23. //                printf("hello gd32...\r\n");
  24. //                led_toggle();
  25. //                delay_ms(750);
  26.     }
  27. }


结果:




3

3

2

2

1

1
豌豆爹 发表于 2022-11-15 13:31 来自手机 | 显示全部楼层
这个高级定时器挺厉害的
 楼主| zeshoufx 发表于 2022-11-16 09:04 | 显示全部楼层
豌豆爹 发表于 2022-11-15 13:31
这个高级定时器挺厉害的

可以的
chenqianqian 发表于 2022-11-19 11:18 来自手机 | 显示全部楼层
这个效果看起来不错啊
daichaodai 发表于 2022-11-19 17:37 来自手机 | 显示全部楼层
实际测试效果如何,稳不稳?
 楼主| zeshoufx 发表于 2022-11-20 13:55 | 显示全部楼层
chenqianqian 发表于 2022-11-19 11:18
这个效果看起来不错啊

是的
 楼主| zeshoufx 发表于 2022-11-20 13:56 | 显示全部楼层
daichaodai 发表于 2022-11-19 17:37
实际测试效果如何,稳不稳?

还没正式测试,,
OKAKAKO 发表于 2022-11-24 21:35 | 显示全部楼层
这个高级定时器效果非常好,各个波形也稳定
 楼主| zeshoufx 发表于 2022-11-25 16:57 | 显示全部楼层
OKAKAKO 发表于 2022-11-24 21:35
这个高级定时器效果非常好,各个波形也稳定

你们也在测试吗
OKAKAKO 发表于 2022-11-25 21:19 | 显示全部楼层

看你的波形输出确实挺稳定,以前做过类似的
 楼主| zeshoufx 发表于 2022-11-26 19:58 | 显示全部楼层
OKAKAKO 发表于 2022-11-25 21:19
看你的波形输出确实挺稳定,以前做过类似的

哦哦,,你们主要是做电机控制这块吗
OKAKAKO 发表于 2022-11-27 10:47 | 显示全部楼层
zeshoufx 发表于 2022-11-26 19:58
哦哦,,你们主要是做电机控制这块吗

我不是这个方向的,参与过类似的项目而已
tpgf 发表于 2022-12-5 09:56 | 显示全部楼层
请问六步换相是一种什么方式呢 适用于什么种类的电机呢
qcliu 发表于 2022-12-5 11:06 | 显示全部楼层
tpgf 发表于 2022-12-5 09:56
请问六步换相是一种什么方式呢 适用于什么种类的电机呢

电子换相无刷电机要对转子永磁体位置进行精确检测,并用电子开关切换不同绕组通电以获得持续向前的动力,在目前的绝大多数电动车三相无刷电机中均使用三个开关式的霍尔传感器检测永磁体相对于定子线圈的位置,控制器跟据三个霍尔传感器输出的六种不同信号输出相应的控制信号驱动电子开关向马达供电。这就是所谓六步换相法
drer 发表于 2022-12-5 12:03 | 显示全部楼层
从电机原理可以看出,换相必须及时,否则会导致电机失步,从而使电机噪音增大,效率降低
coshi 发表于 2022-12-5 13:05 | 显示全部楼层
为什么只有高级定时器适合呢   需要特殊的控制吗
kxsi 发表于 2022-12-5 14:12 | 显示全部楼层
coshi 发表于 2022-12-5 13:05
为什么只有高级定时器适合呢   需要特殊的控制吗

普通的定时器应该不能输出pwm波
wiba 发表于 2022-12-5 14:25 | 显示全部楼层
OKAKAKO 发表于 2022-11-24 21:35
这个高级定时器效果非常好,各个波形也稳定

有没有测试过抗干扰能力呢  另外和频率挂钩吗
 楼主| zeshoufx 发表于 2022-12-5 14:43 | 显示全部楼层
coshi 发表于 2022-12-5 13:05
为什么只有高级定时器适合呢   需要特殊的控制吗

高级定时器可以输出互补的PWM,方便设置死区,刹车
 楼主| zeshoufx 发表于 2022-12-5 14:43 | 显示全部楼层
drer 发表于 2022-12-5 12:03
从电机原理可以看出,换相必须及时,否则会导致电机失步,从而使电机噪音增大,效率降低 ...

是的,,,
您需要登录后才可以回帖 登录 | 注册

本版积分规则

67

主题

1991

帖子

15

粉丝
快速回复 在线客服 返回列表 返回顶部