2022-04-21 05:20 AM
Hello Everyone!
Wanted to ask because I have a problem with getting the z pulse, or index channel to work on my encoder. I am using an encoder on a motor, and a nucleo board, and programming everything on stm32cubeide.
What is the correct way of doing this?
What I did:
- first I got the channels a and b to work, so now i get the counter value
code in main.c:
volatile uint32_t counter = 0;
volatile int16_t count = 0;
volatile int16_t position = 0;
//int16_t index = 0;
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
counter = __HAL_TIM_GET_COUNTER(htim);
count = (int16_t)counter;
position = count/4;
}
int main(void)
{
HAL_TIM_Encoder_Start_IT(&htim1,TIM_CHANNEL_ALL);
while (1)
{
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim);
}
- then i activated an interrupt
- then went to the stm32f7xx_it file and added this:
volatile int16_t counter;
if (HAL_GPIO_ReadPin(GPIOE, 13) == GPIO_PIN_SET) {counter = 0;}
Is this how to do that?
2022-04-21 07:01 AM
If you set up TIM1-> SMCR correctly to the wanted encoder mode (and everything else), you could actually:
a) simply read TIM1->CNT when you need the position
b) reset TIM1->CNT = 0 in the Z-pulse ISR
I don't see the need for callback stuff.
Unfortunately the timer cannot handle the Z-pulse as a 3rd input to reset the timer's counter - or am I wrong?
2022-04-22 12:06 AM
wait so it cannot handle it? What would be the way to do this then?
2022-04-24 11:01 PM
The way you do it, with an interrupt.
I just mean that the STM32 timer peripheral has no option for this 3rd encoder input / Z pulse.
2022-04-25 11:32 AM
Timers in STM32G4xx do have an Index Input option for encoder mode, and generally feature many extended features related to encoders.
JW