Skip to main content
hospodar
Associate III
November 20, 2012
Question

UART example code for STM32F0

  • November 20, 2012
  • 60 replies
  • 11869 views
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
    This topic has been closed for replies.

    60 replies

    vincentos87
    Visitor II
    August 27, 2014
    Posted on August 27, 2014 at 12:05

    I'm trying to connect STM32F0 with the pc using the following converter :

    http://www.amazon.it/gp/product/B00AFRXKFU/ref=pe_115971_53225081_em_1p_0_ti

    I'm not sure if is good for this purpose but for now it not work. I connected the pin in the following way:

    http://itinypic.com/osra1d.jpg

    Here is the code used:

    
    #include ''stm32f0xx.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'
    );}
    
    }

    I used different programs to read data from the micro: Tera Term Realterm Putty terminal v19b
    Tesla DeLorean
    Guru
    August 27, 2014
    Posted on August 27, 2014 at 12:40

    You have the cable sense backward, switch the black and white cables at the USB serial dongle.

    The TX (OUT) of the STM32 must go to the RX (IN) of the USB serial dongle.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    vincentos87
    Visitor II
    August 27, 2014
    Posted on August 27, 2014 at 12:50

    I made a mistake big mistake, sorry.

    Now it works perfectly. 

    Thank you very much.

    kevin239955_stm1
    Visitor II
    December 12, 2014
    Posted on December 12, 2014 at 13:55

    Hello sir,I too have been trying to communicate using USART2 on STM32f107VC,but it seems as if I get some garbage values on hyterminal.Can you help me with it.

      

    void USART_Configuration(void)

    { 

      GPIO_InitTypeDef GPIO_InitStructure;

      USART_InitTypeDef USART_InitStructure; 

    //  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB1Periph_USART2,ENABLE);

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);

     

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

      /*

      *  USART1_TX -> PA2 , USART1_RX -> PA3

      */

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;         

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

      GPIO_Init(GPIOA, &GPIO_InitStructure);   

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;        

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

      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_Rx | USART_Mode_Tx;

      USART_Init(USART2, &USART_InitStructure); 

     // USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    //  USART_ITConfig(USART2, USART_IT_TXE, ENABLE);

      USART_Cmd(USART2, ENABLE);

    }

    int main(void)

    {

    NVIC_Configuration();

           USART_Configuration();

    printf(''*  Thank you for using HY-MiniSTM32V Development Board ! ^_^  *\r\n'');

        /* Infinite loop */

        while (1)

    {

    /* Loop until RXNE = 1 */

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

    USART_SendData(USART2,'x');

     

        }

    }

    Tesla DeLorean
    Guru
    December 12, 2014
    Posted on December 12, 2014 at 14:10

    Then you don't really have an STM32F0 device then do you? Please if you're off topic start a new thread.

    You need to be waiting on TXE (Transmit Empty) for outbound characters, not RXNE (Receive Not Empty). Electrically the USART pins are CMOS levels, not RS232 levels which would require a level converter chip.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    hospodar
    hospodarAuthor
    Associate III
    April 23, 2015
    Posted on April 23, 2015 at 22:29

    to be migrated, sourceId: 13782:74F499D6-C293-4561-BFB5-4F1489999957

    hospodar
    hospodarAuthor
    Associate III
    April 23, 2015
    Posted on April 23, 2015 at 22:32

    to be migrated, sourceId: 13783:74F499D6-C293-4561-BFB5-4F1489999957

    Tesla DeLorean
    Guru
    April 23, 2015
    Posted on April 23, 2015 at 22:48

    Confusion here with pointers, you have a variable, it isn't moving, you pass a pointer to the function.

    int k;
    uint8_t rx;
    for (k = 0; k < 8; k++) { // 8 bytes
    fifo_read(RxFifo, &rx, 1);
    rx_packet[k] = rx;
    }
    int k;
    for (k = 0; k < 8; k++) { // 8 bytes
    fifo_read(RxFifo, &rx_packet[k] , 1);
    }
    fifo_read(RxFifo, rx_packet, 8); // Read it once?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    hospodar
    hospodarAuthor
    Associate III
    May 7, 2015
    Posted on May 07, 2015 at 19:38

    How to make fifo_t structure and read, write functions acessible from more .c files? I tried to do this with header file, but when I put structure definition into this header I obtain error: typedef redefinition with different types (''struct fifo_t'' vs. ''struct fifo_t'').

    What this mean?

    Tesla DeLorean
    Guru
    May 7, 2015
    Posted on May 07, 2015 at 20:03

    Well it means you've got it specified twice, right?

    Put the typedef and prototype definitions in the .H file

    Include the .H file once in each .C file

    Don't define the typedef in the .C file, just the body code for the functions you have prototyped.

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