I have difficulties understanding and finding a solution to find the speed of a BLDC motor. Here is the specs:
- 3 Phase BLDC with 3x Hall sensors in 120 degrees, 2 pole-pair
The image below shows the situation:
I have setup a timer in XOR mode in the micro. The PSC is [19 - 1] and the Counter Period is [10000 - 1] while the timer clock is 80MHz.
My motor has the maximum speed of 3000RPM.
The timer is setup in XOR mode and with each transition generates an event. According to STM32F407 reference manual, in this mode each transition resets the CCR register to 0. It means if I add up this register 6 times, I should get one complete "Electrical Rotation" or half "physical rotation" since the motor is 2 pole pairs.
My questions:
A) I just picked that numbers for the timer PSC and counter period randomly...how these number are going to affect my measurement? e.g. with those numbers can I cover speeds from 1 to 3000 RPM?
B)
I have the following code inside the XOR ISR... does this code make sense?
static uint32_t speed = 0;
static uint32_t time = 0;
static int isMeasuring = 0;
void XOR_ISR() {
uint8_t state = readHallStateFromGPIO();
if(state == 0b101 && isMeasuring == 0) {
time = 0;
isMeasuring = 1;
time += TIM4->CCR1;
} else if(isMeasuring == 1 && state != 0b101) {
time += TIM4->CCR1;
} else if(isMeasuring == 1 && state == 0b101) {
time += TIM4->CCR1;
speed = time;
isMeasuring = 0;
}
Here, the speed (suppose my code is correct, should add up timer values for a whole period of hall sensor). Then I am going to convert it to RPM by converting frequency to time and multiply by 2 because of pole pairs.
Is this correct?