2014-12-01 07:33 AM
Hi,
since a couple of days I'm trying to get an optical encoder to work with the STM32F030F4. I followed the product specification on p. 19 which is telling me ''TIM3 timer is capable of handling quadrature (incremental) encoder signals''. So I tried to get it work on PA6 and PA7 - success. But I need these ports for SPI, so I tried basically the same code for PA6 and PB1, which is TIM3 too, but fails. Here the source code:GPIO_InitTypeDef GPIO_InitStructure;
/*TIM3 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable GPIO, clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Encoder unit connected to TIM3, quadrature mode */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
TIM_Cmd(TIM3, ENABLE);
Does anyone have a solution to this (even if it's on another series of ST uC)?
Thanks!
#rtfm #stm32f030f4-encoder
2014-12-01 09:42 AM
Look, you have to use channels 1 & 2 of the timer for encoder mode.
PB1 -> TIM3_CH4 not going to work PB5 -> TIM3_CH2 will complement PA6 TIM3_CH1 or PB4 TIM3_CH12014-12-01 11:18 AM
Thanks for your answer!
To prevent future mistakes - did I leave out to read a data sheet? Where can I find this information?2014-12-01 12:25 PM
The pin associations can be found in the Data Sheet, Timer block diagrams, and use cases, in the Reference Manual
http://www.st.com/web/catalog/mmc/FM141/SC1169/SS1574/LN1826/PF258968
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00088500.pdf
2014-12-01 12:33 PM
Thank you clive1
I think you got me wrong, I know how to find the pin associations. But I did not find the information for it's working only on CH1 & CH2 of the timer. Can you help me?2014-12-01 04:13 PM
Ok, let me go dig that up.
In the mean time here is the library source, and your options to use TI1 and TI2 nodes./**
* @brief Configures the TIMx Encoder Interface.
* @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
* @note TIM2 is not applicable for STM32F030 devices.
* @param TIM_EncoderMode: specifies the TIMx Encoder Mode.
* This parameter can be one of the following values:
* @arg TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
* @arg TIM_EncoderMode_TI2: Counter counts on TI2FP2 edge depending on TI1FP1 level.
* @arg TIM_EncoderMode_TI12: Counter counts on both TI1FP1 and TI2FP2 edges depending
* on the level of the other input.
* @param TIM_IC1Polarity: specifies the IC1 Polarity
* This parmeter can be one of the following values:
* @arg TIM_ICPolarity_Falling: IC Falling edge.
* @arg TIM_ICPolarity_Rising: IC Rising edge.
* @param TIM_IC2Polarity: specifies the IC2 Polarity
* This parmeter can be one of the following values:
* @arg TIM_ICPolarity_Falling: IC Falling edge.
* @arg TIM_ICPolarity_Rising: IC Rising edge.
* @retval None
*/
void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,
uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity)
{
uint16_t tmpsmcr = 0;
uint16_t tmpccmr1 = 0;
uint16_t tmpccer = 0;
/* Check the parameters */
assert_param(IS_TIM_LIST3_PERIPH(TIMx));
assert_param(IS_TIM_ENCODER_MODE(TIM_EncoderMode));
assert_param(IS_TIM_IC_POLARITY(TIM_IC1Polarity));
assert_param(IS_TIM_IC_POLARITY(TIM_IC2Polarity));
/* Get the TIMx SMCR register value */
tmpsmcr = TIMx->SMCR;
/* Get the TIMx CCMR1 register value */
tmpccmr1 = TIMx->CCMR1;
/* Get the TIMx CCER register value */
tmpccer = TIMx->CCER;
/* Set the encoder Mode */
tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_SMS));
tmpsmcr |= TIM_EncoderMode;
/* Select the Capture Compare 1 and the Capture Compare 2 as input */
tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC1S)) & (uint16_t)(~((uint16_t)TIM_CCMR1_CC2S)));
tmpccmr1 |= TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
/* Set the TI1 and the TI2 Polarities */
tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC1P | TIM_CCER_CC1NP)) & (uint16_t)~((uint16_t)(TIM_CCER_CC2P | TIM_CCER_CC2NP));
tmpccer |= (uint16_t)(TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << (uint16_t)4));
/* Write to TIMx SMCR */
TIMx->SMCR = tmpsmcr;
/* Write to TIMx CCMR1 */
TIMx->CCMR1 = tmpccmr1;
/* Write to TIMx CCER */
TIMx->CCER = tmpccer;
}
2014-12-01 04:30 PM
2014-12-06 04:10 AM
Thanks clive1!