2016-06-08 02:45 AM
Hi guys,
I've implemented the code to send data from STM32f103 to PC with USART interface, but the data that I receive are wrong. This is the code that I've implemented :void
RCC_Configuration(
void
)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig( RCC_HSE_ON );
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if
( HSEStartUpStatus == SUCCESS )
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable );
/* Flash 2 wait state */
FLASH_SetLatency( FLASH_Latency_2 );
/* HCLK = SYSCLK */
RCC_HCLKConfig( RCC_SYSCLK_Div1 );
//Define AHB prescale @72Mhz
/* PCLK2 = HCLK */
RCC_PCLK2Config( RCC_HCLK_Div1 );
//Define APB2 prescale @72Mhz
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config( RCC_HCLK_Div2 );
//Define APB1 prescale @36Mhz
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig( RCC_PCLK2_Div6 );
//Max 14 Mhz now @12Mhz
/* PLLCLK = 8MHz*9 = 72MHz */
RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
/*Set USB clock @48Mhz*/
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
/* Enable PLL */
RCC_PLLCmd( ENABLE );
/* Wait till PLL is ready */
while
( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET )
{;}
/* Select PLL as system clock source */
RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
/* Wait till PLL is used as system clock source */
while
( RCC_GetSYSCLKSource() != 0x08 )
{;}
}
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable SPI1 clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1 , ENABLE );
/* Enable GPIOs clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
}
void
USART_init(
void
)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
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;
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
}
void
USART_putc(
char
c)
{
/* e.g. write a character to the USART */
USART_SendData(USART1, c);
/* Loop until the end of transmission */
while
(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ;
}
void
USART_puts(
const
char
*s)
{
int
i;
for
(i=0; s[i]!=0; i++)
{
USART_putc(s[i]);
}
}
void
printf1(
const
char
*format, ...)
{
va_list list;
va_start(list, format);
int
len = vsnprintf(0, 0, format, list);
char
*s;
s = (
char
*)malloc(len + 1);
vsprintf(s, format, list);
USART_puts(s);
free(s);
va_end(list);
return
;
}
But if I try to send ''Hello world\n'', I receive this:
I use RS232USB converte with three cables for connect the STM32F103 to PC. Some suggestion?
P.S. With another code (I have only the .hex) the communication works fine.
2016-06-08 02:54 AM
Hi verdano.gianmichele,
I suggest you to try the UART_Printf example under the STM32F1 cube firmware package it may be helpful.STM32Cube_FW_F1_V1.4.0\Projects\STM3210E_EVAL\Examples\UART\UART_Printf-Syrine-2016-06-08 03:47 AM
Its pretty apparent the OP isn't using Cube
Unfortunately the code provided is incomplete, try to provide some complete context. You are using a specific board, cite some links or names for that. You specify you have a working .hex, where did that come from? Provides a cite. Perhaps the board doesn't use an 8 MHz crystal and thus the timing assumptions are off? I'd front check for TXE rather than back check for TC. Do you have a debugger? Can you use that?2016-06-08 05:08 AM
2016-06-08 05:30 AM
Are the baud rate settings identical (start/stop bits, parity) ?
A scope is recommended, to check that the outgoing signals actually have the assumed baud rate.> Perhaps the board doesn't use an 8 MHz crystal and thus the timing assumptions are off?That is an often recurring reason for the baud rate being off.
2016-06-08 05:34 AM
2016-06-08 05:42 AM
The system clock is conveyed via HSE_VALUE define, the PLL settings will also need to change. Some debuggers can provide a peripheral view showing configuration settings.
One could also send a stream of 'U' characters and confirm baud timing with an oscilloscope. Internal clocking could also be check by programming PA8 in MCO mode.2016-06-08 05:43 AM
2016-06-08 05:57 AM
2016-06-08 06:21 AM
I've modified the code in this manner and for me the oscillator is an 8Mhz. Other ideas?
void
RCC_Configuration(
void
)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig( RCC_HSE_ON );
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if
( HSEStartUpStatus == SUCCESS )
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable );
/* Flash 2 wait state */
FLASH_SetLatency( FLASH_Latency_2 );
/* HCLK = SYSCLK */
RCC_HCLKConfig( RCC_SYSCLK_Div1 );
//Define AHB prescale @72Mhz
/* PCLK2 = HCLK */
RCC_PCLK2Config( RCC_HCLK_Div1 );
//Define APB2 prescale @72Mhz
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config( RCC_HCLK_Div2 );
//Define APB1 prescale @36Mhz
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig( RCC_PCLK2_Div6 );
//Max 14 Mhz now @12Mhz
/* PLLCLK = 8MHz*9 = 72MHz */
//RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
/*Set USB clock @48Mhz*/
//RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
/* Enable PLL */
RCC_PLLCmd( ENABLE );
/* Wait till PLL is ready */
while
( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET )
{;}
/* Select PLL as system clock source */
RCC_SYSCLKConfig( RCC_SYSCLKSource_HSE );
/* Wait till PLL is used as system clock source */
//while ( RCC_GetSYSCLKSource() != 0x08 )
// {;}
}
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable SPI1 clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1 , ENABLE );
/* Enable GPIOs clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
}