コード例 #1
0
ファイル: tim.c プロジェクト: StefanJia/test2
/* TIM4 init function */
void MX_TIM4_Init(void)
{
  TIM_Encoder_InitTypeDef sConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 0;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = 10000;
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC1Filter = 0;
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC2Filter = 0;
  if (HAL_TIM_Encoder_Init(&htim4, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

}
コード例 #2
0
ファイル: main.c プロジェクト: sincoon/injex
/* TIM1 init function */
void MX_TIM1_Init(void)
{

  TIM_Encoder_InitTypeDef sConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 128;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
  htim1.Init.RepetitionCounter = 0;
  sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
  sConfig.IC1Polarity = TIM_ICPOLARITY_FALLING;
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC1Filter = 5;
  sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC2Filter = 5;
  HAL_TIM_Encoder_Init(&htim1, &sConfig);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

}
コード例 #3
0
ファイル: motor.c プロジェクト: kefalakis/STM32
void EncoderInit(TIM_HandleTypeDef *encoderL,TIM_HandleTypeDef *encoderR){

	//Left motor encoder initialization
	encoderL->Instance 					= TIM3;
	encoderL->Init.Period            	= 0xFFFF;
	encoderL->Init.Prescaler         	= 0;
	encoderL->Init.ClockDivision     	= 0;
	encoderL->Init.CounterMode       	= TIM_COUNTERMODE_UP;
	encoderL->Init.RepetitionCounter 	= 0;

	TIM_Encoder_InitTypeDef sEncoderConfig;

	sEncoderConfig.EncoderMode        = TIM_ENCODERMODE_TI12;
	sEncoderConfig.IC1Polarity        = TIM_ICPOLARITY_RISING;
	sEncoderConfig.IC1Selection       = TIM_ICSELECTION_DIRECTTI;
	sEncoderConfig.IC1Prescaler       = TIM_ICPSC_DIV1;
	sEncoderConfig.IC1Filter          = 3;							///?????

	sEncoderConfig.IC2Polarity        = TIM_ICPOLARITY_RISING;
	sEncoderConfig.IC2Selection       = TIM_ICSELECTION_DIRECTTI;
	sEncoderConfig.IC2Prescaler       = TIM_ICPSC_DIV1;
	sEncoderConfig.IC2Filter          = 3;
	HAL_TIM_Encoder_Init(encoderL, &sEncoderConfig);
	HAL_TIM_Encoder_Start(encoderL, TIM_CHANNEL_ALL);		//TI1&TI2


	//Right motor encoder initialization
	//*
	encoderR->Instance 					= TIM2;
	encoderR->Init.Period            	= 0xFFFF;
	encoderR->Init.Prescaler         	= 0;
	encoderR->Init.ClockDivision     	= 0;
	encoderR->Init.CounterMode       	= TIM_COUNTERMODE_UP;
	//encoderR->Init.RepetitionCounter 	= 3;
	HAL_TIM_Encoder_Init(encoderR, &sEncoderConfig);
	HAL_TIM_Encoder_Start(encoderR, TIM_CHANNEL_ALL);//*/
}
コード例 #4
0
ファイル: encoder.c プロジェクト: MbedTinkerer/test_encoder
void encoder2_init(void) {
  GPIO_InitTypeDef gpio_init;
  gpio_init.Pin = GPIO_PIN_12;
  gpio_init.Pull = GPIO_PULLUP;
  gpio_init.Speed = GPIO_SPEED_HIGH;
  gpio_init.Mode = GPIO_MODE_AF_PP;
  gpio_init.Alternate = GPIO_AF2_TIM4;

  __GPIOD_CLK_ENABLE();
  HAL_GPIO_Init(GPIOD, &gpio_init);

  gpio_init.Pin = GPIO_PIN_13;
  HAL_GPIO_Init(GPIOD, &gpio_init);

  __TIM4_CLK_ENABLE();

  hTIM4.Instance = TIM4;
  //  hTIM4.Init.Prescaler = (SystemCoreClock/65535) - 1; // 1s
  //  hTIM4.Init.CounterMode = TIM_COUNTERMODE_UP;
  //  hTIM4.Init.ClockDivision = 0;
  hTIM4.Init.Period = 65535;
  //hTIM4.Channel = HAL_TIM_ACTIVE_CHANNEL_1 | HAL_TIM_ACTIVE_CHANNEL_2;
  //HAL_TIM_Base_Init(&hTIM4);

  TIM_Encoder_InitTypeDef enc_init;
  enc_init.EncoderMode = TIM_ENCODERMODE_TI12;

  enc_init.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
  enc_init.IC2Polarity = TIM_INPUTCHANNELPOLARITY_RISING;

  enc_init.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  enc_init.IC2Selection = TIM_ICSELECTION_DIRECTTI;

  enc_init.IC1Prescaler = TIM_ICPSC_DIV1;
  enc_init.IC2Prescaler = TIM_ICPSC_DIV1;

  enc_init.IC1Filter = 0;
  enc_init.IC2Filter = 0;

  HAL_TIM_Encoder_Init(&hTIM4, &enc_init);

  __HAL_TIM_SetCounter(&hTIM4,0);
  HAL_TIM_Encoder_Start(&hTIM4, TIM_CHANNEL_1 | TIM_CHANNEL_2);
}
コード例 #5
0
ファイル: main-ex11.c プロジェクト: Shreeyak/mastering-stm32
/* TIM3 init function */
void MX_TIM3_Init(void) {
  TIM_Encoder_InitTypeDef sEncoderConfig;

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 0;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 65535;

  sEncoderConfig.EncoderMode = TIM_ENCODERMODE_TI12;

  sEncoderConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  sEncoderConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  sEncoderConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  sEncoderConfig.IC1Filter = 0;

  sEncoderConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
  sEncoderConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  sEncoderConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  sEncoderConfig.IC2Filter = 0;

  HAL_TIM_Encoder_Init(&htim3, &sEncoderConfig);
}
コード例 #6
0
ファイル: Encoder.c プロジェクト: macerobotics/MRPi1
/**********************************************************
 * @brief  Encoder init timer 8
 * @param  None
 * @retval None
**********************************************************/
static void encoder_iniTimer8(void)
{
TIM_HandleTypeDef timer8_initStruct;
TIM_Encoder_InitTypeDef t8Encod_initStruct;

  // TIM8 Clock Enable
  __TIM8_CLK_ENABLE();

  // Timer 8 init structure
  timer8_initStruct.Instance = TIM8;
  timer8_initStruct.Init.Prescaler = 0;
  timer8_initStruct.Init.CounterMode = TIM_COUNTERMODE_UP;
  timer8_initStruct.Init.Period = 0xFFFF;
  timer8_initStruct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  t8Encod_initStruct.EncoderMode = TIM_ENCODERMODE_TI12;

  t8Encod_initStruct.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
  t8Encod_initStruct.IC2Polarity = TIM_INPUTCHANNELPOLARITY_RISING;

  t8Encod_initStruct.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  t8Encod_initStruct.IC2Selection = TIM_ICSELECTION_DIRECTTI;

  t8Encod_initStruct.IC1Prescaler = TIM_ICPSC_DIV1;
  t8Encod_initStruct.IC2Prescaler = TIM_ICPSC_DIV1;

  t8Encod_initStruct.IC1Filter = 8;// digital filter
  t8Encod_initStruct.IC2Filter = 8;// digital filter


  // Init timer
  HAL_TIM_Encoder_Init(&timer8_initStruct,&t8Encod_initStruct);

  __HAL_TIM_SetCounter(&timer8_initStruct,0);

  HAL_TIM_Encoder_Start(&timer8_initStruct, TIM_CHANNEL_1 | TIM_CHANNEL_2);

}
コード例 #7
0
ファイル: main.c プロジェクト: eemei/library-stm32f4
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 168 MHz */
  SystemClock_Config();
  
  /* Configure LED1 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED3);
  
  /* Initialize TIM3 to emulate a quadrature encoder outputs */
  Init_TIM_Emulator(&EmulatorHandle);  

  /* -1- Initialize TIM1 to handle the encoder sensor */
  /* Initialize TIM1 peripheral as follow:
       + Period = 65535
       + Prescaler = 0
       + ClockDivision = 0
       + Counter direction = Up
  */
  Encoder_Handle.Instance = TIM1;  
  
  Encoder_Handle.Init.Period            = 65535;
  Encoder_Handle.Init.Prescaler         = 0;
  Encoder_Handle.Init.ClockDivision     = 0;
  Encoder_Handle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  Encoder_Handle.Init.RepetitionCounter = 0; 
    
  sEncoderConfig.EncoderMode        = TIM_ENCODERMODE_TI12;
  
  sEncoderConfig.IC1Polarity        = TIM_ICPOLARITY_RISING;   
  sEncoderConfig.IC1Selection       = TIM_ICSELECTION_DIRECTTI;  
  sEncoderConfig.IC1Prescaler       = TIM_ICPSC_DIV1; 
  sEncoderConfig.IC1Filter          = 0;
  
  sEncoderConfig.IC2Polarity        = TIM_ICPOLARITY_RISING;   
  sEncoderConfig.IC2Selection       = TIM_ICSELECTION_DIRECTTI;  
  sEncoderConfig.IC2Prescaler       = TIM_ICPSC_DIV1; 
  sEncoderConfig.IC2Filter          = 0; 
  
  if(HAL_TIM_Encoder_Init(&Encoder_Handle, &sEncoderConfig) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /* Start the encoder interface */
  HAL_TIM_Encoder_Start(&Encoder_Handle, TIM_CHANNEL_ALL);

  /* Infinite loop */
  while (1)
  {
    /* Step 1: */
    /* Emulate a Forward direction */
    Emulate_Forward_Direction(&EmulatorHandle);
    /* Insert 1s delay */
    HAL_Delay(1000);
    /* Get the current direction */
    uwDirection = __HAL_TIM_IS_TIM_COUNTING_DOWN(&Encoder_Handle);
    
    /* Step 2: */
    /* Emulate a Backward direction */
    Emulate_Backward_Direction(&EmulatorHandle);
    /* Insert 1s delay */
    HAL_Delay(1000);
    /* Get the current direction */
    uwDirection = __HAL_TIM_IS_TIM_COUNTING_DOWN(&Encoder_Handle);
  }
}
コード例 #8
0
ファイル: motor.c プロジェクト: gerdb/street-art-robot
/**
 * @brief  Configures IOs to control the two motors and one pump
 * @param  None
 * @retval None
 */
void MOTOR_Init(void) {
	GPIO_InitTypeDef GPIO_InitStruct;

	// Enable all timers
	PUMP_PWM_TIMER_CLK_ENABLE();
	MOTOR_PWM_TIMER_CLK_ENABLE();
	MOTOR_HALL_ENC1_TIMER_CLK_ENABLE();
	MOTOR_HALL_ENC2_TIMER_CLK_ENABLE();
	MOTOR_HALL_SPEED_TIMER_CLK_ENABLE();


	// Enable the clock for all IO pins
	MOTOR_PWM_CLK_ENABLE();
	MOTOR_CURR_CLK_ENABLE();
	MOTOR_HALL_M1_ENC_CLK_ENABLE();
	MOTOR_HALL_M2_ENC_CLK_ENABLE();
	MOTOR_HALL_SPEED_CLK_ENABLE();

	// Motor driver --------------------------------------------------------

	// Configure the 4 motor pins of motor 1 and 2 as normal IO
	GPIO_InitStruct.Pin = MOTOR_M2_IN1_PIN | MOTOR_M2_IN2_PIN
						| MOTOR_M1_IN1_PIN| MOTOR_M1_IN2_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
	GPIO_InitStruct.Alternate = MOTOR_PWM_TIMER_AF;
	HAL_GPIO_Init(MOTOR_PWM_PORT, &GPIO_InitStruct);

	// Configure the 2 motor pins of motor 3 as PWM output
	GPIO_InitStruct.Pin = MOTOR_M3_IN1_PIN | MOTOR_M3_IN2_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
	GPIO_InitStruct.Alternate = PUMP_PWM_TIMER_AF;
	HAL_GPIO_Init(MOTOR_PWM_PORT, &GPIO_InitStruct);

	// Configure the 10 motor current pins
	GPIO_InitStruct.Pin = MOTOR_M2_I0_PIN | MOTOR_M2_I1_PIN | MOTOR_M2_I2_PIN
						| MOTOR_M2_I3_PIN | MOTOR_M2_I4_PIN
						| MOTOR_M1_I0_PIN | MOTOR_M1_I1_PIN | MOTOR_M1_I2_PIN
						| MOTOR_M1_I3_PIN | MOTOR_M1_I4_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
	HAL_GPIO_Init(MOTOR_CURR_PORT, &GPIO_InitStruct);

    // Timer configuration for motor pwm
	htimMotor.Instance = MOTOR_PWM_TIMER;
	htimMotor.Init.Period = MOTOR_MAX - 1; // = 20kHz = 84MHz / 1 / 4200
	htimMotor.Init.Prescaler = 1-1;
	htimMotor.Init.ClockDivision = 1;
	htimMotor.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_PWM_Init(&htimMotor);

    // Timer configuration for pump
	htimPump.Instance = PUMP_PWM_TIMER;
	htimPump.Init.Period = MOTOR_MAX - 1; // = 20kHz = 168MHz / 2 / 4200
	htimPump.Init.Prescaler = 2-1;
	htimPump.Init.ClockDivision = 1;
	htimPump.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_PWM_Init(&htimPump);

    // Configure Timer 1 channel 1 and 2 as PWM output
    sConfigTimMotor.OCMode = TIM_OCMODE_PWM1;
    sConfigTimMotor.Pulse = 0;
    sConfigTimMotor.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigTimMotor.OCFastMode  = TIM_OCFAST_ENABLE;
    sConfigTimMotor.OCNPolarity = TIM_OCNPOLARITY_HIGH;
    sConfigTimMotor.OCIdleState = TIM_OCIDLESTATE_RESET;
    sConfigTimMotor.OCNIdleState= TIM_OCNIDLESTATE_SET;

    // Configure Timer 1 channel 1 as PWM output
    sConfigTimPump.OCMode = TIM_OCMODE_PWM1;
    sConfigTimPump.Pulse = 0;
    sConfigTimPump.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigTimPump.OCFastMode  = TIM_OCFAST_ENABLE;
    sConfigTimPump.OCNPolarity = TIM_OCNPOLARITY_HIGH;
    sConfigTimPump.OCIdleState = TIM_OCIDLESTATE_RESET;
    sConfigTimPump.OCNIdleState= TIM_OCNIDLESTATE_SET;

    // PWM Mode
    HAL_TIM_PWM_ConfigChannel(&htimMotor, &sConfigTimMotor, TIM_CHANNEL_1);
    HAL_TIM_PWM_ConfigChannel(&htimMotor, &sConfigTimMotor, TIM_CHANNEL_2);
    HAL_TIM_PWM_ConfigChannel(&htimMotor, &sConfigTimMotor, TIM_CHANNEL_3);
    HAL_TIM_PWM_ConfigChannel(&htimMotor, &sConfigTimMotor, TIM_CHANNEL_4);
    HAL_TIM_PWM_ConfigChannel(&htimPump, &sConfigTimPump, TIM_CHANNEL_1);
    HAL_TIM_PWM_ConfigChannel(&htimPump, &sConfigTimPump, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htimMotor, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htimMotor, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htimMotor, TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htimMotor, TIM_CHANNEL_4);
    HAL_TIM_PWM_Start(&htimPump, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htimPump, TIM_CHANNEL_2);

    MOTOR_SetVal(MOTOR_M1, 0, 0);
    MOTOR_SetVal(MOTOR_M2, 0 , 0);
    MOTOR_SetVal(MOTOR_PUMP, 0, 0);

    // Encoder ----------------------------------------------------------

	// Configure the hall encoder pins
	GPIO_InitStruct.Pin = MOTOR_HALL_M1A_ENC_PIN | MOTOR_HALL_M1B_ENC_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
	GPIO_InitStruct.Alternate = MOTOR_HALL_ENC1_TIMER_AF;
	HAL_GPIO_Init(MOTOR_HALL_M1_ENC_PORT, &GPIO_InitStruct);

	GPIO_InitStruct.Pin = MOTOR_HALL_M2A_ENC_PIN | MOTOR_HALL_M2B_ENC_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
	GPIO_InitStruct.Alternate = MOTOR_HALL_ENC2_TIMER_AF;
	HAL_GPIO_Init(MOTOR_HALL_M2_ENC_PORT, &GPIO_InitStruct);

	GPIO_InitStruct.Pin = MOTOR_HALL_M1A_SPEED_PIN | MOTOR_HALL_M1B_SPEED_PIN
						| MOTOR_HALL_M2A_SPEED_PIN | MOTOR_HALL_M2B_SPEED_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
	GPIO_InitStruct.Alternate = MOTOR_HALL_SPEED_TIMER_AF;
	HAL_GPIO_Init(MOTOR_HALL_SPEED_PORT, &GPIO_InitStruct);

    // Timer configuration for hall encoder M1
	htimEncM1.Instance = MOTOR_HALL_ENC1_TIMER;
	htimEncM1.Init.Period = 0xFFFF;
	htimEncM1.Init.Prescaler = 0;
	htimEncM1.Init.ClockDivision = 0;
	htimEncM1.Init.CounterMode = TIM_COUNTERMODE_UP;

	sConfigEncM.EncoderMode = TIM_ENCODERMODE_TI2;
	sConfigEncM.IC1Filter = 0;
	sConfigEncM.IC1Polarity = TIM_ICPOLARITY_RISING;
	sConfigEncM.IC1Prescaler = TIM_ICPSC_DIV1;
	sConfigEncM.IC1Selection = TIM_ICSELECTION_DIRECTTI;
	sConfigEncM.IC2Filter = 0;
	sConfigEncM.IC2Polarity = TIM_ICPOLARITY_RISING;
	sConfigEncM.IC2Prescaler = TIM_ICPSC_DIV1;
	sConfigEncM.IC2Selection = TIM_ICSELECTION_DIRECTTI;

    // Encoder Mode
	HAL_TIM_Encoder_Init(&htimEncM1, &sConfigEncM);
    HAL_TIM_Encoder_Start(&htimEncM1, TIM_CHANNEL_1);
    HAL_TIM_Encoder_Start(&htimEncM1, TIM_CHANNEL_2);

    // Timer configuration for hall encoder M2
	htimEncM2.Instance = MOTOR_HALL_ENC2_TIMER;
	htimEncM2.Init.Period = 0xFFFF;
	htimEncM2.Init.Prescaler = 0;
	htimEncM2.Init.ClockDivision = 0;
	htimEncM2.Init.CounterMode = TIM_COUNTERMODE_UP;

    // Encoder mode
	HAL_TIM_Encoder_Init(&htimEncM2, &sConfigEncM);
    HAL_TIM_Encoder_Start(&htimEncM2, TIM_CHANNEL_1);
    HAL_TIM_Encoder_Start(&htimEncM2, TIM_CHANNEL_2);

#ifdef MOTOR_MEASURE_SPEED

    // Timer configuration for input capture
    htimEncSpeed.Instance = MOTOR_HALL_SPEED_TIMER;
    htimEncSpeed.Init.Period = 0xFFFF;
    htimEncSpeed.Init.Prescaler = 84-1; // 10us
    htimEncSpeed.Init.ClockDivision = 0;
    htimEncSpeed.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_IC_Init(&htimEncSpeed);


    sConfigEncSpeed.ICFilter = 0;
    sConfigEncSpeed.ICPolarity = TIM_ICPOLARITY_RISING;
    sConfigEncSpeed.ICPrescaler = TIM_ICPSC_DIV1;
    sConfigEncSpeed.ICSelection = TIM_ICSELECTION_DIRECTTI;

    // Configure the NVIC
    HAL_NVIC_SetPriority(TIM_HALL_SPEED_IRQn, 0, 1);

    /* Enable the TIM8 global Interrupt */
    HAL_NVIC_EnableIRQ(TIM_HALL_SPEED_IRQn);

    // Input capture mode
    HAL_TIM_IC_ConfigChannel(&htimEncSpeed, &sConfigEncSpeed, TIM_CHANNEL_1);
    HAL_TIM_IC_ConfigChannel(&htimEncSpeed, &sConfigEncSpeed, TIM_CHANNEL_2);
    HAL_TIM_IC_ConfigChannel(&htimEncSpeed, &sConfigEncSpeed, TIM_CHANNEL_3);
    HAL_TIM_IC_ConfigChannel(&htimEncSpeed, &sConfigEncSpeed, TIM_CHANNEL_4);
    HAL_TIM_IC_Start_IT(&htimEncSpeed, TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htimEncSpeed, TIM_CHANNEL_2);
    HAL_TIM_IC_Start_IT(&htimEncSpeed, TIM_CHANNEL_3);
    HAL_TIM_IC_Start_IT(&htimEncSpeed, TIM_CHANNEL_4);
#endif //MOTOR_MEASURE_SPEED

}
コード例 #9
0
ファイル: main.c プロジェクト: eemei/library-stm32f4
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 180 MHz */
  SystemClock_Config();

  /* Initialize LED1 & LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED3);

  /* Initialize TIM3 to emulate a quadrature encoder outputs */
  Init_TIM_Emulator(&EmulatorHandle);

  /* -1- Initialize TIM1 to handle the encoder sensor */
  /* Initialize TIM1 peripheral as follow:
       + Period = 65535
       + Prescaler = 0
       + ClockDivision = 0
       + Counter direction = Up
  */
  Encoder_Handle.Instance = TIM1;

  Encoder_Handle.Init.Period            = 65535;
  Encoder_Handle.Init.Prescaler         = 0;
  Encoder_Handle.Init.ClockDivision     = 0;
  Encoder_Handle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  Encoder_Handle.Init.RepetitionCounter = 0;

  sEncoderConfig.EncoderMode        = TIM_ENCODERMODE_TI12;

  sEncoderConfig.IC1Polarity        = TIM_ICPOLARITY_RISING;
  sEncoderConfig.IC1Selection       = TIM_ICSELECTION_DIRECTTI;
  sEncoderConfig.IC1Prescaler       = TIM_ICPSC_DIV1;
  sEncoderConfig.IC1Filter          = 0;

  sEncoderConfig.IC2Polarity        = TIM_ICPOLARITY_RISING;
  sEncoderConfig.IC2Selection       = TIM_ICSELECTION_DIRECTTI;
  sEncoderConfig.IC2Prescaler       = TIM_ICPSC_DIV1;
  sEncoderConfig.IC2Filter          = 0;

  if(HAL_TIM_Encoder_Init(&Encoder_Handle, &sEncoderConfig) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start the encoder interface */
  HAL_TIM_Encoder_Start(&Encoder_Handle, TIM_CHANNEL_ALL);

  /* Infinite loop */
  while (1)
  {
    /* Step 1: */
    /* Emulate a Forward direction */
    Emulate_Forward_Direction(&EmulatorHandle);
    /* Insert 1s delay */
    HAL_Delay(1000);
    /* Get the current direction */
    uwDirection = __HAL_TIM_DIRECTION_STATUS(&Encoder_Handle);

    /* Step 2: */
    /* Emulate a Backward direction */
    Emulate_Backward_Direction(&EmulatorHandle);
    /* Insert 1s delay */
    HAL_Delay(1000);
    /* Get the current direction */
    uwDirection = __HAL_TIM_DIRECTION_STATUS(&Encoder_Handle);
  }
}
コード例 #10
0
ファイル: encodersbsp.c プロジェクト: Felipeasg/lf_lite_plus
void encodersbsp_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_Encoder_InitTypeDef sConfig;

	__TIM2_CLK_ENABLE();

	GPIO_InitStruct.Pin = GPIO_PIN_15;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
	GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

	GPIO_InitStruct.Pin = GPIO_PIN_3;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
	GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	__TIM5_CLK_ENABLE();

	GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
	GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


	mst_htim2.Instance = TIM2;
	mst_htim2.Init.Prescaler = 0;
	mst_htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
	mst_htim2.Init.Period = 0xffffffff;
	mst_htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
	sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
	sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
	sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
	sConfig.IC1Filter = 3;
	sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
	sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
	sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
	sConfig.IC2Filter = 3;
	HAL_TIM_Encoder_Init(&mst_htim2, &sConfig);


	mst_htim5.Instance = TIM5;
	mst_htim5.Init.Prescaler = 0;
	mst_htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
	mst_htim5.Init.Period = 0xffffffff;
	mst_htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
	sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
	sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
	sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
	sConfig.IC1Filter = 3;
	sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;
	sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
	sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
	sConfig.IC2Filter = 3;
	HAL_TIM_Encoder_Init(&mst_htim5, &sConfig);

	HAL_TIM_Encoder_Start(&mst_htim2, TIM_CHANNEL_1);
	HAL_TIM_Encoder_Start(&mst_htim2, TIM_CHANNEL_2);

	HAL_TIM_Encoder_Start(&mst_htim5, TIM_CHANNEL_1);
	HAL_TIM_Encoder_Start(&mst_htim5, TIM_CHANNEL_2);

}