Reputation: 21
I have a question about using a timer to count pulses form a encoder (only one pin) I don't care about the direction.
I'm tryning to use Timer 3, but I'm not sure how to set it up in CubeMx.
I start it with: HAL_TIM_Base_Start( mpEncoderTim );
And read with: count = mpEncoderTim->Instance->CNT;
Hope anyone of you, have done this before, so you can give me a hint
thanks in advance and best regards panduro..
Upvotes: 0
Views: 3011
Reputation: 21
Thanks.
I found the error, the timer period was set to 0, so the timer would never count higher than 0 :-(
best regards..
Upvotes: 1
Reputation: 104
I'v had some kind of similar issue with stepper motor. I'v made a stepper driver code for (DRV8825, A4899 chips) , code is iRQ based and with maths for motor acceleration, speed and etc. But I had to test if it is accurate, so I had to count steps. For testing case I configured concrete pin as ETR(external trigger input) - it is input pin,my stepper output pin goes to that input,(as for sure goes to motor too). And each rising or lowering edge(depends on configuration) generates an irq, so at irq I can set counter of steps, in your case pulses.
I am not familiar with cubeMX stuff, most of the time I write directly to register or using old SPL(standard peripherial drivers). I will show how it works with SPL as it should be more readable for cubeMX users.
Anyway I just try to show basic idea how to do it.This concrete code works on STM32f030
Firstly configure input pin (read datasheet just few pins can work as external triggers and with concrete timers)
void digital_input_config (void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_2);
}
Now you need a timer configuration, each timer has different pins with ETR, so it depends on your STM32 MCU.You have to read data sheet accurately.
void enable_capture_TIM1(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_DIV_NONE,TIM_ExtTRGPolarity_NonInverted,0x00);
TIM_SelectInputTrigger(TIM1,TIM_TS_ETRF);
TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Trigger);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = TIM1_BRK_UP_TRG_COM_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
TIM_Cmd(TIM1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_Trigger, ENABLE);
}
And at the IRQ you can do pulse counting.
void TIM1_BRK_UP_TRG_COM_IRQHandler(void){
if(TIM_GetITStatus(TIM1, TIM_IT_Trigger) != RESET) {
TIM_ClearITPendingBit(TIM1, TIM_IT_Trigger);
PulseCNT++;
}
}
I'v tested this code with pulse generator and it's really accurate, it can be used for pulse count, or frequency measuring.
Upvotes: 0