Skip to main content
raj2
Associate II
March 5, 2013
Question

usart code

  • March 5, 2013
  • 4 replies
  • 989 views
Posted on March 05, 2013 at 13:01

i need code to send a char to PC by  USART1  using stm32f3 discovery kit .

please help..

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    March 5, 2013
    Posted on March 05, 2013 at 14:18

    This should be workable

    // STM32 USART1 (Tx PA.9, Rx PA.10) STM32F3-Discovery - sourcer32@gmail.com
    #include ''stm32f30x.h''
    /**************************************************************************************/
    void RCC_Configuration(void)
    {
    /* Enable GPIO clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    /* Enable USART clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    }
    /**************************************************************************************/
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /* Connect PA9 to USART1_Tx */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
    /* Connect PA10 to USART1_Rx */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
    /* Configure USART Tx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* Configure USART Rx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    }
    /**************************************************************************************/
    void USART1_Configuration(void)
    {
    USART_InitTypeDef USART_InitStructure;
    /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
    /* USART configured as follow:
    - BaudRate = 115200 baud
    - Word Length = 8 Bits
    - One Stop Bit
    - No parity
    - Hardware flow control disabled (RTS and CTS signals)
    - Receive and transmit enabled
    */
    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 configuration */
    USART_Init(USART1, &USART_InitStructure);
    /* Enable USART */
    USART_Cmd(USART1, ENABLE);
    }
    /**************************************************************************************/
    int main(void)
    {
    RCC_Configuration();
    GPIO_Configuration();
    USART1_Configuration();
    while(1)
    {
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
    USART_SendData(USART1, 0x49); // Send 'I'
    }
    while(1); // Don't want to exit
    }
    /**************************************************************************************/
    #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
    
    '', file, line) */
    /* Infinite loop */
    while (1)
    {
    }
    }
    #endif

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    raj2
    raj2Author
    Associate II
    March 26, 2013
    Posted on March 26, 2013 at 10:19

    hello clive 1 ,

    thanks for code . its is working .

    but i have a small problem to set the clock of usart1 . 

    in this function it  set the clock

    USART_Init(USART1, &USART_InitStructure);

    but aslo it call another function  '' RCC_GetClocksFreq(&RCC_ClocksStatus);''

    this is little bit confusion .

    i am using stm32f3 discovery kit which has no extenal crystal oscillator.

     

     what frequency is given to usart?? how  can i find it ??

    thanks 

    Tesla DeLorean
    Guru
    March 26, 2013
    Posted on March 26, 2013 at 16:28

    Well the routine determines the clock source, bus divisors, and PLL settings, and uses that to compute the clock into the USART, and the baud rate divisor to use.

    Also be aware that the STM32F3-Discovery board derives it's HSE clock via the 8 MHz crystal used by the STM32F103 ST-LINK part. This clock outputs via the MCO pin, and goes to the input of the F3 via SB12. This acts like an external clock input rather than using a crystal oscillator input/output pair. Refer to the schematic for your board for additional connectivity information.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    levantoan2029
    Associate II
    January 19, 2015
    Posted on January 19, 2015 at 04:42

    Hi clive1

    I want to comunicate Stm32f3 with PC by mini USB integrated on Kit. Could you tell me how to use?

    thanh you very much!