cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 USART

nakulrao
Associate II
Posted on September 30, 2012 at 03:24

I am using the following code to transfer an array of 100 elements to the PC via UART. I am not able to understand what my mistake is. Can someone please point out my mistake. Here is the code

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

   /* Peripheral Clock Enable -------------------------------------------------*/

   /* Enable GPIO clock */

   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC , ENABLE);

   

   /* Enable USART clock */

   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

   /* Connect USART pins to AF7 */

   

   GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);

   GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);

   

   /* Configure USART Tx and Rx as alternate function push-pull */

   

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

   

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

   GPIO_Init(GPIOC, &GPIO_InitStructure);

   

   USART_InitStructure.USART_BaudRate = 9600;

   USART_InitStructure.USART_WordLength = USART_WordLength_8b;

   USART_InitStructure.USART_StopBits = USART_StopBits_1;

   /* When using Parity the word length must be configured to 9 bits */

   USART_InitStructure.USART_Parity = USART_Parity_No;

   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

   USART_InitStructure.USART_Mode = USART_Mode_Tx;

   USART_Init(USART3, &USART_InitStructure);

   

   USART_ClearFlag(USART3, USART_FLAG_TC);

   

   /* Enable USART */

   USART_Cmd(USART3, ENABLE);

   SRAM_ReadBuffer(RxBuffer, 0x00, 100);

  for(i=0;i<100;i++)

  {

                USART_SendData(USART3, RxBuffer[i]);

 

  Delay(1000);

***  while(USART_GetFlagStatus(USART3,USART_FLAG_TC) == RESET)

  {}

  USART_ClearFlag(USART3, USART_FLAG_TC);

  }

The line marked as ***. I have tried it with both set and reset but to no avail.

2 REPLIES 2
Posted on September 30, 2012 at 14:43

USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Peripheral Clock Enable -------------------------------------------------*/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC , ENABLE);
/* Enable USART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Connect USART pins to AF7 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
/* When using Parity the word length must be configured to 9 bits */
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART3, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
SRAM_ReadBuffer(RxBuffer, 0x00, 100);
for(i=0;i<100;i++)
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET);
USART_SendData(USART3, RxBuffer[i]);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
nakulrao
Associate II
Posted on October 03, 2012 at 16:33

Thanks a lot..works perfectly now...i really admire the way you have solved everyones problems in this forum..great...thanks again