Question
Problem in input capturing to measure the rpm
I want to measure the rpm of bldc motor, so by using hall sensor (H.S) output we can capture the frequency. With help of Hall sensor frequency and num of motor poles, we can able to measure the RPM. I captured the frequency successfully by using input capture mode in stm32f103 .
Works well without running motor:
- I use an8008 multi-meter to generate frequency ( say 500 HZ ).
- And i use the timer pin to capture the frequency and it works well.
Works too bad while running motor: ( PROBLEM )
- But when i run motor and capture the frequency of H.S or multi-meter anything, the captured output frequency is not good varies rapidly.
- And i am using single MCU to run the motor as well as to capture the frequency.
// Input capture mode...
#include "stm32f10x.h"
int n; int gap;
float counter0,counter1,Counter,Frequency,RPM;
int main()
{
RCC->APB2ENR |=RCC_APB2ENR_TIM1EN;
GPIOA->CRH&=~GPIO_CRH_CNF8;
GPIOA->CRH|=(1<<3);
GPIOA->ODR|=(1<<8);
TIM1->PSC|=719;
TIM1->CCMR1 |=TIM_CCMR1_CC1S_0; // set T1[1] as input capture
TIM1->CCER &=~TIM_CCER_CC1P; // Rising edge
TIM1->CCMR1 &=~TIM_CCMR1_IC1PSC; // Diabling the PSC
TIM1->DIER |=TIM_DIER_UIE|TIM_DIER_CC1IE;
TIM1->CCER |=TIM_CCER_CC1E;
TIM1->CR1 |= TIM_CR1_CEN;
NVIC_EnableIRQ(TIM1_CC_IRQn);
while(1)
{
}
}
void TIM1_CC_IRQHandler(void)
{
if ((TIM1->SR & TIM_SR_CC1IF) != 0)
{
if ((TIM1->SR & TIM_SR_CC1OF) != 0) /* Check the overflow */
{
/* Overflow error management */
gap = 0; /* Reinitialize the laps computing */
TIM1->SR &= ~(TIM_SR_CC1OF | TIM_SR_CC1IF); /* Clear the flags */
}
if (gap == 0) /* Test if it is the first rising edge */
{
counter0 = TIM1->CCR1; /* Read the capture counter which clears the
CC1ICF */
gap = 1; /* Indicate that the first rising edge has yet been detected */
}
else
{
counter1 = TIM1->CCR1; /* Read the capture counter which clears the
CC1ICF */
if (counter1 > counter0) /* Check capture counter overflow */
{
Counter = counter1 - counter0;
Frequency=100000/Counter;
RPM=(Frequency*60)/4;
}
else
{
Counter = counter1 + 0xFFFF - counter0 + 1;
Frequency=100000/Counter;
RPM=(Frequency*60)/4;
}
counter0 = counter1;
}
}
else
{
/* Unexpected Interrupt */
/* Manage an error for robust application */
}
}MCU - stm32f103
Timer pin - A8
