Hello,
i am using an STM32F103C8T6 to read data from two hall sensors to detect the direction of rotation of a pulley mounted on a motor.
I am not able to count the pulses from the sensor with accuracy, i read multiple interrupts and is a problem. What i am doing wrong?
Attached the code to read the data from one sensor (i have the same problems with both sensors):
#define HAL_SENSOR_LINE_A0_PORT GPIOA
#define HAL_SENSOR_LINE_A0_PIN 0
void hal_sensor_init(void)
{
/* Enabling events on rising edge of the encoder lines.*/
// line A0
palSetLineMode(HAL_SENSOR_LINE_A0, PAL_MODE_INPUT_PULLUP);
palEnableLineEvent(HAL_SENSOR_LINE_A0, PAL_EVENT_MODE_FALLING_EDGE);
palSetLineCallback(HAL_SENSOR_LINE_A0, (palcallback_t)hal_sensor_line_a0_rising_edge, NULL);
}
static void hal_sensor_line_a0_rising_edge(void)
{
chSysLock();
if (palReadPad(GPIOA, 0) == PAL_LOW)
{
hal_sensor_A0_counter++;
hal_sensor_evaluate_data(PULLEY_PROBE_A0);
}
chSysUnlock();
}
Thank you,
Antonio
Hall sensor does not recognize FALLING_EDGE
Moderator: RoccoMarco
- Giovanni
- Site Admin
- Posts: 14893
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Hall sensor does not recognize FALLING_EDGE
Hi,
The problem is that the ISR is not executed immediately, there could be a delay caused by other IRQs or critical sections. The pin could have become high before you read it in the ISR.
Just don't read the pin state, after a falling edge, you can assume it went low.
A more accurate way to do this is to use a timer to count input pulses, you need to write low level code for this however, see the TIMx section in the STM32 Reference Manual.
Giovanni
The problem is that the ISR is not executed immediately, there could be a delay caused by other IRQs or critical sections. The pin could have become high before you read it in the ISR.
Just don't read the pin state, after a falling edge, you can assume it went low.
A more accurate way to do this is to use a timer to count input pulses, you need to write low level code for this however, see the TIMx section in the STM32 Reference Manual.
Giovanni
Re: Hall sensor does not recognize FALLING_EDGE
Hello.
I'm working with cupracing to the hall sensor development.
We need only to collect the correct sequence of awaked sensors in order to evaluate the movement direction.
So we need only that the falling edge event is correctly detected.
At the moment, for example, we have 6 real (hand-made) events but we have more than 20 counts (counters is only for debug).
Where is the mistake?
Thanks, Edoardo.
I'm working with cupracing to the hall sensor development.
We need only to collect the correct sequence of awaked sensors in order to evaluate the movement direction.
So we need only that the falling edge event is correctly detected.
At the moment, for example, we have 6 real (hand-made) events but we have more than 20 counts (counters is only for debug).
Where is the mistake?
Thanks, Edoardo.
-
mikeprotts
- Posts: 166
- Joined: Wed Jan 09, 2019 12:37 pm
- Has thanked: 19 times
- Been thanked: 31 times
Re: Hall sensor does not recognize FALLING_EDGE
The timing here looks uncertain. The time taken for this code to run could be longer than the time for a new trigger.
It's possible you may get additional triggers from crosstalk if you've got parallel wiring, or any significant unshielded length.
Also priorities matter, if you have to catch every trigger, you need to be at the highest priority of any interrupt.
It would be a good idea to include a second counter off an else, to see if you are getting triggered when the pin is high.
It would help to make comments and names match the falling edge processing
.
Mike
It's possible you may get additional triggers from crosstalk if you've got parallel wiring, or any significant unshielded length.
Also priorities matter, if you have to catch every trigger, you need to be at the highest priority of any interrupt.
It would be a good idea to include a second counter off an else, to see if you are getting triggered when the pin is high.
It would help to make comments and names match the falling edge processing
Mike