cancel
Showing results for 
Search instead for 
Did you mean: 

Help me USART STM32F4

pattaradanai
Associate II
Posted on August 09, 2013 at 18:34

What is wrong. it isn't show data in terminal,

**********************************************************************

&sharpinclude <stm32f4xx.h>

&sharpinclude <stm32f4xx_rcc.h>

&sharpinclude <stm32f4xx_gpio.h>

&sharpinclude <main.h>

&sharpinclude <stm32f4xx_usart.h>

&sharpinclude <stdio.h>

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

char str[14];

char msg1;

int msg;

//********************** Function ***********************************************

void USART_SETUP (void);

void delay(void);

void USART1_PUTC(unsigned char c);

void USART1_PUTS(unsigned char *s);

int USART1_GETC(void);

//************************ Main Function ***************************************

int main()

{

    USART_SETUP();

  STM_EVAL_LEDInit(LED3);

  STM_EVAL_LEDInit(LED4);

  STM_EVAL_LEDInit(LED5);

  STM_EVAL_LEDInit(LED6);

    STM_EVAL_LEDOn(LED6);

  while(1)

  {

        while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET);

        msg1 = USART_ReceiveData(USART1);

        while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);

        USART_SendData(USART1,msg1);

  }

 

 

}

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

void delay (void)

{

  unsigned int i,j;

  for(i=0;i<5000;i++)

    {

        for(j=0;j<700;j++);

    }

}

//***************************** USART_SETUP *******************************

void USART_SETUP(void)

{

    GPIO_InitTypeDef GPIO_InitStruct;

    USART_InitTypeDef USART_InitStruct;

    

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    /*

        USART2 use port A and pin 8-12

            PA8  is USART1_CK

            PA9  is USART1_TX

            PA10 is USART1_RX

            PA11 is USART1_CTS

            PA12 is USART1_RTS

    */

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);

    

    /* Configure PA2  in AF pushpull mode */

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 ;

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

  GPIO_Init(GPIOA, &GPIO_InitStruct);

    

    /* USARTx configured as follow:

        - BaudRate = 9600 baud  

        - Word Length = 8 Bits

        - One Stop Bit

        - No parity

        - Hardware flow control disabled (RTS and CTS signals)

        - Receive and transmit enabled

  */

  USART_InitStruct.USART_BaudRate = 115200;

  USART_InitStruct.USART_WordLength = USART_WordLength_8b;

  USART_InitStruct.USART_StopBits = USART_StopBits_1;

  USART_InitStruct.USART_Parity = USART_Parity_No;

  USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_Init(USART1, &USART_InitStruct);

    

    

    

    USART_Cmd(USART1, ENABLE);

}

void USART1_PUTC(unsigned char c)

{

    while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);

    USART_SendData(USART1,(int) c);

}

void USART1_PUTS(unsigned char *s)

{

    while(*s)

    {

        USART1_PUTC(*s++);

    }

}

int USART1_GETC(void)

{

    while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET);

    return(USART_ReceiveData(USART1));        

}

#stm32f4 #usart #rs232
6 REPLIES 6
Posted on August 12, 2013 at 00:45

You haven't explicitly stated that you're using an STM32F4-Discovery board, but if you are PA9 is not usable for a USART pin due to a huge bulk capacitor being attached to it.

Might help if your comments were somewhat coherent with the code.

A secondary issue with USART functionality is with timing, from both the perspective of the external crystal used, and the value defined for HSE_VALUE.

Thirdly, the output/input for the STM32 is CMOS levels, this is NOT compatible with RS232 levels, and requires some interface circuitry.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ran2
Associate III
Posted on August 12, 2013 at 10:34

Is this going to display some characters on your PC?

Posted on August 13, 2013 at 18:20

Is this going to display some characters on your PC?

The example as presented attempts to echo back received characters to the sender, presumably a terminal app on a PC.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pattaradanai
Associate II
Posted on August 14, 2013 at 06:24

First, I define HSE_Value = 8000000 in option and headfile stm32f4xx.h

Secondly, I connect port USART with interface circuit with MAX3232

Third, I connect port PA9 follow Datasheet STM32F407

and it same problem

Thank you

pattaradanai
Associate II
Posted on August 14, 2013 at 06:28

Yes, It show some character but it not english character.

 

Posted on August 14, 2013 at 14:03

Reinterating, PA9 is NOT usable on the STM32F4-Discovery board

Send a stream of characters, review signal on scope.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..