cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103 encoder mode with as5040

domanski0karol
Associate III
Posted on April 26, 2015 at 22:50

Hi, I have problem with encoder mode. AS5040 is in quadrature output. I use this code and it doesnt work, where is the problem?

void gpio_conf(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM1_Configuration(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM1, ENABLE);
}

#stm32 #encoder #mode
9 REPLIES 9
Posted on April 26, 2015 at 23:56

Presuming the pins are correct

void TIM1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 65535; // Maximal
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // Not used
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
domanski0karol
Associate III
Posted on April 27, 2015 at 14:24

I think thats it doesnt work. I check it with this code and nothing.

int main(void) {
rcc_conf();
gpio_conf();
TIM1_Configuration();
while (1) {
test = TIM1->CNT;
if(test > 1) {
led_conf(1,1); //led on
}
}
}

Posted on April 27, 2015 at 14:40

PB8 and PB9 relate to TIM1_CH1 and TIM1_CH2 how exactly?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
domanski0karol
Associate III
Posted on April 27, 2015 at 15:14

I dont understand. The pins are connect to AS5040 to 3 and 4 pin.

http://circuits.datasheetdir.com/374/AS5040-pinout.jpg

Posted on April 27, 2015 at 16:17

Ok, but does the STM32 Data Sheet suggest the pins relate to TIM1 or are you just inventing connectivity?

http://www.st.com/web/en/resource/technical/document/datasheet/CD00161566.pdf

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
domanski0karol
Associate III
Posted on April 27, 2015 at 18:11

Oh, I think about PB13 and PB14, I change it, but it doesnt help.

Posted on April 27, 2015 at 21:09

Ok, and those are the NEGATED channels. You need to use TIM1_CH1 and TIM1_CH2

So PE9/PE11 as remapped pins, PA8/PA9 in non-remapped. The pin options here are pretty limited.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
domanski0karol
Associate III
Posted on April 28, 2015 at 08:02

domanski0karol
Associate III
Posted on April 28, 2015 at 17:54

Ok, thank you for this. So if I cant do it with encoder mode how can I do this? With interruption?