cancel
Showing results for 
Search instead for 
Did you mean: 

USART Send String Problem

damncm
Associate
Posted on May 30, 2014 at 09:57

Hi, I need your help...

I try to use the USART1 of the STM32F107 to send a string (''HELLO WORLD'') but on the hyperterminal, I receive just the first letter (''H''). I cannot found the fault, I use Coocox CoIDE with the GCC ARM compiler. Here's my code:

&sharpinclude ''stm32f10x.h''

&sharpinclude ''stm32f10x_gpio.h''

&sharpinclude ''stm32f10x_rcc.h''

&sharpinclude ''stm32f10x_flash.h''

&sharpinclude ''stm32f10x_usart.h''

&sharpinclude ''misc.h''

ErrorStatus HSEStartUpStatus;

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

void RCC_Configuration(void);

void GPIO_Configuration(void);

void NVIC_Configuration(void);

void USART_Configuration(void);

void USART_Send(USART_TypeDef*,volatile char*);

int main(void)

{

RCC_Configuration();

GPIO_Configuration();

USART_Configuration();

USART_Send(USART1,''HELLO WORLD'');

while(1)

{

}

return 0;

}

void RCC_Configuration(void)

{

RCC_DeInit();

RCC_HSEConfig(RCC_HSE_ON);

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)

{

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

FLASH_SetLatency(FLASH_Latency_2);

RCC_HCLKConfig(RCC_SYSCLK_Div1);

RCC_PCLK2Config(RCC_HCLK_Div1);

RCC_PCLK1Config(RCC_HCLK_Div2);

RCC_PREDIV2Config(RCC_PREDIV2_Div5);

RCC_PLL2Config(RCC_PLL2Mul_8);

RCC_PLL2Cmd(ENABLE);

while(RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET){}

RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2,RCC_PREDIV1_Div5);

RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_9);

RCC_PLLCmd(ENABLE);

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource() != 0x08){}

}

SysTick_Config(SystemCoreClock/1000);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

}

void GPIO_Configuration()

{

/* TX - PA9 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA,&GPIO_InitStructure);

/* RX - PA10 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA,&GPIO_InitStructure);

}

void USART_Configuration(void)

{

USART_InitStructure.USART_BaudRate = 9600;

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

USART_Init(USART1,&USART_InitStructure);

USART_Cmd(USART1,ENABLE);

}

void USART_Send(USART_TypeDef* USARTx,volatile char *str)

{

while(*str)

{

while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE) == RESET);

USART_SendData(USARTx,*str);

str++;

}

}

#send #usart #string
3 REPLIES 3
Posted on May 30, 2014 at 15:35

Doesn't look unreasonable, suggest you use your IDE's debugger, and step through the code a little to understand what might be going on. Put a scope on the pin, check bit timings, signal etc.

I wouldn't use volatile in this context, but it should be harmless.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
damncm
Associate
Posted on May 30, 2014 at 15:45

I found out that it works when I send the ''HELLO WORLD'' from an interrupt handler. Why do it works from an interrupt handler and not from the main ???

Posted on May 30, 2014 at 20:17

You'll have to work through it with your debugger, it could be that you tool chain is generating bad code, CooCox is known for not using the CMSIS model correctly. Try an eval version of Keil or IAR, do they also behave oddly?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..