I want to do a rpm counter. I also have a problem with the hall sensor. STM32F103C8 microcontroller, sensor such https://www.aliexpress.com/item/329...ail.1000016.1.333239a3rFRUR9&isOrigTitle=true , IDE Keil 5.
Setting the clock:
Setting the timer:
If I understand correctly, 1 "tick" of the timer is 1 microsecond.
The D0 pin of the hall sensor is connected to timer 3 input 1. The function hallSensor is called in a FreeRTOS thread.When I bring the magnet to the sensor, I get into the interrupt. In debug mode, if put a breakpoint on rpmTmp = 60/halfCalc; I get a value. But I am not sure of its correctness. If do not set a breakpoint, the RPM is not calculated at all and is always zero.
Source code:
Setting the clock:
Setting the timer:
If I understand correctly, 1 "tick" of the timer is 1 microsecond.
The D0 pin of the hall sensor is connected to timer 3 input 1. The function hallSensor is called in a FreeRTOS thread.When I bring the magnet to the sensor, I get into the interrupt. In debug mode, if put a breakpoint on rpmTmp = 60/halfCalc; I get a value. But I am not sure of its correctness. If do not set a breakpoint, the RPM is not calculated at all and is always zero.
Source code:
JavaScript:
static void rpmCalc(void);
//-----------------------------------------------------------------------------
void hallSensor(void)
{
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
while(1)
{
if(flagUpdateData == 1)
{
flagUpdateData = 0;
rpmCalc();
}
//--- Reset RPM if 1 second no new value
/*while(flagUpdateData != 1)
{
osDelay(10);
cntWait++;
if(cntWait == 100)
{
rpm = 0;
cntWait = 0;
}
if(flagUpdateData == 1)
{
break;
}
}*/
osDelay(50);
}
}
//-----------------------------------------------------------------------------
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
cntCapture++;
if(cntCapture == 1)
{
capValue1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
//__HAL_TIM_SET_COUNTER(&htim3, 0x0000); // Reset counter
//--- Reset counter
TIM3->CNT = 0;
}
else if(cntCapture == 2)
{
capValue2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
//__HAL_TIM_SET_COUNTER(&htim3, 0x0000); // Reset counter
//--- Reset counter
TIM3->CNT = 0;
cntCapture = 0;
flagUpdateData = 1;
}
__HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_UPDATE);
//currTime = osKernelSysTick();
}
}
}
//-----------------------------------------------------------------------------
static void rpmCalc(void)
{
uint32_t halfCalc = 0;
//--- If the values are incorrect
if((capValue2 - capValue1) <= 5)
{
cntCapture = 0;
flagUpdateData = 0;
TIM3->CNT = 0;
return;
}
//--- Computation of the period, translation in seconds
halfCalc = (capValue2 - capValue1)/1000;
//--- Setting a ban on reading before writing the value of RPM
setSemaphor(SemHallBusyHandle);
//--- Calculation RPM
rpmTmp = 60/halfCalc;
//--- Cancel reading ban
clrSemaphor(SemHallBusyHandle);
}
//-----------------------------------------------------------------------------
uint32_t hallGetRpm(void)
{
while(getSemaphor(SemHallBusyHandle))
{
osDelay(5);
}
return rpmTmp;
}
Last edited: