32 single chip microcomputer controls the rotation of YINGDIAN T80 motor

32 single chip microcomputer controls T80 brushless motor through good YINGDIAN regulation

Control principle of electric regulator

Why do we need electrical regulation

The speed, positive and negative indicators of brushless motor are realized by changing the duty cycle and frequency of control signal (PWM). We can easily control PWM with single chip microcomputer. However, it is unrealistic to drive a 500W motor with the driving ability of small IO port of single chip microcomputer, so we want to realize the effect of small current controlling large current, Just as the DC brushless motor is driven by L298N motor driver, we control the brushless motor by brushless electric regulator.

Requirements for input of electric regulation control signal

I use it for the first time (i.e. uncertain). It seems that most electric regulators need a PWM pulse frequency of 50Hz. Among them, the high-level pulse width needs to be 1-2ms, and 50Hz is a square wave with a period of 20ms. Among them, the high-level time is 1-2ms, and the duty cycle is 5% - 10%.

How to unlock the electric regulator

After adjusting for a long time today, the motor doesn't move, the wiring is correct, and the pwm signal is reasonable. Later, I checked several articles and found the following operation process.

  1. The single chip microcomputer gives 2ms high level (maximum throttle), and the power can not be adjusted at this time
  2. Power on
  3. MCU gives 1ms high level (minimum throttle) (unlocking is completed)
  4. For the high level in any 1-2ms range, turn

Each time, the electric regulator only needs to carry out such four-step operation from power failure to operation, and then the duty cycle can be adjusted within the range at will.

Wiring of electric regulator

The electric regulator I used is haoying 40A patch electric regulator.
The wiring diagram is from the instruction manual on the official website:
The three-phase electrical port of the electric regulator is connected to the three terminals of the brushless motor
The two signal ports of the electric regulator are connected with the PWM signal of the single chip microcomputer
Electrically adjustable red and black copper wires are connected to the positive and negative of the battery
This is finished in hardware.

MCU code part

I use the punctual atomic mini board, and the main control chip is STM32RCT6.

PWM configuration

The following is the configuration in the pwm.c file. Here is the punctual atomic routine "PWM output experiment".
In a word, the PWM mode is changed from PWM2 to PWM1 for the convenience of subsequent parameter setting.
PWM is output from PA8 pin, corresponding to channel 1 of TIM1.

In the corresponding main function, set the automatic reload value and pre frequency division coefficient.
This is TIM1_ PWM_ Definition of init():

//PWM output initialization
//arr: Auto reload value
//psc: clock pre division frequency
void TIM1_PWM_Init(u16 arr,u16 psc)

This is the configuration in the main function:

	TIM1_PWM_Init(19999,71);//No frequency division. PWM frequency = 72000000/(19999+1)(71+1)=50Hz  

Key configuration

Key matching pwm output codes are as follows:

while(1)
	{
		t=KEY_Scan(0);		//Get key value
		switch(t)
		{				 
			case KEY0_PRES:	TIM_SetCompare1(TIM1,2000);
				break;
			
			case KEY1_PRES:	TIM_SetCompare1(TIM1,1000);
				break;
			
			case WKUP_PRES:	TIM_SetCompare1(TIM1,1500);	 		
				break;
			
			default:
				delay_ms(10);	
		} 
	}		 

2000 corresponds to the maximum throttle and 1000 corresponds to the minimum throttle. In combination with the previously mentioned how to unlock the motor, I know how to operate it.

  1. Press the KEY0 key on the development board to give the maximum throttle
  2. Power on
  3. Press the KEY1 key on the development board to give the minimum throttle (unlocking is completed)
  4. Just give any duty cycle

Complete code

Main function

#include "led.h"
#include "delay.h"
#include "sys.h"
#include "pwm.h"
#include "key.h"
#include "usart.h"
//ALIENTEK Mini STM32 development board example code 8
//PWM output experiment   
//Technical support: www.openedv.com
//Guangzhou Xingyi Electronic Technology Co., Ltd
int m = 0;

 int main(void)
 {	
	u8 t=0;	 
	delay_init();	    	 //Delay function initialization	      
  KEY_Init();    //Key initialization

	TIM1_PWM_Init(19999,71);//No frequency division. PWM frequency = 72000000/(19999+1)(71+1)=50Hz  
 
 while(1)
	{
		t=KEY_Scan(0);		//Get key value
		switch(t)
		{				 
			case KEY0_PRES:	TIM_SetCompare1(TIM1,2000);
				break;
			
			case KEY1_PRES:	TIM_SetCompare1(TIM1,1000);
				break;
			
			case WKUP_PRES:	TIM_SetCompare1(TIM1,1500);	 		
				break;
			
			default:
				delay_ms(10);	
		} 
	}		 
}

pwm.c

#include "pwm.h"
#include "led.h"
//	 
//This program is only for learning and use, and shall not be used for any other purpose without the permission of the author
//ALIENTEK Mini STM32 development board
//PWM drive code			   
//Punctual atom @ ALIENTEK
//Technical Forum: www.openedv.com
//Modified on: December 3, 2010
//Version: V1.0
//Copyright, piracy must be investigated.
//Copyright(C) punctual atom 2009-2019
//All rights reserved
// 	  


