cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 Usart receive strange problem

daniel239955_stm1
Associate II
Posted on May 25, 2015 at 16:45

Hi there,

I try to send data from PC to my stm32 board using RS232 but each time I send data to stm I get exactly same strange other data for sample: if i send 0x12 i receive 0xc4 always and if i send 0x2f i receive 0x3f , i dont understand what is buggy here this is my code for initialization

// USART Initialization
void enableUsart()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 |
RCC_APB2Periph_AFIO |
RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_StructInit (& GPIO_InitStruct);
// Initialize USART1_Tx
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStruct);
// Initialize USART1_RX
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA , &GPIO_InitStruct);
USART_InitTypeDef USART_InitStructure;
// Initialize USART structure
USART_StructInit (& USART_InitStructure);
// Modify USART_InitStructure for non -default values , e.g.
// USART_InitStructure.USART_BaudRate = 38400;
USART_InitStructure.USART_BaudRate = 19200;
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);
USART_Cmd(USART1 , ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART1 global interrupt */
NVIC_EnableIRQ(USART1_IRQn);
// NVIC_InitTypeDef NVIC_InitStructure;
// // No StructInit call in API
// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init (& NVIC_InitStructure);
}

and this is interrupt function

void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t data;
// buffer the data (or toss it if there 's no room
// Flow control will prevent this
data = USART_ReceiveData(USART1);
parseMessage(data); 
}
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
uint8_t data;
/* Write one byte to the transmit data register */
if (uq_dequeue (&UART1_TXq, &data)){
USART_SendData(USART1, data);
} else {
// if we have nothing to send , disable the interrupt
// and wait for a kick
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
TxPrimed = 0;
}
}
}

thanks in advance
4 REPLIES 4
Posted on May 25, 2015 at 18:27

The STM32 does not accept RS232 levels, you'll need something like a

http://pdfserv.maximintegrated.com/en/ds/MAX3222-MAX3241.pdf

Other than that use a scope, send some data, verify the bit rates, loop back the transmit, etc.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
daniel239955_stm1
Associate II
Posted on May 25, 2015 at 18:51

thanks for quick reply!

i using max232 in my hardware like the attachment and using 10kO resistors on RX and TX lines for decrease noises like the attachment,

0690X00000604WpQAI.png

other than that everything looks OK to me, USART->SR has value 0xC0 too so maybe clock configuration is wrong, i use a HSE clock with 12MHz crystal , how can i check?

Posted on May 25, 2015 at 20:41

Here we use oscilloscopes. You can measure internal frequencies via the MCO (PA8) and timer pins. For the USART you'd send a stream of characters, 'U' (0x55) being very effective, and measure the bit timings.

For a different HSE clock you need to make sure the HSE_VALUE defined within your project reflects the clock being supplied, otherwise the math to compute the baud rate will be wrong. A quick test of this would be setting 9600 baud as ((9600 * 8) / 12)

The MAX232 device is rated for 5V operation, not 3.0-3.3V

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
daniel239955_stm1
Associate II
Posted on May 26, 2015 at 06:54

The MAX232 device is rated for 5V operation, not 3.0-3.3V

   I m using MAX3232.

HSE_VALUE is 12MHz , i set PCLK2 equal to HCLK and HCLK = SYSCLK that is PLLCLK = HSE * 6 , so my PCLK2 is equal to 72 MHz