GD32E230C8T6驱动带霍尔传感器的无刷直流减速电机

前面发布的文章虽然把电机驱动,但是电机抖动换相有问题,找了很久的解决办法今天将电机完美驱动,无任何问题。还是一样先看我上一个文章,再看这个文章,没基础的先找资料学习基础,我直接上程序。

void gpio_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
	
	
//    gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN_0);
//    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
	

    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
    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_9);
    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_10);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
 
    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_8);
    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_9);
    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_10);
 
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
 
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_14);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_14);
 
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_15);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
 
    gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_13);
    gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_14);
    gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_15);
    //霍尔引脚初始化
		gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_4);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);//HB
		gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_4);
		
		gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_5);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);//HA
		gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_5);
		
		gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_8);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);//HC
		gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_8);	 
		
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_0);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);//HC
		gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_0);			
}

void timer0_config(void)
{
 
    timer_oc_parameter_struct timer_ocintpara={0};
    timer_parameter_struct timer_initpara={0};
    timer_break_parameter_struct timer_Breakinitpara={0};
	
	  timer_ic_parameter_struct timer_icinitpara = {0};
    timer_parameter_struct timer_initpara2={0};
	
	  gpio_config();
    rcu_periph_clock_enable(RCU_TIMER0);
		rcu_periph_clock_enable(RCU_TIMER2);
		
		timer_deinit(TIMER2);

    timer_initpara2.prescaler         = 71;  // 预分频:72MHz / 72 = 1MHz
    timer_initpara2.alignedmode       = TIMER_COUNTER_EDGE;  
    timer_initpara2.counterdirection  = TIMER_COUNTER_UP;  // 向上计数模式
    timer_initpara2.period            = (65536 - 1);  
    timer_initpara2.clockdivision     = TIMER_CKDIV_DIV1;  // 输入时钟1分频
    timer_init(TIMER2,&timer_initpara2);

    timer_icinitpara.icpolarity  = TIMER_IC_POLARITY_BOTH_EDGE; 
    timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
    timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;  // 时钟1分频
    timer_icinitpara.icfilter =5;
		
    timer_input_capture_config(TIMER2, TIMER_CH_0, &timer_icinitpara);
		timer_input_capture_config(TIMER2, TIMER_CH_1, &timer_icinitpara);
		timer_input_capture_config(TIMER2, TIMER_CH_2, &timer_icinitpara);
		timer_hall_mode_config(TIMER2,TIMER_HALLINTERFACE_ENABLE);//使能定时器霍尔传感器模式
		
	  timer_master_output_trigger_source_select(TIMER2,TIMER_TRI_OUT_SRC_CH0);//触发源为捕获比较触发 主模式
		
	  timer_channel_control_shadow_config(TIMER2,ENABLE );
		timer_auto_reload_shadow_enable(TIMER2);// 使能重装载影子
		
		
		timer_master_slave_mode_config(TIMER2, TIMER_MASTER_SLAVE_MODE_ENABLE);  // 使能主模式
//			timer_slave_mode_select(TIMER2,TIMER_SLAVE_MODE_EXTERNAL0);//从模式选择   外部时钟源模式
//    timer_primary_output_config(TIMER2,ENABLE);//开启主模式输出
		
    timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH0);  // 清除通道0中断标志位
    timer_interrupt_enable(TIMER2, TIMER_INT_CH0);  // 使能通道0中断
		 nvic_irq_enable(TIMER2_IRQn, 0);  // 使能中断服务,抢占优先级为0
    timer_enable(TIMER2);  // 使能定时器2
 
    timer_deinit(TIMER0);//初始化定时器0
 
   
    timer_initpara.prescaler         = 0;
    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(TIMER0, &timer_initpara);
 
     
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    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);

 
    timer_master_slave_mode_config(TIMER0, TIMER_MASTER_SLAVE_MODE_ENABLE);//使能主从模式
		
		
//		timer_slave_mode_select(TIMER0,TIMER_SLAVE_MODE_RESTART);//从模式选择  重启模式
	
		
		timer_input_trigger_source_select(TIMER0,TIMER_SMCFG_TRGSEL_ITI0);//内部触发源配置为外部触发
		
		
		
		
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0,PWM_velu);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    
 
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, PWM_velu);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    
 
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2,PWM_velu);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM0);
  
	  
    timer_primary_output_config(TIMER0, ENABLE);
   
    timer_enable(TIMER0);
		
		 
	 }

定时器肯定是要改的,霍尔传感器的捕获输入改为双边沿触发,这样在中断里检测的霍尔值才完整,之前的定时器会发生缺相现象。

然后就是换相函数,换相函数的时序一定是要符合你的电机,这个电机厂家都会给,别自己瞎研究,下面是我的换相函数。

void BLDC_Convet_anticlockwise(uint16_t  phase)
{       
	InitSysTick();
 switch(phase)   //
    {
        case 0x00: //AB ---- Q1 Q5  
        {
            
            DelayUs(500);
            timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
 
            timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM0);
 

            timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM0);
                
            timer_event_software_generate(TIMER0,TIMER_SWEVG_CMTG);
 
        }
//        break;
        case 1: //AB ---- Q1 Q5  
        {
            
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_DISABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
 
           
            timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_HIGH);
 
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_LOW);
                
            timer_event_software_generate(TIMER0,TIMER_SWEVG_CMTG);
        }
        break;
        case 3://0x01: //AC ---- Q1 Q6 
        {
          
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_DISABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
 
          
            timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_LOW);
 
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_HIGH);
                
            timer_event_software_generate(TIMER0,TIMER_SWEVG_CMTG);  
        }                
        break;
				 case 2: //CB ---- Q3 Q5 
        {
					
					timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_HIGH);
 
            
 
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_DISABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM0);
            
           
            timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_LOW);
                
            timer_event_software_generate(TIMER0,TIMER_SWEVG_CMTG);  
                 
        }
        break;
				 case 5: //BA ---- Q2 Q4 
        { 
            
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_DISABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM0);
 
            
 
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_LOW);
            
           
            timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_HIGH);
                
            timer_event_software_generate(TIMER0,TIMER_SWEVG_CMTG);  
        }
        break;
        case 4: //CA ---- Q3 Q4 
        {
					
					timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_DISABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_LOW);
 
            
 
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_DISABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM0);
            
           
            timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_HIGH);
                
            timer_event_software_generate(TIMER0,TIMER_SWEVG_CMTG);  
        
        }
        break;  
        case 6: //BC ---- Q2 Q6 
        {  
 
            
            timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_ENABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_DISABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM0);
 
           
            timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_LOW);
 
          
            timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_DISABLE);//CC1E
            timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);//CC1NE
            timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_HIGH);
                
            timer_event_software_generate(TIMER0,TIMER_SWEVG_CMTG);  
        }            
        break;
                                 
       
 
        default:break;
    }   
}

到此电机项目全部完成,想要更好的控制电机,接下来要用算法来控制了,最常用的就是PID,三个环电流环,速度环,位置环可以精准控制电机,这个我今天开始研究,研究出来会将代码分享给大家。

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值