cancel
Showing results for 
Search instead for 
Did you mean: 

USART (RS232) Help

wing
Associate
Posted on October 02, 2011 at 01:31

Hi,

I'm new to programming on the STM32VL-Discovery, I'm trying to get USART or RS232 to work as it is part of my Project.

I got this from the STM32F100 examples, but it is not working. Help would be appreciated thanks.

Below is the following code I tried modifying to make it work but still not working after many tries:

#include ''stm32f10x.h''

#include <stdio.h>

/** @addtogroup STM32F10x_StdPeriph_Examples

  * @{

  */

/** @addtogroup USART_Printf

  * @{

  */

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

/* Private function prototypes -----------------------------------------------*/

#ifdef __GNUC__

  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf

     set to 'Yes') calls __io_putchar() */

  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif /* __GNUC__ */

 

/* Private functions ---------------------------------------------------------*/

void GPIO_ (void);

void USART_Config (void);

/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

int main(void)

{

       GPIO_();

       USART_Config();

  /* Output a message on Hyperterminal using printf function */

  printf(''\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r'');

  while (1)

  {

  }

}

/**

  * @brief  Retargets the C library printf function to the USART.

  * @param  None

  * @retval None

  */

PUTCHAR_PROTOTYPE

{

  /* Place your implementation of fputc here */

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

  USART_SendData(USART2, (uint8_t) ch);

  /* Loop until the end of transmission */

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

  {}

  return ch;

}

#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

void GPIO_ (void)

{

    //Enable Clock

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO| RCC_APB2Periph_USART1,ENABLE);

    //RX Port C Pin 8

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;

    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOC,&GPIO_InitStructure);

    //TX Port C Pin 9

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

    GPIO_Init(GPIOC,&GPIO_InitStructure);

}

void USART_Config (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_DeInit(USART1);

      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_Init(USART1,&USART_InitStructure);

      //Enable USART1

      USART_Cmd(USART1,ENABLE);

}

4 REPLIES 4
raptorhal2
Lead
Posted on October 02, 2011 at 02:23

What happens when you single step through the code using your debugger ?

Cheers, Hal

Posted on October 03, 2011 at 03:27

Decide if you are using USART1 or USART2, because you refer to USART2 for the character output, why?

USART1 doesn't come out of PC8/9, in fact no USART or remapped USART use these pins. Why are you trying to use them, or expecting them to work?

USART1 uses PA9 (Tx), and PA10 (Rx), suggest you read the data manual more thoroughly, and use these pins.

http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00251732.pdf

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
wing
Associate
Posted on October 06, 2011 at 22:40

Thanks for the help I got it to display something now, does anyone know how to get printf and scanf to work on usart1?

I'm using Atollic to program

Posted on October 07, 2011 at 01:31

Atollic Lite doesn't support them. If you're using the full version look to see if there are any hosting/retargeting examples.

Absent that, you can always use sprintf(), and sscanf() variants. Which might be better choices if you expect to get data from multiple sources. Also watch stack utilization/sizes as these class of functions tend to be quite hungry.

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