cancel
Showing results for 
Search instead for 
Did you mean: 

Timer, motor _speed

bendkhilarwa
Associate III
Posted on April 09, 2016 at 16:45

Hello,

I wanna calculate the motor speed using timer of STM32F4. I wanna know the time spended in one revolution.  So , who would  explain for me how can I do that . thanks a lot.
11 REPLIES 11
bendkhilarwa
Associate III
Posted on April 15, 2016 at 21:26

hello, 

 I want to count the input signal (PA1) in a period  of 10 second. 

The PA1 can have two state:

state1 :-connected to 5v

state2: not connected to anything.

So I want that the variable impulsion be incremented (+1) for each translation from state 2 to state 1.

So I use the sysTick to configure a period of 10 second, in each I incremente the varibale impulsion. But this variable it's not well incremented. can you help me to solve this problem please.

#include ''stm32f4xx.h''

__IO uint32_t TimmingDelay;

uint64_t revolution_period;

int impulsion=0;

/**************************************************************************/

/**************************************************************************/

/**************************************************************************/

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GpioConfiguration;

  GpioConfiguration.GPIO_Pin =GPIO_Pin_1;

  GpioConfiguration.GPIO_Mode = GPIO_Mode_IN;

  GpioConfiguration.GPIO_OType = GPIO_OType_PP;

  GpioConfiguration.GPIO_Speed = GPIO_Speed_50MHz;

  GpioConfiguration.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOA,&GpioConfiguration);

  

}

/**************************************************************************/

void SysTick_Handler(void)

{

  if(TimmingDelay !=0)

  {

    TimmingDelay --;

   }

}  

/**************************************************************************/

 

void count_pulse(__IO uint32_t time)

{

  TimmingDelay = time;

  while(TimmingDelay !=0)

  {

    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)==Bit_SET)

    {

      impulsion=impulsion + 1;

    }

  }

}

/**************************************************************************/

int main(void)

{

     

//Enable the GPIOD Clock

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

// GPIOA Configuration

  GPIO_Configuration();

  //Systick Configuration

  NVIC_SetPriority (SysTick_IRQn, 0);

  SysTick_Config(SystemCoreClock/1000);

  while(1)

  {

// insert a delay of 10s 

    count_pulse(10000);

    revolution_period= 10000/impulsion;

    }

   

 

}

 

      

 

/**************************************************************************/

#ifdef  USE_FULL_ASSERT

/**

  * @brief  Reports the name of the source file and the source line number

  *         where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

{

  /* User can add his own implementation to report the file name and line number,

     ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */

  while (1)

  {}

}

#endif 

Posted on April 15, 2016 at 23:11

Ok, if there are two states, you should probably remember what the last state was, so you can determine if it actually changed, and only increment when it changes, not just when it is in the static high state.

The timers can be configured to count pulses on certain pins. It would be under External Count mode. See also other examples posted to the forum.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..