cancel
Showing results for 
Search instead for 
Did you mean: 

Receive data is wrong

andonmuhendislik
Associate II
Posted on July 12, 2014 at 11:17

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.

&sharpinclude ''stm32f4_discovery.h''

&sharpinclude <stm32f4xx_usart.h>

&sharpdefine 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

USART_Cmd(USART2, ENABLE);

}

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

while(*s){

// wait until data register is empty

while( !(USARTx->SR & 0x00000040) );

USART_SendData(USARTx, *s);

*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;

cnt++;

}

else{ // otherwise reset the character counter and print the received string

cnt = 0;

USART_puts(USART2, received_string);

}

}

}

#uart #discovery #stm32f4
4 REPLIES 4
Posted on July 12, 2014 at 13:51

Doesn't look unreasonable, the *s++ should really be s++

You might reasonably expect one byte of line noise when the pin/usart are initialized. It's not clear from your complaint exactly what you're seeing.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andonmuhendislik
Associate II
Posted on July 12, 2014 at 16:34

the *s++ should really be s++

I'll make the change. but at the moment I do not have the opportunity to try

It's not clear from your complaint exactly what you're seeing.

Yes distress does not appear. I have sent from the PC to send data to the processor, but I can not get the data. I'm getting different characters. If it seems to have problems in communication speed. No problem in sending the communication speed but I want it I can see the data. 

for example; 

I'm sending ''abc'' from Computer , I wrote the program is sending me back the data received by the microprocessor and the ''øúñ'' is sending as different characters. Thus, the data is wrong and wrong is sending data.
Posted on July 12, 2014 at 19:33

You need to make sure you NUL terminate C Strings

Here's a simple USART2 Echo example

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/USART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=188]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FUSART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=188
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andonmuhendislik
Associate II
Posted on July 16, 2014 at 09:09

MAX232 integrates the TX leg is broken. I do not know how. I replace. Currently running. Thank you for your interest and relevance