cancel
Showing results for 
Search instead for 
Did you mean: 

STMF100RB Encoder Interface

Sean Mutlu
Associate II
Posted on July 10, 2017 at 03:16

Hello,

I have set up an encoder interface using SPL stm32f10x_tim.h which blinks an LED every time the motor makes a revolution. I am wondering how I would go about getting direction data from a similar setup. 

&sharpinclude 'stm32f10x_rcc.h'

&sharpinclude 'stm32f10x_gpio.h'

&sharpinclude 'stm32f10x_tim.h'

&sharpinclude 'misc.h'

/* Private function prototypes -----------------------------------------------*/

static void TIM_Config(void);

void TIM3_IRQHandler (void);

/* Private functions ---------------------------------------------------------*/

/**

* @brief Main program.

* @param None

* @retval None

*/

int main(void)

{

/* TIM Configuration */

TIM_Config();

/* Infinite loop */

while (1)

{

int status = TIM_GetCounter(TIM3);

printf ('status = %d\r\n',status);

}

}

/**

* @brief Configure the TIM pins and the TIM IRQ Handler.

* @param None

* @retval None

*/

static void TIM_Config(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

NVIC_InitTypeDef NVIC_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

/* TIM3 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

/* GPIOA clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);

/* TIM1 channel 2 pin (PE.11) configuration */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Time base configuration */

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

// 48 counts per revolution of the motor shaft & gear ratio 34:1

TIM_TimeBaseStructure.TIM_Period = 1632;

// Initialize TIM3

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

/* Set TIM3 to Encoder mode */

TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

/* TIM Interrupts enable */

TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

/* TIM3 enable counter */

TIM_Cmd(TIM3, ENABLE);

/* Enable the TIM3 global Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

/*blink LED every revolution */

void TIM3_IRQHandler(void)

{

GPIO_WriteBit(GPIOC,GPIO_Pin_9,Bit_SET);

for (int i = 0; i <100; i++);

GPIO_WriteBit(GPIOC,GPIO_Pin_9,Bit_RESET);

TIM_ClearITPendingBit(TIM3,TIM_IT_Update);

}

#encoder #timer #stm32f100rb
0 REPLIES 0