In actual I need to measure speed of motor. I have followed a procedure, i.e
- use Encoder mode to get pulses from sensor.
- count number of these pulses in 1 sec~1000msec using SysTick_Handler(ISR). Means use SysTick_Handler function as a time base to count pulses in 1000msec.
Now, I am bit confused about how to encorporate step no. 2 in codes. Because I have been taught that Systick Handler(ISR) is called every 1 millisecond. And if it is so, while passing through lines of codes inside Handler, will the duration of 1 millisecond sec not get passed. Will the Systick_Handler not get called again before even completely counting for 1 millisecond. Also, how do I make sure that the Systick_Handler counts for only one second and then again starts counting afresh after duration of 1 sec gets over.
This is the Handler function.
void SysTick_Handler(void)
{
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
Encoder_Start(); //encoder mode start
counter=Encoder_Read(void); //reading counter(defined globally and shared by handler and main)
Encoder_Stop(); //encoder mode stop
}
And this is the main
main.c
.
.
.
uint32_t Encoder_Read(void) //reads counts from sensor
{
return TIM3->CNT;
}
void Encoder_Start(void)
{
HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
}
void Encoder_Stop(void)
{
HAL_TIM_Encoder_Stop(&htim3, TIM_CHANNEL_ALL);
}
main()
{
.
.
while()
{
speed=counter*constant; //speed, counter is used here
printf("\n%d",speed); //print speed using semihosting on eclipse
PWM code;
PWM code;
PWM code;
}
.
.
}
My original question was this
Development board is STM32F407, OS is Linux(Ubuntu), code is generated using STM32CubeMX.
Any ideas about this?
Or any other alternative, I need to have a time base of 1 sec to measure the number of counts from sensor(LM393 based) and use this time base again and again to measure the speed and display it every second.