Skip to main content
Visitor II
September 18, 2024
Question

Why is my SysTick timer not incrementing, causing no RPM calculation?

  • September 18, 2024
  • 6 replies
  • 3655 views

Hi all,

I am working on a motor control project using STM32, and I am trying to calculate the RPM of a BLDC motor using Hall sensor pulses. I have set up a SysTick timer to trigger an interrupt every 1 millisecond, which should increment a counter (sys_tick_count). This counter is then used in my millis() function to calculate elapsed time.

However, I am facing an issue where the RPM calculation doesn't seem to trigger because the time difference (time_diff) remains zero throughout the execution. It seems like the millis() function is not returning an incrementing system time, so the RPM is never calculated.

Here's the relevant portion of my code:

 

// SysTick timer initialization
void SysTick_Init(void)
{
 SysTick_Config(SystemCoreClock / 1000); // Interrupt every 1ms
}

// SysTick Handler
void SysTick_Handler(void)
{
 sys_tick_count++;
}

// Simple function to get the time in milliseconds
uint32_t millis(void)
{
 return sys_tick_count; // Return system uptime in milliseconds
}

// Function to calculate speed (RPM)
float calculate_speed(void)
{
 static uint32_t last_time = 0;
 uint32_t current_time = millis();
 float rpm = 0;

 uint32_t time_diff = current_time - last_time;
 printf("Time difference: %lu ms\n", time_diff); // Debugging output

 if (time_diff >= 1000) {
 rpm = (hall_pulse_count / 6.0) * 60.0; // Assuming 6 pulses per revolution
 printf("RPM calculation, hall_pulse_count: %lu, RPM: %f\n",
 hall_pulse_count,
 rpm);
 hall_pulse_count = 0;
 last_time = current_time;
 }
 else
 {
 printf("Waiting to calculate RPM, time remaining: %lu ms\n", 1000 - time_diff);
 }
 return rpm;
}

 

My Observations:

  • The SysTick_Handler does not seem to be updating sys_tick_count properly, as time_diff remains zero.
  • I am unsure if I am configuring the SysTick timer or handling the SysTick interrupt correctly.

Question:

  • Is there something wrong with my SysTick_Handler or the SysTick_Init setup?
  • Could there be something interfering with the SysTick timer, or is there a better way to handle this kind of timing?

Any help would be greatly appreciated!

 

    This topic has been closed for replies.

    6 replies

    Graduate II
    September 18, 2024

    Use the </> when posting code so it's formatted and readable.

    Graduate II
    September 18, 2024

    Maybe you didn't define sys_tick_count as volatile. Don't know because you didn't show how you declared it.

    Super User
    September 18, 2024

    Is sys_tick_count defined volatile? Are you inside of another interrupt with same or higher priority?

    Make the example simpler, remove all the code and just observe sys_tick_count in the debugger.

    romiAuthor
    Visitor II
    September 18, 2024

    Thanks for your input! I ensured that sys_tick_count is declared as volatile. I followed the steps you outlined, and now the SysTick is functioning correctly and incrementing as expected.

    However, the actual speed is still showing as zero, even though the motor is running. The Hall sensors seem to be working (I verified the signals). Could there be an issue with the logic in the way the speed is being calculated or the pulse counting? Any further advice would be very helpful.

    Super User
    September 18, 2024

    You're not giving us enough info to determine that. When is calculate_speed called? We have no idea. Maybe it's never called. Maybe it's called too often. Maybe not enough, maybe at the wrong times. Verify it's getting called at the expected rate. What speeds do you expect?

    romiAuthor
    Visitor II
    September 18, 2024

    Thank you for your suggestions. I followed your advice and made sure that sys_tick_count is defined as volatile. I also checked if I was inside another interrupt of the same or higher priority, but everything seems correctly prioritized.

    I simplified the code as you suggested, removing other logic, and monitored the sys_tick_count.  The SysTick seems to be incrementing correctly, but the actual speed is still showing as zero, even though the motor is running and the Hall sensors appear to be functioning correctly. Could there be something I am missing with the Hall sensor signal counting or the timing? I appreciate any further insights you might have.  

    ST Employee
    September 18, 2024

    Hello @romi

    Maybe add a debugging statement to print the "hall_pulse_count" value each time the interrupt handler is called, to see if the pulses are being counted correctly 

    Graduate II
    September 18, 2024

    Obviously, your first if statement is not being entered. If hall_pulse count is at least 1 and you compare is >= 200 then your rpm will return 10. So check your current_time is greater than last_time. Probably something wrong with getting millis value

     

    float calculate_speed(void) {
    	static uint32_t last_time = 0;
    	uint32_t current_time = millis(); 
    	float rpm = 0;
    
    	if (current_time - last_time >= 200) {
    		if (hall_pulse_count > 0) {
    			rpm = (hall_pulse_count / 6.0) * 60.0;
    		}
    		hall_pulse_count = 0;
    		last_time = current_time;
    	}
    
    	return rpm;
    }