AnsweredAssumed Answered

Receive data is wrong

Question asked by ANDON Muhendislik on Jul 12, 2014
Latest reply on Jul 16, 2014 by ANDON Muhendislik
USART communication with stm32f4 discovery application did. At the beginning of the data sent to the PC comes an unwanted character. Sent from the computer to one of the characters I can not even. 
I changed it to HSE VALUE = 8000000 
Baud rate settings should I say I could not, I was sent from the microprocessor to the computer I can see the data. But I get so meaningless data.

Note: I used the MAX232 IC.

#include "stm32f4_discovery.h"
#include <stm32f4xx_usart.h>

#define MAX_STRLEN 42
volatile char received_string[MAX_STRLEN+1];

void init_USART2(uint32_t baudrate){

     GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX
     USART_InitTypeDef USART_InitStruct; // this is for the USART2 initilization
     NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)

     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // Pins 6 (TX) and 7 (RX) are used
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;                // the pins are configured as alternate function so the USART peripheral has access to them
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;          // this defines the IO speed and has nothing to do with the baudrate!
     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;               // this defines the output type as push pull mode (as opposed to open drain)
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;               // this activates the pullup resistors on the IO pins
     GPIO_Init(GPIOA, &GPIO_InitStruct);                         // now all the values are passed to the GPIO_Init() function which sets the GPIO registers

     GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); //
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);

     USART_InitStruct.USART_BaudRate = baudrate;                    // the baudrate is set to the value we passed into this init function
     USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)
     USART_InitStruct.USART_StopBits = USART_StopBits_1;          // we want 1 stop bit (standard)
     USART_InitStruct.USART_Parity = USART_Parity_No;          // we don't want a parity bit (standard)
     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
     USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver
     USART_Init(USART2, &USART_InitStruct);                         // again all the properties are passed to the USART_Init function which takes care of all the bit setting

     USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // enable the USART2 receive interrupt

     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;           // we want to configure the USART2 interrupts
     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART2 interrupts
     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;           // this sets the subpriority inside the group
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                // the USART2 interrupts are globally enabled
     NVIC_Init(&NVIC_InitStructure);                                    // the properties are passed to the NVIC_Init function which takes care of the low level stuff


void USART_puts(USART_TypeDef* USARTx, volatile char *s){

          // wait until data register is empty
          while( !(USARTx->SR & 0x00000040) );
          USART_SendData(USARTx, *s);

int main(void) {
     uint16_t  GERI=48;
  init_USART2(115200); // initialize USART1 @ 115200 baud

  USART_puts(USART2, "SEND MESSAGE"); // just send a message to indicate that it works

  while (1){
     * You can do whatever you want in here

void USART2_IRQHandler(void){

     // check if the USART1 receive interrupt flag was set
     if( USART_GetITStatus(USART2, USART_IT_RXNE) ){

          static uint8_t cnt = 0; // this counter is used to determine the string length
          char t = USART2->DR; // the character from the USART1 data register is saved in t

          /* check if the received character is not the LF character (used to determine end of string)
           * or the if the maximum string length has been been reached

          if( (t != '\n') && (cnt < MAX_STRLEN) ){
               received_string[cnt] = t;
          else{ // otherwise reset the character counter and print the received string
               cnt = 0;
               USART_puts(USART2, received_string);