2013-05-08 04:08 AM
Hi, can anyone provide an example of how to set up a counter to work with 2 quadrature inputs for up/down encoder counting?
Thanks #stm32-pulse-counter2013-05-08 08:08 AM
Quadrature encoder example.... for up/down encoder counting?
this will lead you astray, ''for up/down encoder counting'' you do not need a ''quadrature encoder'' you need a ''quadrature decoder'' Erik
2013-05-08 08:57 AM
This is a quick blind implementation, should suffice.
// STM32 TIM3 Encoder Decoder (PC.06:A PC.07:B) VLDiscovery - sourcer32@gmail.com
#include ''stm32F10x.h''
#include ''STM32vldiscovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
// clock for GPIO and AFIO (for ReMap)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
// clock for TIM3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// PC.06 TIM3_CH1, PC.07 TIM3_CH2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig( GPIO_FullRemap_TIM3, ENABLE ); // Map TIM3 to GPIOC
}
/**************************************************************************************/
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 65535; // Maximal
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM3, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM3_Configuration();
// TIM3->CNT contains current position
while(1); // Do not exit
}
2014-04-14 11:04 AM
Will Encoder Interface Mode always use CH1 and CH2 for the pins?
-Bill2014-04-14 12:11 PM
Will Encoder Interface Mode always use CH1 and CH2 for the pins?
That's how it's wired. Each TIM has only one counting element.2016-09-16 09:48 AM
Hi,
Could you advise and show how the general timer (e.g TIM4) of STM32F1xx be configured as a counter which counts pulses input and with direction input to control up or down counting?Thanks,Tanchi68062016-09-16 10:10 AM
Wouldn't the provided code pretty do that? Change pins and configuration to use TIM4 instead of TIM3
2016-09-17 02:07 AM
Hi,
I modified the code as attached. TIM4 reads no problem with encoder A/B signals applied to pins PD12/PD But it reads only 0~65535 (or 0~1 with different setting), when pulse and direction signals are applied. What else should TIM4 should be configured in order TIM4 can count the pulse up or down controlled by a direction signal? tanchi6806 ________________ Attachments : puDir_Init.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0rR&d=%2Fa%2F0X0000000bee%2FvM65hZtzyZxnVLveKLsz8Aww5mE_xztRD2IrAQ2Kahw&asPdf=false