Principle introduction and code realization of pwm motor speed regulation

1. The principle and introduction of pwm to realize speed regulation

Insert picture description here

PWM (Pulse Width Modulation) pulse width modulation.

1) Duty cycle

The pwm duty cycle is the ratio of the effective level in the entire period of a pulse period.

By adjusting the duty cycle of the PWM, the continuous change of the voltage on the IO port can be adjusted, so the power of the peripheral can be controlled to continuously change, and the speed of the DC motor can also be controlled.
Then the key is how to adjust the output of the PWM waveform. As shown in the
Insert picture description here
figure below, ARR in the figure is a preload value we give to the timer, and the up and down change of CCRx is the key to generating the PWM wave. We assume that the part of the output with ARR greater than CCRx is high (ie t1-t2, t3-t4, t5-t6), and the part of the output with ARR less than CCRx is low (ie 0-t1, t2-t3, t4-t5) ), then changing the value of CCRx can change the duty cycle of the output PWM.
As long as you understand the above picture, it is not difficult to understand how to control the output waveform of PWM. The important thing is how to set the values ​​of the two registers, ARR and CCRx. (How to set the value of the register here will be very clear in my code, you can look at the principle first)

  • PWM mode, effective level

Earlier we assumed that the output is high when ARR is greater than CCRx, and the output is low when ARR is less than CCRx, but this may not be the case in actual applications, and it may be the opposite situation-when ARR is greater than CCRx, the output is low. When ARR is less than CCRx, the output is high. As for the situation, it depends on the PWM mode and the polarity of the effective level.

Mode 1: When ARR is less than CCRx, the output is "active" level, and when ARR is greater than CCRx, the output is "invalid" level.
Mode 2: When ARR is less than CCRx, the output is "invalid" level, and when ARR is greater than CCRx, the output is "valid" level.

Note that I used "valid" and "invalid" here instead of "high" and "low", which means that the effective level can be high or low, not necessarily high. The PWM mode and the polarity of the effect level need to be implemented by the programmer by configuring the relevant registers.

2) Configuration mode

● Up-count configuration
When the DIR bit in the TIM1_CR1 register is low, the up-count is executed.
The following is an example of PWM mode 1. When TIM1_CNT<TIM1_CCRi, the PWM reference signal OCiREF is high, otherwise it is low. If the comparison value in TIM1_CCRi is greater than the auto-reload value (TIM1_ARR), then OCiREF remains at '1'. If the comparison value is 0, OCiREF remains at '0'. The following figure shows an example of the edge-aligned PWM waveform when TIM1_ARR=8.
Insert picture description here
● Down-counting configuration
When the DIR bit of the TIM1_CR1 register is high, the down-counting is executed.
In PWM mode 1, when TIM1_CNT>TIM1_CCRi, the reference signal OCiREF is low, otherwise it is high. If the comparison value in TIM1_CCRi is greater than the auto-reload value in TIM1_ARR, OCiREF remains at '1'. 0% PWM waveform cannot be generated in this mode.

Those who need the stm8 register reference manual can go to csdn to download or comment

2. The pwm signal configuration process

(Here takes stm8 to configure TIM1 as an example)

1) Set the automatic reload value

Both TIM1_ARR register ARR[15:0]
Insert picture description here
Insert picture description here

2) Set the capture comparison value

Both TIM1_CCRx register CCRi[15:0]
Insert picture description here
Insert picture description here

3) Set pwm output mode and alignment

Both TIM1_CCMRx register OCIM[1:0] and TIM1_CR1 register CMS[1:0] bits
Insert picture description here
Insert picture description here

4) Configure the signal output polarity and enable the output

Both TIM1_CCERx registers CCiP and CCiE
Insert picture description here

5) Enable the counter to turn on the main switch

Both the CEN bit of the TIM1_CR1 register and the MOE bit of the TIM1_BKR register (the TIM3 does not have the MOE bit)
Insert picture description here
Insert picture description here
need the stm8 register reference manual can go to csdn to download or comment

3. PWM speed control code

(Here is an example of configuring TIM3 bit with stm8)

