cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder interface

tvv_it
Associate
Posted on November 21, 2012 at 17:51

Hi everybody!

I have a problem when i reading data from ENCODER to stm32f103rc. My code is in the following:

/*      */

#include ''stm32f10x.h''

#include <stdio.h>

#include ''main.h''

#include ''stm32f10x_it.h''

#include ''stm32f10x_tim.h'' 

GPIO_InitTypeDef GPIO_InitStructure;

/* *****************  */

PUTCHAR_PROTOTYPE

{

/* Place your implementation of fputc here */

/* e.g. write a character to the USART */

USART_SendData(USART1, (uint8_t) ch);

/* Loop until the end of transmission */

while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

{}

return ch;

}

void Delay(__IO uint32_t num)

{

__IO uint32_t index = 0;

/* default system clock is 72MHz */

for(index = (720000 * num); index != 0; index--)

{

}

}

void USART_Configuration(void)

{

  // /* USARTx configured as follow:

  //      - BaudRate = 115200 baud  

  //     - Word Length = 8 Bits

  //    - One Stop Bit

  //    - No parity

  //   - Hardware flow control disabled (RTS and CTS signals)

  //    - Receive and transmit enabled

  USART_InitTypeDef USART_InitStructure;

USART_InitStructure.USART_BaudRate = 115200;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_ClearFlag(USART2,USART_FLAG_TC);

/* USART configuration */

USART_Init(USART1, &USART_InitStructure);

USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

//enable Receive Data register not empty interrupt

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

/* Enable USART */

USART_Cmd(USART1, ENABLE);

}

void RCC_Configuration(void)

{

    /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  

    initialize the PLL and update the SystemFrequency variable. */

    //SystemInit();

    /* Enable GPIOB clocks */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 

}

void Encoder(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

//GPIO_InitTypeDef GPIO_InitStructure;

//RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM2, ENABLE);

TIM_TimeBaseStructure.TIM_Period = 0x0FFF;

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI1,TIM_ICPolarity_Falling,

TIM_ICPolarity_Falling);

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_SetCounter(TIM2, 3200);

TIM_Cmd(TIM2, ENABLE);

}

void GPIO_Configuaration(void)

{

    /* Configure PA9 for USART Tx as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure PA10 for USART Rx as input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* encoder config*/

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;

    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

}

int main(void)

{

     uint16_t temp;

    /* Configure the system clocks */

    RCC_Configuration();    

    //Timer4_Configuration();

    Encoder();

//GPIO_Remap_Configuration();

GPIO_Configuaration();

USART_Configuration();

    while (1)

    {

        temp=TIM_GetCounter(TIM2);

        printf(''Gia tri encoder: %d\n'',temp);

Delay(5);

}

}

#ifdef  USE_FULL_ASSERT

/**

  * @brief  Reports the name of the source file and the source line number

  *   where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,

     ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */

/* Infinite loop */

while (1)

{

}

}

#endif

1 REPLY 1
stst9184
Associate II
Posted on November 23, 2012 at 11:08

Encoder shall have two digital inputs, you've configured just one of them.

joee