//PWM output initialization
//arr: Auto reload value
//psc: clock pre division frequency
void TIM1_PWM_Init(u16 arr,u16 psc)
{  
	 GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);// 
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);  //Enable GPIO peripheral clock enable
	                                                                     	

   //Set this pin as the multiplexing output function to output the PWM pulse waveform of TIM1 CH1
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //Multiplexed push-pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	
	TIM_TimeBaseStructure.TIM_Period = arr; //Sets the value of the auto reload register cycle of the load activity at the next update event 	  80K
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //Set the prescaled value used as the divisor of TIMx clock frequency without frequency division
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Set clock division: TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM up count mode
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //According to Tim_ The parameter specified in timebaseinitstruct initializes the time base unit of TIMx

 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Select timer mode: TIM pulse width modulation mode 1
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Compare output enable
	TIM_OCInitStructure.TIM_Pulse = 0; //Set the pulse value to be loaded into the capture comparison register
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Output polarity: TIM output has high polarity
	TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //According to Tim_ Initialize peripheral TIMx with the parameters specified in ocinitstruct

  TIM_CtrlPWMOutputs(TIM1,ENABLE);	//MOE main output enable	

	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH1 preload enable	 
	
	TIM_ARRPreloadConfig(TIM1, ENABLE); //Enable TIMx preload register on ARR
	
	TIM_Cmd(TIM1, ENABLE);  //Enable TIM1
 
   
}

key.c

#include "key.h"
#include "delay.h"
//	 
//This program is only for learning and use, and shall not be used for any other purpose without the permission of the author
//ALIENTEK Mini STM32 development board
//Press the key to enter the drive code		   
//Punctual atom @ ALIENTEK
//Technical Forum: www.openedv.com
//Modified on: March 6, 2014
//Version: V1.0
//Copyright, piracy must be investigated.
//Copyright(C) Guangzhou Xingyi Electronic Technology Co., Ltd. 2009-2019
//All rights reserved									   
//	 
 	    
//Key initialization function 
//PA15 and PC5 are set as inputs
void KEY_Init(void)
{
	
	GPIO_InitTypeDef GPIO_InitStructure;

 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);//Enable PORTA,PORTC clock

	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//Close jtag, enable SWD, and debug in SWD mode
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_15;//PA15
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Set as pull-up input
 	GPIO_Init(GPIOA, &GPIO_InitStructure);//Initialize GPIOA15
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_5;//PC5
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Set as pull-up input
 	GPIO_Init(GPIOC, &GPIO_InitStructure);//Initialize GPIOC5
 
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;//PA0
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 is set as input, and the default is drop-down	  
	GPIO_Init(GPIOA, &GPIO_InitStructure);//Initialize GPIOA.0
	
} 
//Key handling function
//Return key value
//mode:0, continuous pressing is not supported; 1. Support continuous pressing;
//Return value:
//0, no key pressed
//KEY0_PRES, key0 press
//KEY1_PRES, key1 press
//WKUP_PRES,WK_UP press 
//Note that this function has response priority, key0 > key1 > wk_ UP!!
u8 KEY_Scan(u8 mode)
{	 
	static u8 key_up=1;//Press the key to release the sign
	if(mode)key_up=1;  //Support continuous pressing		  
	if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))
	{
		delay_ms(10);//debounce  
		key_up=0;
		if(KEY0==0)return KEY0_PRES;
		else if(KEY1==0)return KEY1_PRES;
		else if(WK_UP==1)return WKUP_PRES; 
	}else if(KEY0==1&&KEY1==1&&WK_UP==0)key_up=1; 	     
	return 0;// No key press
}

Not fully understood

On the positive and negative of motor

I set the parameters of electric adjustment to 1000-2000 stroke, with a median of 1500. Then my motor does not rotate corresponding to 1500. 2000 is the maximum speed of forward rotation and 1000 is the maximum speed of reverse rotation. My guess is that 1000-2000 is the total interval that can be allocated to forward and reverse rotation. If the median is set to 1500, this interval is evenly allocated and I do not want to reverse, If you want the extreme forward rotation, set the median to 1000, so that the whole interval is forward rotation. Of course, at the same time, the function of inversion is lost.

I'm not sure about the above. I'll try to change the electric regulation parameters tomorrow.

About power on sequence

I found that I first power on the power regulator, then give the maximum throttle, hear Di Di Di Di Di, and then give the minimum throttle, di. After that, I can also unlock the power regulator.

About program structure

I now use the key to control the input of the maximum throttle and the minimum throttle. When the program automatically unlocks the electric adjustment, it has not been written yet. It is expected to be written with the delay function, but I have to try how long the specific demonstration will take. I have to ensure that after giving the maximum throttle, I hear Di Di Di Di Di, and then give the minimum throttle. After the maximum throttle is recognized, If it's slow, it's OK. If it's early, it won't work.

Other blogger related articles

I mainly read these two articles and wrote this summary after my practice. I hope it will be helpful to you.
Link: 32 single chip microcomputer control electric regulation.
Link: Using STM32F103 single chip microcomputer to control electric adjustable braking brushless motor.

Keywords: C Single-Chip Microcomputer stm32

Added by alivec on Mon, 06 Dec 2021 21:26:31 +0200