/****************************************************************/
//TIM3输出比较功能初始化函数TIM3_init(),有形参F_PWM_SET,无返回值
//Duty_CH1为通道一PD2的占空比
//Duty_CH2为通道二PD0的占空比
//F_PWM_SET为自动重装载寄存器数值
/****************************************************************/
void Motor_SpeedSet(unsigned long F_PWM_SET,float Duty_CH1,float Duty_CH2)
{
    
    
 
  //配置自动重装载寄存器
  TIM3_ARRH=F_PWM_SET/256;//配置自动重装载寄存器高位“ARRH”
  TIM3_ARRL=F_PWM_SET%256;//配置自动重装载寄存器低位“ARRL”
 
  TIM3_CH1_PWM_SET(F_PWM_SET,Duty_CH1);
  TIM3_CH2_PWM_SET(F_PWM_SET,Duty_CH2);
  
  TIM3_CR1|=0x01;//使能TIM1计数器功能“CEN=1”

}


//Duty_CH1为占空比
//F_SET_CH1为输出比较的数值
//TIM3_CH1为PD2
void TIM3_CH1_PWM_SET(unsigned long F_PWM_SET,float Duty_CH1)
{
    
    
 // F_SET_CH1 = 16000;        //TIM1输出比较功能初始化配置
 // printf("start\n");
  
  float CompareNum;                  //变量为比较值,用于配置比较寄存器
  CompareNum=Duty_CH1*F_PWM_SET;     //利用占空比和预装载数值来反求比较寄存器所需要设定的值
  
  /*
  //配置自动重装载寄存器
  TIM3_ARRH=F_PWM_SET/256;
  TIM3_ARRL=F_PWM_SET%256;         
  */
  
  //配置比较计数器
  TIM3_CCR1H=((u16)(CompareNum))/256;//配置捕获/比较寄存器1高位“CCR1H”
  TIM3_CCR1L=((u16)(CompareNum))%256;//配置捕获/比较寄存器1低位“CCR1L”
  
  //printf("计数器配置完成...\n");
  
  //配置向上计数模式边沿对齐,不用设置,默认向上计数
  //TIM1_CR1&=0x8F;
  
  //配置为PWM模式1,OC1M[2:0]=110,需要注意这里只是设置了有效电平,但是并没有说有效电平是高电平还是低电平
  //PWM模 - 在 计数 式1 向上无效电平;在向下计数时,一旦TIMx_CNT>则为有效电平(OC1REF=1)。
  //CC1通道被配置为输出,CC1S[1:0]=00
  TIM3_CCMR1=0x60;
  
  //配置CC1P=0,OC1信号有效电平为高电平
  //也就是比TIM3_CCR小时输出高电平,比TIM3_CCR大时输出低电平
  TIM3_CCER1&=0xFD;         
  
  //配置CC1E=1,OC1信号输出到对应的输出引脚
  TIM3_CCER1|=0x01;         
  
  //空闲状态时OC1为高电平,不需要设置
  //TIM1_OISR|=0x01;          
    
  //TIM3_CR1|=0x01;//使能TIM1计数器功能“CEN=1”
}



//Duty_CH1为占空比
//F_SET_CH1为输出比较的数值
//TIM3_CH1为PD0
void TIM3_CH2_PWM_SET(unsigned long F_PWM_SET,float Duty_CH2)
{
    
    
 // F_SET_CH1 = 16000;        //TIM1输出比较功能初始化配置
 // printf("start\n");
  
  float CompareNum;                  //变量为比较值,用于配置比较寄存器
  CompareNum=Duty_CH2*F_PWM_SET;     //利用占空比和预装载数值来反求比较寄存器所需要设定的值
  
  /*
  //配置自动重装载寄存器
  TIM3_ARRH=F_PWM_SET/256;
  TIM3_ARRL=F_PWM_SET%256;         
  */
  
  //配置比较计数器
  TIM3_CCR2H=((u16)(CompareNum))/256;//配置捕获/比较寄存器1高位“CCR1H”
  TIM3_CCR2L=((u16)(CompareNum))%256;//配置捕获/比较寄存器1低位“CCR1L”
 
  
  //配置为PWM模式1,OC1M[2:0]=110,需要注意这里只是设置了有效电平,但是并没有说有效电平是高电平还是低电平
  //PWM模 - 在 计数 式1 向上无效电平;在向下计数时,一旦TIMx_CNT>则为有效电平(OC1REF=1)。
  //CC1通道被配置为输出,CC1S[1:0]=00
  TIM3_CCMR2=0x60;
  
  //配置CC2P=0,OC2信号有效电平为高电平
  //也就是比TIM3_CCR小时输出高电平,比TIM3_CCR大时输出低电平
  TIM3_CCER1&=0xDF;         
  
  //配置CC2E=1,OC2信号输出到对应的输出引脚
  TIM3_CCER1|=0x10;         
     
  //TIM3_CR1|=0x01;//使能TIM1计数器功能“CEN=1”
}


