AnsweredAssumed Answered

STM32F4 discovery (LQFP100 )

Question asked by les.artas on Jan 16, 2013
Latest reply on Jan 16, 2013 by John F.
I have a working code to receive and send one character (one char), but I need to receive a string in the form of numbers. Then convert it to double and count with these values.

Any ideas on how to receive a string or do something else?
Thank you very much for your advice.

My code:


#include "stm32f4_discovery.h"
#include <stm32f4xx.h>
#include <stm32f4xx_usart.h>
#include <stm32f4xx_gpio.h>

//////////////////
#include <stdlib.h>
#include <stdio.h>
#include<math.h>
//////////////////

//----------------Function-------------------//

    void inicializace(void);
    void conf_GPIO(void);
    void conf_USART2(void);
    void USART_puts(USART_TypeDef *USARTx, volatile char *z);
 
    void SWV_puts(const char *s ); /////// Print to console

//--------------------------------------------------//


void Delay(__IO uint32_t nCount)
{
    while(nCount--)
    {
    }
}

int main(void)
{

     inicializace();
     conf_GPIO();
     conf_USART2();

     int i=0;

     while(1)
     {
                    
         char array[100];


         while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
         array[i] = USART_ReceiveData(USART2);

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

         USART_SendData(USART2, array[i]);

          i++;
     }
 }
 
 
///////////////////I tried something like this//////////////////////
 /* 
  *I have array[i]= 1457; ... + '\0'
 * but the result is not good, because the string is not in itself all numbers

 char string[100];
  int i = 0;
  while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
  array[i]= USART_ReceiveData(USART2);

    i++;

     if(i==4)
     {
     memcpy(string, array, 4);
     string[5] = '\0';
     while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
         double D = atof(string);
       USART_SendData(USART2,D);
   
 */  
///////////////////////////////////////////////////////////////////   
   




//Settings USART - GPIO - Clock//
      //////////////////


void USART_puts(USART_TypeDef *USARTx, volatile char *z)
{
while(*z){

while(!(USARTx->SR &0x00000080));
USART_SendData(USARTx, *z);
z++;
         }
}

void inicializace(void)
{
    //------clock--------//
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    //----clock Direct Memory Access-----//
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
}


//NASTAVENI GPIO///
void conf_GPIO(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    //---Connect_USART2_PINS_TR_RX---//
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);


//conf TX
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
            GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
            GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;    //Pupd_UP (puvodne)
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_Init(GPIOA, &GPIO_InitStructure);


//conf RX
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
            GPIO_Init(GPIOA, &GPIO_InitStructure);



}
//NASTAVENI USARTU 2//
void conf_USART2(void)
{
        USART_InitTypeDef USART_InitStructure;
        USART_InitStructure.USART_BaudRate = 115200;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;                 //puv 2
        USART_InitStructure.USART_Parity = USART_Parity_No;    //Odd
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
        USART_Init(USART2, &USART_InitStructure);
        USART_Cmd(USART2, ENABLE);
}

/////////////////Funciton for print to console////////////////////

/**
  * @brief   This function sends strings to the serial wire viewer.
  * @param  *s: pointer to string to be displayed on SWV
  * @retval None
  */
void SWV_puts(const char *s )
{
    while (*s) ITM_SendChar(*s++);

}

/**
  * @brief   This function sends numbers to the serial wire viewer.
  * @param  number: number to be displayed on SWV
  * @retval None
  */
void SWV_printnum(long number)
{
 unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
 unsigned int i = 0;

 //if number is 0
 if (number == 0)
 {
  ITM_SendChar('0'); //if number is zero
     return;
 }
 //account for negative numbers
 if (number < 0)
 {
  ITM_SendChar('-');
  number = number * -1;
 }
 while(number > 0)
 {
  buf[i++] = number % 10; //display in base 10
  number = number / 10;
  //NOTE: the effect of i++ means that the i variable will be at number of digits + 1
 }
 for(; i > 0; i--)
 {
  ITM_SendChar((char)('0' + buf[i-1]));
 }
}

/**
  * @brief  This function sends numbers to the serial wire viewer.
  * @param  number: number to be displayed on SWV
  * @param  digits: number of digits after decimal point
  * @retval None
  */
void SWV_printfloat(double number, int digits)
{
 int i = 0;
 //handle negative numbers
 if(number < 0.0)
 {
  ITM_SendChar('-');
  number = -number;
 }
 //round correctly so that uart_printfloat(1.999, 2) shows as "2.00"
 double rounding = 0.5;
 for(i = 0; i < digits; ++i) rounding = rounding / 10.0;
 number = number + rounding;

 //extract the integer part of the number and print it
 unsigned long int_part = (unsigned long) number;
 double remainder = (double)(number - (double)int_part);
 SWV_printnum(int_part); //print the integer part
 if(digits > 0) ITM_SendChar('.'); //print decimal pint
 int toprint;
 while(digits-- > 0)
 {
  remainder = remainder * 10.0;
  toprint = (int)remainder;
  SWV_printnum(toprint);
  remainder = remainder - toprint;
 }

}










Outcomes