stm32实现直流电机调速

直流电动机调速是指电动机在一定负载的条件下,根据需要,人为地改变电动机的转速。直流电动机调速调速性能好。所谓“调速性能”,是指电动机在一定负载的条件下,根据需要,人为地改变电动机的转速。直流电动机可以在重负载条件下,实现均匀、平滑的无级调速,而且调速范围较宽。

对于普通的直流电机,只要在电机的两根线上接上电源电机就转动,导线反接后,电机就发转。如果,电机两端的电压为额定电压,则电机满速运转,如果电压为额定电压的一半,则电机以一般的速度运转。所以电机调速的手段就是更变电机两端的电压,而通常的做法就是通过PWM来实现电机调速。 1 PWM电机调速的原理 所谓PWM就是脉冲宽度调制,对于方波而言,一个完整的周期是由高电平脉冲和低电平脉冲所构成的,高电平所占周期的比例就是占空比。占空比越大的话,那么波形的平均电压越大;占空比越小,波形的平均电压也就越小。所以,如果把PWM信号接在电机的控制端,则通过改变占空比,则实现了电机两端电压的调节,由此实现了电机转速的调节。两个极端的例子:如果占空比为100%,则电压最大;如果占空比为0,则电压为0。

电机调速简单电路的实现,只控制电机一个方向的调速时,可以通过如下的电路模型来实现。在选型时需要根据电流大小、电压大小来合理选择三极管的型号。 在三极管的控制端,调节PWM的占空比可以实现调节电机两端电压的大小,从而实现了调速功能。

为了控制电机的正转和反转,可以设计一个H桥驱动电路来实现电机正转和反转,并实现调速。项目使用stm32输出PWM波控制L298N实现电机驱动具体电路如下

stm32实现直流电机调速_第1张图片

关键代码如下

void TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef	TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
	
	NVIC_InitTypeDef NVIC_InitStructure;
	
	// ¿ªÆô¶¨Ê±Æ÷ʱÖÓ,¼´ÄÚ²¿Ê±ÖÓCK_INT=72M
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_TIM1, ENABLE);
	
   // Êä³ö±È½ÏͨµÀ1 GPIO ³õʼ»¯
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8 | GPIO_Pin_9  ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
		
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_13 | GPIO_Pin_14 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_SetBits(GPIOB, GPIO_Pin_13);
	GPIO_ResetBits(GPIOB, GPIO_Pin_14);
	
	TIM_DeInit(TIM1);        //½«ÍâÉèTIM1¼Ä´æÆ÷ÖØÉèΪȱʡֵ  
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1 ;    //ÉèÖÃÁËʱÖÓ·Ö¸î(Tck_tim) 
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up ;   //Ñ¡ÔñÁ˼ÆÊýÆ÷ģʽ(TIMÏòÉϼÆÊýģʽ)  
	TIM_TimeBaseInitStruct.TIM_Period = 6005 ;       //É趨¼ÆÊýÆ÷×Ô¶¯ÖØ×°Öµ,È¡Öµ·¶Î§0x0000~0xFFFF   
	TIM_TimeBaseInitStruct.TIM_Prescaler = 71 ;    //ÉèÖÃÓÃÀ´×÷ΪTIM3ʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵΪ(79+1),È¡Öµ·¶Î§0x0000~0xFFFF 
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct ) ;       
	
	/* ¶¨Ê±Æ÷Êä³öͨµÀ1ģʽÅäÖà */
 
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;/* ģʽÅäÖãºPWMģʽ1 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	 /* Êä³ö״̬ÉèÖãºÊ¹ÄÜÊä³ö */
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; /* »¥²¹Í¨µÀÊä³ö״̬ÉèÖãºÊ¹ÄÜÊä³ö */
  TIM_OCInitStructure.TIM_Pulse = 3000;/* ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä */
  
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;/* µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪ¸ßµçƽ */
  TIM_OCInitStructure.TIM_OCNPolarity= TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCNIdleState_Reset;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;/* ģʽÅäÖãºPWMģʽ1 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	 /* Êä³ö״̬ÉèÖãºÊ¹ÄÜÊä³ö */
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; /* »¥²¹Í¨µÀÊä³ö״̬ÉèÖãºÊ¹ÄÜÊä³ö */
  TIM_OCInitStructure.TIM_Pulse = 3000;/* ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä */
	
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;/* µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪ¸ßµçƽ */
  TIM_OCInitStructure.TIM_OCNPolarity= TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCNIdleState_Reset;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
	
	TIM_OC2Init(TIM1, &TIM_OCInitStructure);
 
 
  /* Automatic Output enable, Break, dead time and lock configuration*/
  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  TIM_BDTRInitStructure.TIM_DeadTime = 5;
  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
  
  TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
  TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
 
	
  TIM_ARRPreloadConfig(TIM1, ENABLE);/* ʹÄܶ¨Ê±Æ÷ÖØÔؼĴæÆ÷ARR */
  TIM_Cmd(TIM1, ENABLE);/* ʹÄܶ¨Ê±Æ÷ */
  TIM_CtrlPWMOutputs(TIM1, ENABLE); /* TIMÖ÷Êä³öʹÄÜ */
  
  TIM_CCxCmd(TIM1,TIM_Channel_1,TIM_CCx_Enable);
  TIM_CCxNCmd(TIM1,TIM_Channel_1,TIM_CCxN_Enable);
  TIM_CCxCmd(TIM1,TIM_Channel_2,TIM_CCx_Enable);
  TIM_CCxNCmd(TIM1,TIM_Channel_2,TIM_CCxN_Enable);
 
}

 

你可能感兴趣的:(stm32实现直流电机调速)