//将pwm信号接到l298n的使能端就可以了
 printf("现在开始测试PWM信号产生...\n");
 case '1':
      Motor_SpeedSet(20000,0.2,0.2);      //一档速
      break;
 case '2':
      Motor_SpeedSet(20000,0.4,0.4);      //二档速
      break;
  
      Motor_SpeedSet(60000,0.8,0.8);	//可以配置不同当的速度,只要你想,100档速的小车都可以
   
      printf("信号已产生...\n");

4. Why pwm can adjust the speed

Here is an article—"STM32 PWM output principle and DC motor PWM drive principle detailed explanation and routines", which can be viewed in the reference link at the bottom. This blogger wrote very well. Here I will extract some key principles.

Insert picture description here
It means that when the light is off, you still feel that it is on. When the light keeps on and then goes off, as long as the interval is extremely short, we look like it is always on. The same.

Then think about it at this time. There are two lights with the same power supply voltage. In a fixed period of time, the A light flashes constantly. The frequency is so fast that the human eye can’t distinguish it. 50% of the time is on. , 50% is off, and the B light is always on. In this way, you will obviously feel that the A lamp is much dimmer than the B lamp. So this is a way to control the brightness of the small lights.

Of course, you can also understand that under the same power, the two lamps AB are doing the work of converting electrical energy into light energy. In a day, the A lamp works intermittently for 12 hours, and the B lamp works continuously for 24 hours. . Then it is obvious who converts more light energy, and who is brighter is self-evident. At
Insert picture description here
this time, as long as you can control the light on and off in a very short time, you can control the brightness.

In the above figure, the voltage of the power supply terminals of the two lamps AB can be seen as the figure below. No
Insert picture description here
! The coil of the motor, you know, it has a structure similar to an inductance. It has the non-mutability of the current. This property is like the high school teacher said, the inductance coil is DC and AC resistance, and the magnetic field generated inside it is not allowed. The sudden change of the current, he will use the magnetic field generated by the current to suppress the change of the current.
So if you use PWM to control the motor, it can be simplified and understood as follows: The PWM generated by the chip is used to make the power image at both ends of the motor look like a PWM signal without talking about the principle of the drive circuit board. As for how he became we will break down next time.
This means that if the motor is controlled by PWM, the voltage across the motor is abruptly like 010101, but the motor current is not. Even if the voltage is 0, there is still current flowing in the motor due to the self-induced electromotive force of the coil, and the motor continues. Rotation, then why can we use PWM to control the speed?
You might think of a word spoken by a high school physics teacher: equivalent voltage.
We use PWM to control the equivalent voltage. Then you can understand.
You can also think in terms of energy conservation. After the motor is turned, the coil resistance remains constant, the power remains constant, and the peak voltage at both ends is also constant. In this case, the difference in duty cycle on the power supply voltage graph will change. During this period of time, the electric motor consumes energy differently, and the mechanical energy to be converted is different, and the speed is also different. Isn’t that easy to understand?

Probably take the interception here. If you want to see the full introduction, you can click on the reference link at the bottom of the article.

Reference link:
STM32 PWM output principle and DC motor PWM drive principle detailed explanation and routines

Guess you like

Origin blog.csdn.net/weixin_44751294/article/details/111567625