Skip to main content
aybala5
Associate III
October 6, 2014
Question

STM32 sends weird characters to COM ???

  • October 6, 2014
  • 14 replies
  • 4254 views
Posted on October 06, 2014 at 17:36

Hello,

I send a char string to com port using sym32f4( with 3 pins only: GND VCC TX) and listen using ''hercules'' program finded on the net. I get weird characters, took a screanshot and uploaded as attachment. Can anyone help me, thnks.
    This topic has been closed for replies.

    14 replies

    megahercas6
    Associate III
    October 6, 2014
    Posted on October 06, 2014 at 17:45

    Please set correct HSE values inside files;

    stm32f4xx.h

    #if !defined (HSE_VALUE) 
    #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
    #endif /* HSE_VALUE */

    and also in system_config.c #define PLL_M 8
    Tesla DeLorean
    Guru
    October 6, 2014
    Posted on October 06, 2014 at 17:56

    If not the PLL/HSE settings (EVAL boards use 25 MHz, DISCO boards use 8 MHz), then look at how you're connecting the USART, it uses CMOS levels, not RS232 levels. Review timing issues using a scope.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    aybala5
    aybala5Author
    Associate III
    October 6, 2014
    Posted on October 06, 2014 at 18:11

    Hello Mr. karpavicius,

    First thanks for the response. I have no system_config.c file, i made the first change in

    stm32f4xx.h  from 25 to 8 but it didnt worked. I use stm32f4 discvory board send from PA2 (TX).

    aybala5
    aybala5Author
    Associate III
    October 6, 2014
    Posted on October 06, 2014 at 18:15

    #include ''stm32f4xx.h''
    #include ''MPU6h''
    #include <
    stdio.h
    >
    void USART_puts(USART_TypeDef* USARTx, volatile char *s){
    while(*s){
    // wait until data register is empty
    while( !(USARTx->SR & 0x00000040) );
    USART_SendData(USARTx, *s);
    *s++;
    }
    }
    void init_usart(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    /* enable peripheral clock for USART2 */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    /* GPIOA clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    /* GPIOA Configuration: USART2 TX on PA2 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    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);
    /* Connect USART2 pins to AF2 */
    // TX = PA2
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
    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_Tx;
    USART_Init(USART2, &USART_InitStructure);
    USART_Cmd(USART2, ENABLE); // enable USART2
    }
    void Delay(__IO uint32_t nCount)
    {
    while(nCount--)
    {
    }
    }
    int main(void)
    {
    SystemInit();
    init_usart();
    MPU6050_I2C_Init();
    MPU6050_Initialize();
    if( MPU6050_TestConnection()== SUCCESS);
    {
    // connection success
    }
    /*
    * <
    pre
    >
    * 0 = +/- 250 degrees/sec
    * 1 = +/- 500 degrees/sec
    * 2 = +/- 1000 degrees/sec
    * 3 = +/- 2000 degrees/sec
    * </
    pre
    >
    */
    MPU6050_SetFullScaleGyroRange(3);
    while (1)
    { 
    s16 AccelGyro[6]={0};
    MPU6050_GetRawAccelGyro(AccelGyro);
    s16 AccX;
    s16 AccY;
    s16 AccZ;
    s16 GyrX;
    s16 GyrY;
    s16 GyrZ;
    AccX=AccelGyro[0];
    AccY=AccelGyro[1];
    AccZ=AccelGyro[2];
    GyrX=AccelGyro[3];
    GyrY=AccelGyro[4];
    GyrZ=AccelGyro[5];
    int a;
    //a=0x0008FFFF;
    char str[50];
    sprintf(str,''GX%dGY%dGZ%dAX%dAY%dAZ%dS
    
    '',GyrX,GyrY,GyrZ,AccX,AccY,AccZ);
    USART_puts(USART2,str);
    //while(a--)
    //{ 
    //}
    }
    }

    Mr Clive here is the code. Values come i monitor in watch1 but cannot send in corect way. I have no scope by the way :( What tests should i do ? Thanks.
    megahercas6
    Associate III
    October 6, 2014
    Posted on October 06, 2014 at 18:23

    can you make buffer yourself, fill it by hand, and send it ?

    sometimes sprintf can do some fun stuff

    also why you don't have system_config.c file, do you made your own file for setting up registers, pll and so on ? if no, just add that file from discovery projects that can be downloaded from st.com

    one fun fact, sometimes changing optimization helps,

    Tesla DeLorean
    Guru
    October 6, 2014
    Posted on October 06, 2014 at 18:55

    also why you don't have system_config.c file, do you made your own file for setting up registers, pll and so on ? if no, just add that file from discovery projects that can be downloaded from st.com

     

    system_stm32f4xx.c ?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    aybala5
    aybala5Author
    Associate III
    October 6, 2014
    Posted on October 06, 2014 at 18:57

    I tested  USART_SendData(UART4, 0x49); // Send 'I' but i got [.

    I used default libraries for stm32f4 but there is no  system_config.c instead there is

    system_stm32f4xx.c file. Whatever i changed the PLL_M =8 and HSE= 8Mhz but it didnt worked :(

    aybala5
    aybala5Author
    Associate III
    October 6, 2014
    Posted on October 06, 2014 at 19:00

    Yes Mr clive i have system_stm32f4xx.c. I saw one of your share code:

    https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FSTM32f4%20rs232%20IAR%20IDE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=556

    // STM32 UART4 (Tx PC.10, Rx PC.11) STM32F4 Discovery - sourcer32@gmail.com
    // PC11 technically conflicts with SCLK on CS43L22
    #include ''stm32f4_discovery.h''

    ......

    USART_SendData(UART4, 0x49); // Send 'I'

    .... I got '[' instead of 'I'. Dont know why. Is your example codes for stm32f4 Discv. Board ? If so can you share full code with config or just system_stm32f4xx file. So i can work with true config. Thanks.
    Tesla DeLorean
    Guru
    October 6, 2014
    Posted on October 06, 2014 at 19:17

    C:\Keil\ARM\Examples\ST\STM32F4xx_DSP_StdPeriph_Lib_V1.4.0\Project\F4DISCO_USART2HELLO

    See attached project

    /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Attachments/46669/F4DISCO_USART2HELLO.zip

    // STM32F4-DISCO USART2 - sourcer32@gmail.com
    #include ''stm32f4xx.h''
    #include <
    stdio.h
    >
    //****************************************************************************
    void RCC_Configuration(void)
    {
    /* --------------------------- System Clocks Configuration -----------------*/
    /* USART2 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    /* GPIOA clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    }
    //****************************************************************************
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /*-------------------------- GPIO Configuration ----------------------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
    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_2MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* Connect USART pins to AF */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // USART2_TX
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // USART2_RX
    }
    //****************************************************************************
    void USART2_Configuration(void)
    {
    USART_InitTypeDef USART_InitStructure;
    /* USARTx configuration ------------------------------------------------------*/
    /* USARTx configured as follow:
    - BaudRate = 9600 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 = 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);
    }
    //****************************************************************************
    int main(void)
    {
    RCC_Configuration();
    GPIO_Configuration();
    USART2_Configuration();
    puts(''Hello World!'');
    while(1); // Don't want to exit
    }
    //****************************************************************************
    #include <
    rt_misc.h
    >
    #pragma import(__use_no_semihosting_swi)
    struct __FILE { int handle; /* Add whatever you need here */ };
    FILE __stdout;
    FILE __stdin;
    int fputc(int ch, FILE *f)
    {
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
    USART_SendData(USART2, ch);
    return(ch);
    }
    int fgetc(FILE *f)
    {
    char ch;
    while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
    ch = USART_ReceiveData(USART2);
    return((int)ch);
    }
    int ferror(FILE *f)
    {
    /* Your implementation of ferror */
    return EOF;
    }
    void _ttywrch(int ch)
    {
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
    USART_SendData(USART2, ch);
    }
    void _sys_exit(int return_code)
    {
    label: goto label; /* endless loop */
    }
    //****************************************************************************
    #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
    //****************************************************************************

    ________________

    Attachments :

    F4DISCO_USART2HELLO.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0vY&d=%2Fa%2F0X0000000bfk%2FyPTGIprn8KKkg_Wrd8jL_CDmYd3sgVTUmcOMsFpwWjY&asPdf=false
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    October 6, 2014
    Posted on October 06, 2014 at 19:23

    Is your example codes for stm32f4 Discv. Board ?

    It says so doesn't it? Assumes

    http://www.st.com/web/en/catalog/tools/PF257904

    is being used, and you have a suitable serial connection wired up.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..