Skip to main content
achourwissem
Associate III
June 2, 2013
Question

STM32f4 rs232 IAR IDE

  • June 2, 2013
  • 20 replies
  • 2345 views
Posted on June 02, 2013 at 13:34

hello :)

i had a problem  when i send a character or receive a character , i always get a wrong character like ''-'' or a heart ...

ps: i'm using rs232 -> USB converter

here is my code:

&sharpinclude ''stm32f4xx.h''

void main()

{

 //UART4 initialization

RCC->APB1ENR |= 0x00080000; // Enable clock for UART4

RCC->AHB1ENR |= 0x00000004; // Enable clock for GPIOC

GPIOC->MODER |= 0x00a00000; // PC10, PC11 => AF mode

GPIOC->AFR[1] |= 0x00008800; // select AF8 (UART4,5,6) for PA10, PA11

UART4->BRR = 0x1120; // 9600 baud

UART4->CR1 |= 0x200c; // Enable UART for TX, RX

UART4->CR1 |= 0x0020; // Enable RX interrupt

while(1){

  if (UART4->SR & 0x0080) {

   UART4->DR='a';

   for (int i=0; i<10000000; i++) {};

  }

}

}

please any help! :(

#too-vague
    This topic has been closed for replies.

    20 replies

    Tesla DeLorean
    Guru
    June 2, 2013
    Posted on June 02, 2013 at 16:23

    please any help!

    With register level code you really own your own problems. The baud rate value looks wrong, For a chip running at 168 MHz, and APB1 at 42 MHz, 0x1117 would be better for actually getting 9600 baud. Why would you enable a receive interrupt?

    // STM32 UART4 (Tx PC.10, Rx PC.11) STM32F4 Discovery - sourcer32@gmail.com
    // PC11 technically conflicts with SCLK on CS43L22
    #include ''stm32f4_discovery.h''
    /**************************************************************************************/
    void RCC_Configuration(void)
    {
    /* --------------------------- System Clocks Configuration -----------------*/
    /* UART4 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
    /* GPIOC clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    }
    /**************************************************************************************/
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /*-------------------------- GPIO Configuration ----------------------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
    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(GPIOC, &GPIO_InitStructure);
    /* Connect USART pins to AF */
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
    }
    /**************************************************************************************/
    void UART4_Configuration(void)
    {
    USART_InitTypeDef USART_InitStructure;
    /* USARTx configuration ------------------------------------------------------*/
    /* USARTx 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 = 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(UART4, &USART_InitStructure);
    USART_Cmd(UART4, ENABLE);
    }
    /**************************************************************************************/
    int main(void)
    {
    RCC_Configuration();
    GPIO_Configuration();
    UART4_Configuration();
    while(1)
    {
    while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET); // Wait for Empty
    USART_SendData(UART4, 0x49); // Send 'I'
    }
    while(1); // Don't want to exit
    }

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    achourwissem
    Associate III
    June 2, 2013
    Posted on June 02, 2013 at 21:17

    thnx for your reply! :)

    but it still didnt work for me :\ even i changed the baud rate to many values

    Tesla DeLorean
    Guru
    June 3, 2013
    Posted on June 03, 2013 at 04:41

    In that case you'll need to pay particular attention to the crystal on the board, the define for HSE_VALUE, and the PLL settings in system_stm32f4xx.c. Most notably, the STM32F4-Discovery board uses an 8 MHz crystal, and most of ST's other offerings use a 25 MHz crystal. Failing to deal with this will result in baud rates being wrong, I highly recommend sending characters repetitively and measuring the bit time with a scope.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    achourwissem
    Associate III
    June 3, 2013
    Posted on June 03, 2013 at 23:30

    thnx clive for urr answer :) this is my config in system_stm32f4xx.c and plz tell me what can i do

     This file configures the system clock as follows:

      *=============================================================================

      *=============================================================================

      *        Supported STM32F4xx device revision    | Rev A

      *-----------------------------------------------------------------------------

      *        System Clock source                    | PLL (HSE)

      *-----------------------------------------------------------------------------

      *        SYSCLK(Hz)                             | 168000000

      *-----------------------------------------------------------------------------

      *        HCLK(Hz)                               | 168000000

      *-----------------------------------------------------------------------------

      *        AHB Prescaler                          | 1

      *-----------------------------------------------------------------------------

      *        APB1 Prescaler                         | 4

      *-----------------------------------------------------------------------------

      *        APB2 Prescaler                         | 2

      *-----------------------------------------------------------------------------

      *        HSE Frequency(Hz)                      | 25000000

      *-----------------------------------------------------------------------------

      *        PLL_M                                  | 25

      *-----------------------------------------------------------------------------

      *        PLL_N                                  | 336

      *-----------------------------------------------------------------------------

      *        PLL_P                                  | 2

      *-----------------------------------------------------------------------------

      *        PLL_Q                                  | 7

      *-----------------------------------------------------------------------------

      *        PLLI2S_N                               | NA

      *-----------------------------------------------------------------------------

      *        PLLI2S_R                               | NA

      *-----------------------------------------------------------------------------

      *        I2S input clock                        | NA

      *-----------------------------------------------------------------------------

      *        VDD(V)                                 | 3.3

      *-----------------------------------------------------------------------------

      *        Main regulator output voltage          | Scale1 mode

      *-----------------------------------------------------------------------------

      *        Flash Latency(WS)                      | 5

      *-----------------------------------------------------------------------------

      *        Prefetch Buffer                        | OFF

      *-----------------------------------------------------------------------------

      *        Instruction cache                      | ON

      *-----------------------------------------------------------------------------

      *        Data cache                             | ON

      *-----------------------------------------------------------------------------

      *        Require 48MHz for USB OTG FS,          | Enabled

      *        SDIO and RNG clock                     |

      *-----------------------------------------------------------------------------
    Tesla DeLorean
    Guru
    June 3, 2013
    Posted on June 04, 2013 at 00:07

    Do you have an STM32F4-Discovery board? I've kind of assumed this, but you've not explicitly stated what you're using.

    If so you'll need to modify the PLL_M define to 8 in this file, and define HSE_VALUE as 8000000 within the project.

    In Keil it would be Options -> C/C++ -> Preprocessor Symbols -> Define

    Should be something like : STM32F4XX, USE_STDPERIPH_DRIVER, HSE_VALUE=8000000

    For IAR you'll need to read your manuals.

    Make sure it's not redefined in any other source files.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    achourwissem
    Associate III
    June 4, 2013
    Posted on June 04, 2013 at 13:08

    thnx! i think i'm near to the good results , because it changed a little and the characters are better but still wrong , i changed the PLL_M to 8 and HSE to 8Mhz , i think it still something missing :)

    should i change also the APB1 Prescaler or no ? and what is the new baud rate value i should put it ? 
    achourwissem
    Associate III
    June 4, 2013
    Posted on June 04, 2013 at 13:09

    and yeah i'm using STM32F4

    Tesla DeLorean
    Guru
    June 4, 2013
    Posted on June 04, 2013 at 13:34

    Have you checked the bit timing with a scope?

    Why would you need to change the APB settings? The PLL value being changed is the one that generates the 1 MHz comparison frequency internally. The other setting should get us to 168 MHz. The startup_stm32f4xx.s code calls the SystemInit() routine in system_stm32f4xx.c, and it is this code that should set up the pll and buses.

    You could also use timers, and MCO pins to confirm internal clock speeds with a scope.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    achourwissem
    Associate III
    June 4, 2013
    Posted on June 04, 2013 at 20:29

    anyway i dont have scope to test on it , i'm little confused what to do

    thnx anyway clive :)
    Tesla DeLorean
    Guru
    June 4, 2013
    Posted on June 04, 2013 at 21:24

    That does put you in a bit of a pickle.

    Also be conscious that the STM32 outputs CMOS levels, not RS232 compatible levels. You want to be using a USB-to-CMOS Serial type device where the STM32 connects directly to a chip from FTDI, Prolific or SiLabs. For RS232 interfacing a MAX3232 type level converter will be required.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..