cancel
Showing results for 
Search instead for 
Did you mean: 

UART example code for STM32F0

hospodar
Associate III
Posted on November 20, 2012 at 23:34

Hello,

I looking for some example code for communication between my ST-DISCOVERY F0 board and PC (UART). Can someone help me with it? I´m novice.

Thank you
60 REPLIES 60
Posted on November 21, 2012 at 05:35

Something like this will probably work

/* USART2 PA.2 Tx, PA.3 Rx STM32F0-Discovery sourcer32@gmail.com */
#include ''stm32f0xx.h''
#include ''stm32f0_discovery.h''
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
/* Configure USART2 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2,ENABLE);
while(1)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
USART_SendData(USART2, 'X');
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
frankmeyer9
Associate II
Posted on November 21, 2012 at 09:30

And if you really intend to connect the discovery board to the PC, there you find some easy to replicate level shifter circuitry.

http://www.acmesystems.it/106

Andrew Neil
Evangelist III
Posted on November 21, 2012 at 10:18

''if you really intend to connect the discovery board to the PC, there you find some easy to replicate level shifter circuitry''

Or use something like this for the connection to the PC:

http://www.ftdichip.com/Products/Cables/USBTTLSerial.htm

It connects direct to the microcontroller's

UART pins - be sure to choose the correct voltage version!

Or this:

0690X00000603FwQAI.jpg

http://www.ftdichip.com/Products/Cables/RPi.htm

Although it's described as a ''Raspberry Pi'' cable, it neither knows nor cares what you actually connect it to!

This one just provides Rx, Tx, and Ground. 3.3V only.

Posted on February 01, 2013 at 01:14

And for USART1, this should be workable for polling. Edit: fixed bug

/* USART1 PA.9 Tx, PA.10 Rx STM32F0-Discovery sourcer32@gmail.com */
#include ''stm32f0xx.h''
#include ''stm32f0_discovery.h''
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure USART1 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
while(1)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'X');
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gururaj
Associate
Posted on February 16, 2013 at 14:27

Posted on February 16, 2013 at 15:41

Looks like I missed a semi-colon in my wait of TXE loop.

You need to enable the interrupt on the USART

  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shammi88r
Associate II
Posted on March 08, 2013 at 12:50

Hello,

I have written the program for the Usart2 and i want to see the result on putty terminal but i am not able to see any kind of data on the putty terminal 

here is my source code 

int main (void)

{

   USART_InitTypeDef USART_InitStructure;

   RCC_Configurartion();

   GPIO_Configurartion();

   USART_InitStructure.USART_BaudRate = 115200;/*set the baud rate to 115200 */

   USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*set the data bit 8 */

   USART_InitStructure.USART_StopBits = USART_StopBits_1;/*set the stop bit 1 */

   USART_InitStructure.USART_Parity = USART_Parity_No;/*no parity */

   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/*No hardware flow control */

   USART_InitStructure.USART_Mode = USART_Mode_Rx |USART_Mode_Tx;/*send and receive */

   /*Configure the serial port */

   USART_Init (USART1, &USART_InitStructure);

   /*Configure the serial port */

   USART_Init (USART2, &USART_InitStructure);

   /*Enable serial port 1 */

   USART_Cmd (USART1, ENABLE);

   /*Enable the serial port */

   USART_Cmd (USART2, ENABLE);

   /*Wait until end of transmission from USART1 to USART2 */

 while (1)

  {

 USART_SendData (USART2, '1');

 while (USART_GetFlagStatus (USART2, USART_FLAG_TC)== RESET);

 USART_SendData (USART2, '5');

 while (USART_GetFlagStatus (USART2, USART_FLAG_TC)== RESET);

// if (USART_GetFlagStatus (USART1, USART_FLAG_RXNE)== SET)

   USART_SendData (USART2, USART_ReceiveData (USART1));

   while (USART_GetFlagStatus (USART1, USART_FLAG_TC)== RESET);

   Delay (0XFFFFF);

  }

}

void GPIO_Configurartion(void)

{

   GPIO_InitTypeDef GPIO_InitStructure;

//////Configure USART1 Rx as input Floating

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

//////Configure USART1 Tx as alternet function push pull

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

   GPIO_Init(GPIOA, &GPIO_InitStructure);

//////Configure USART2 Rx as input Floating

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_Speed  = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOD, &GPIO_InitStructure);

//////Configure USART2 Tx as alternet function push pull

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOD, &GPIO_InitStructure);

}

void RCC_Configurartion(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

}

Posted on March 08, 2013 at 15:28

Tags are great and all, but perhaps you should specify what part you're using, and start a new thread if it doesn't relate to this thread's topic??

RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // << APB2 SURELY?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on March 13, 2013 at 17:49

Have someone working program for demonstrating USART with interrupts for F0?