cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 USART COMMUNICATION

xtian
Associate II
Posted on February 06, 2013 at 10:06

Hi Guyz,

it doesnot want to output, I have checked RCC and all initialization.. what am I still Missing?


/**

******************************************************************************

* @file IO_Toggle/main.c 

* @author MCD Application Team

* @version V1.0.0

* @date 19-September-2011

* @brief Main program body

******************************************************************************

* @attention

*

* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE

* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY

* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING

* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE

* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

*

* <
h2
><
center
>© COPYRIGHT 2011 STMicroelectronics</
center
></
h2
>

****************************************************************************** 

*/ 


/* Includes ------------------------------------------------------------------*/

#include ''stm32f4_discovery.h''



GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;


void Delay(__IO uint32_t nCount);

void resetDataAndCounter(void);

void SendData(void);


int datas[40];

int DIOData = 0;

int ctr=0;


int main(void)

{

/* GPIOD Periph clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 


/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOD, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStructure); 


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

USART_Init(USART2, &USART_InitStructure); 

USART_Cmd(USART2, ENABLE);




GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 

GPIO_Init(GPIOC, &GPIO_InitStructure); 


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 ; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

GPIO_Init(GPIOA, &GPIO_InitStructure); 


resetDataAndCounter();

while (1)

{ 

USART_SendData(USART2,65);

if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)==1)

{

GPIOD->BSRRL = GPIO_Pin_12;

DIOData = 255 - GPIOC ->IDR>>0 & 0x00FF;

if ((DIOData > 40)&&(DIOData <=96))

{

datas[ctr] = DIOData;

ctr++;

}

if (ctr ==40)

{

SendData();

ctr = 0;

}

}

else

{

//GPIOD->BSRRH = GPIO_Pin_12;

}

}

}


void resetDataAndCounter(void)

{

ctr = 0;

for (int ctr1 = 0; ctr1 <40; ctr1++)

{

datas[ctr1] = 0;

}

}


void SendData(void)

{

for (int ctr1 = 0; ctr1<30; ctr1++)

{

USART_SendData(USART2, datas[ctr1]);

for (int ctr2=0; ctr2<=70024; ctr2++);

}

USART_SendData(USART2,44);

}


/**

* @brief Delay Function.

* @param nCount:specifies the Delay time length.

* @retval None

*/

void Delay(__IO uint32_t nCount)

{

while(nCount--)

{

}

}


#ifdef USE_FULL_ASSERT


/**

* @brief Reports the name of the source file and the source line number

* where the assert_param error has occurred.

* @param file: pointer to the source file name

* @param line: assert_param error line source number

* @retval None

*/

void assert_failed(uint8_t* file, uint32_t line)

{ 

/* User can add his own implementation to report the file name and line number,

ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */


/* Infinite loop */

while (1)

{

}

}

#endif


/**

* @}

*/ 


/**

* @}

*/ 


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

4 REPLIES 4
Posted on February 06, 2013 at 10:26

Your code gives me headaches...

Ok, you're not setting up the GPIO pins right for the USART, presumably you want PA2 for the output, it needs to be in AF mode, and you need to route the AF to the right pin source. Then you need to make sure you don't reconfigure the GPIO to some other function later.

For an example of USART2 on the F4 see

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/DispForm.aspx?ID=2606&RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/How%20to%20use%20the%20fpu%20in%20stm32f4xx&Source=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder%3D%252Fpubl...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xtian
Associate II
Posted on February 06, 2013 at 10:46

Sorry about that clive... I am a bit excited that I failed to clean my code...

so usart2 is in PD5... I Tried using PD5 and still failed where Am I Still wrong?


#include ''stm32f4_discovery.h''



GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;


void Delay(__IO uint32_t nCount);

void InitializeClocks(void);

void InitializeInputs(void);

void InitializeOutputs(void);

void resetDataAndCounter(void);

void SendData(void);


int datas[40];

int DIOData = 0;

int ctr=0;


int main(void)

{


InitializeInputs();

InitializeOutputs();

InitializeClocks();

resetDataAndCounter();


while (1)

{ 

USART_SendData(USART2,65);//this is just a test if my usart is working

if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)==1)

{

GPIOD->BSRRL = GPIO_Pin_12;

DIOData = 255 - GPIOC ->IDR>>0 & 0x00FF;

if ((DIOData > 40)&&(DIOData <=96))

{

datas[ctr] = DIOData;

ctr++;

}

if (ctr ==40)

{

SendData();

ctr = 0;

}

}

else

{

//GPIOD->BSRRH = GPIO_Pin_12;

}

}

}


void resetDataAndCounter(void)

{

ctr = 0;

for (int ctr1 = 0; ctr1 <40; ctr1++)

{

datas[ctr1] = 0;

}

}


void InitializeOutputs(void)

{

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 

GPIO_Init(GPIOC, &GPIO_InitStructure); 


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOD, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOD, &GPIO_InitStructure);


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

USART_Init(USART2, &USART_InitStructure); 

USART_Cmd(USART2, ENABLE);

}


void SendData(void)

{

for (int ctr1 = 0; ctr1<30; ctr1++)

{

USART_SendData(USART2, datas[ctr1]);

for (int ctr2=0; ctr2<=70024; ctr2++);

}

USART_SendData(USART2,44);

}

void InitializeClocks(void)

{

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 

}


void InitializeInputs(void)

{

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 ; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

GPIO_Init(GPIOC, &GPIO_InitStructure);

}


void Delay(__IO uint32_t nCount)

{

while(nCount--)

{

}

}


#ifdef USE_FULL_ASSERT


void assert_failed(uint8_t* file, uint32_t line)

{ 


while (1)

{

}

}

#endif

Posted on February 06, 2013 at 11:31

You have to configure the pin correctly, like the example I pointed too. Here explicitly for PD5.

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); // USART2_TX

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xtian
Associate II
Posted on February 06, 2013 at 11:53

Thanks clive... I See... it has a different approach from STM32VL...I'll try this out