2013-12-20 03:09 PM
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);
}
2013-12-20 08:33 PM
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.2013-12-21 07:28 AM
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 regards2013-12-21 01:31 PM
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);
}
}
2013-12-21 01:49 PM
I don't really understand the merits of using encode mode here.
2013-12-21 05:04 PM
Hi
Thank you for help.not work yetI want control the rotation of motor. I will replce the user button with an encoder.they are another solutionbest regards2013-12-21 05:42 PM
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¤tviews=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