光耦电机测速

很久没更新了,现在开始放些32程序。生活学习日记也没有更过,到放假电赛期间更忙了,最近在练习题目。主攻仪器仪表的。

我这个电机模块是不带正反转控制端的,所以说是用双pwm(和之前51一样)测速用的上升沿捕获功能。我也不知道准不准 - =

电机驱动程序

#include "motor.h"

void PWM_Set(u32 arr,u32 psc)    
{
  GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef	TIM_TimeBaseStructure;//时基单元结构体
  TIM_OCInitTypeDef	TIM_OCInitStructure;            //输出比较结构体
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE ); 
  
  GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);     //TIM3重定义
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_1;	  //PB5 TIM3_CH2定时器3的通道2
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init( GPIOB, &GPIO_InitStructure );

	RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );//开启TIM3时钟
    TIM_InternalClockConfig( TIM3 );              //SMS =000采用内部时钟给TIM2提供时钟源( TIMxCLK -> CK_INT -> CK_PSC -> CK_CNT 
    TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载寄存器的值
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //TIMX预分频的值
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据以上功能对定时器进行初始化
//----------------------------输出比较产生PWM-----------------------------------------
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;	
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	//比较输出使能
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;	//输出极性为低电平
	TIM_OC2Init( TIM3, &TIM_OCInitStructure );      //对通道2(TIM3_CH2)进行初始化
  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);   //使能定时器TIM3在CCR2上的预装载值 
  
  TIM_OC4Init( TIM3, &TIM_OCInitStructure );      
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); 
  
  TIM_CtrlPWMOutputs(TIM3,ENABLE);   
  TIM_ARRPreloadConfig(TIM3,ENABLE);
	TIM_Cmd( TIM3, DISABLE );//波形暂时不输出,根据题目再开使能
}

void cesu()
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_TIM1|RCC_APB2Periph_AFIO, ENABLE); 
  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_12; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_SetBits(GPIOA,GPIO_Pin_12);	
  
  TIM_DeInit(TIM1);
  TIM_TimeBaseStructure.TIM_Period =5; //重载5
  TIM_TimeBaseStructure.TIM_Prescaler =1; 
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 
  TIM_ITRxExternalClockConfig(TIM1,TIM_TS_ETRF);
  TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,2);
  TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); 
  TIM_SetCounter(TIM1, 0); 
  TIM_Cmd(TIM1,ENABLE );
}

void dianji(u8 n,u32 s)   //n:0 正转 1反转 
{                                        //s:占空比速度                                           
    switch(n)
    {
      case 0: TIM_Cmd( TIM3,ENABLE);  
              TIM_SetCompare2(TIM3,s);
              TIM_SetCompare4(TIM3,9999);
      break;
      
      case 1: TIM_Cmd( TIM3,ENABLE);  
              TIM_SetCompare2(TIM3,9999);
              TIM_SetCompare4(TIM3,s);
      break;
      default:break;
    }
}

/*
TIM3_CH2(9999,72-1);   //频率: 72*10^6/(9999+1)/ (143+1)=50Hz    71+1=100hz
TIM_SetCompare2(TIM3,4999);  //占空比50%  6999占空比70%   占空比越小电机速度越快  
这个芯片采用双路pwm控制一个电机,IN1给高,IN2给pwm 点击正转  IN2给搞,IN1给PWM,电机反转
配合结构体成员TIM_OCInitTypeDef.TIM_OCPolarity来一起使用。

     现在假定TIM_OCInitTypeDef.TIM_OCPolarity = TIM_OCPolarity_High,则起始波形为高电位。

若TIM_OCInitTypeDef.TIM_OCMode = TIM_OCMode_PWM1时:

     当计时器值小于比较器设定值时则TIMX输出脚此时输出有效高电位。

     当计时器值大于或等于比较器设定值时则TIMX输出脚此时输出低电位。

若TIM_OCInitTypeDef.TIM_OCMode = TIM_OCMode_PWM2时:

     当计时器值小于比较器设定值时则TIMX输出脚此时输出有效低电位。

     当计时器值大于或等于比较器设定值时则TIMX输出脚此时输出高电位。
*/

  • 主程序
  • void Tim2_init()
    {
      TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    	TIM_DeInit( TIM2 );                  //将TIM寄存器重置为缺省值
    	TIM_InternalClockConfig( TIM2 );    //SMS =000采用内部时钟给TIM2提供时钟源( TIMxCLK( 72MHz ) -> CK_INT -> CK_PSC -> CK_CNT )
      TIM_TimeBaseStructure.TIM_Period=2000;      //2000*1/1000=1S
      TIM_TimeBaseStructure.TIM_Prescaler=35999;   //35999+1/72M=1/2000;
      TIM_TimeBaseStructure.TIM_ClockDivision=0;
      TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
      TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); 
      
      TIM_ClearFlag( TIM2, TIM_FLAG_Update ); //清除溢出中断标志位
    	TIM_ARRPreloadConfig( TIM2, DISABLE ); //禁止ARR预装载缓冲器
    	TIM_ITConfig( TIM2,TIM_IT_Update,ENABLE );// 打开更新事件中断
      TIM_Cmd(TIM2,ENABLE);                     //使能TIM2
    }
    void NVIC_Cfg()
    {
      NVIC_InitTypeDef NVIC_InitStructure; 	
      NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2 ); 
        
      NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn; 
    	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; 
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
    	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    	NVIC_Init( &NVIC_InitStructure );
      
      NVIC_InitStructure.NVIC_IRQChannel =TIM1_UP_IRQn; 
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
      NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器
      //PWM
       NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
    	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 
    	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    	NVIC_Init( &NVIC_InitStructure );
    
    }
    
    int main(void)
    {
      delay_init();
      LCD_Init();  
      cesu();  
      Tim2_init();
      NVIC_Cfg();
      PWM_Set(9999,72-1);   //频率为:72*10^6/(9999+1)/(143+1)=50Hz    71+1=100hz
      dianji(0,5999);
      POINT_COLOR=DARKBLUE;
      while(1)
      {
         LCD_ShowNum(10,10,cnt,4,16);//圈数
      }
    }

     

 

你可能感兴趣的:(stm32)