cancel
Showing results for 
Search instead for 
Did you mean: 

Use User button of STM32F4 as encoder mode (Timer)

badreddine
Associate II
Posted on December 21, 2013 at 00:09

Hey

In order to use the user button of stm32f4 discovery as encoder mode, I was configured timer2 as follows but it didn't work:  

void configureEncoder(void) {

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);// Enable GPIO_B

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);// ENABLE TIM2

// Configure PA.00 as Alternative Function in order to it connect to TI2 (external clock)

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Input/Output controlled by peripheral

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;// Use the button as external source clock

GPIO_Init(GPIOA, &GPIO_InitStructure);

// Connect the pins to their Alternate Functions

GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);

 

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Period = 0xffff;// max resolution

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

 

/* Configure the timer */

TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI2, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

TIM_SetAutoreload (TIM2, 0xffff);

 

/* Enable the TIM2 gloabal Interrupt */

NVIC_InitTypeDef   NVIC_InitStructure ;

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

 

/* TIM IT enable */

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    /* TIM2 enable counter */

 

TIM_ICInitTypeDef TIM_ICInitStructure;

 TIM_ICInitStructure.TIM_Channel     = TIM_Channel_2;

 TIM_ICInitStructure.TIM_ICPolarity  = TIM_ICPolarity_Rising;

 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

 TIM_ICInitStructure.TIM_ICFilter    = 0x0;

 TIM_ICInit(TIM2, &TIM_ICInitStructure);

    /* TIM2 counter enable */

    TIM_Cmd(TIM2, ENABLE);

}

// IRQ function

void TIM2_IRQHandler(void) // PASS takes approx 500ns of CPU time!

{

    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

    {

        //TODO: Interrupt Implement Me!!

        //TIM_GetCounter(TIM9)

     counter=TIM2->CNT;// _GetCounter(TIM2);

GPIO_SetBits(GPIOD,GPIO_Pin_13);

GPIO_ResetBits(GPIOD,GPIO_Pin_12);

 

    }

 

    // Clear the interrupt

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

}

6 REPLIES 6
Posted on December 21, 2013 at 05:33

Isn't PA.0 TIM2_CH1, so not TI2, but rather TI1

Clear the Update interrupt after you qualify it, not immediately prior to routine exit.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
badreddine
Associate II
Posted on December 21, 2013 at 16:28

Thank you for your help

I don't understand where I can clear the Update interrupt. (In the ''TIM2_IRQHander'' or in the main function).

Best regards

Posted on December 21, 2013 at 22:31

You'd definately need to handle it in the interrupt routine itself, otherwise you'd never be able to leave (it would keep re-entering)

void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) // Qualify the interrupt
{
// Clear the interrupt
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//TODO: Interrupt Implement Me!!
//TIM_GetCounter(TIM9)
counter=TIM2->CNT;// _GetCounter(TIM2); // Close to ZERO
GPIO_SetBits(GPIOD,GPIO_Pin_13);
GPIO_ResetBits(GPIOD,GPIO_Pin_12);
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 21, 2013 at 22:49

I don't really understand the merits of using encode mode here.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
badreddine
Associate II
Posted on December 22, 2013 at 02:04

Hi

Thank you for help.

not work yet

I want control the rotation of motor. I will replce the user button with an encoder.

they are another solution

best regards

Posted on December 22, 2013 at 02:42

Here for the F0, but relatively portable to the F4

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/Encoder%20Interface%20STM32F0%20Discovery&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=187]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FEncoder%20Interface%20STM32F0%20Discovery&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=187

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..