AnsweredAssumed Answered

Help me USART STM32F4

Question asked by kiratiwudhikul.phatt on Aug 9, 2013
Latest reply on Aug 14, 2013 by Clive One
What is wrong. it isn't show data in terminal,
**********************************************************************
#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <main.h>
#include <stm32f4xx_usart.h>
#include <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));        
}


Outcomes