This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

sprintf function does not work in keil

Hi to all
I am new in STM32 and keil (I used Atmel chips) and now I am getting familier with them.
I made a project and I can send data to serial port with the following function :
"HAL_UART_Transmit(&huart1,(uint8_t *) "Hello\n\r", 7, 10);"
but I do not know why there is no data when I use the following function based on sprintf:
"sprintf((char *) buffer,"23");"

The thing is that the code is well compiled and there is no error but the function does not work.
Do I need to add some more code for sprintf function ??!!!

Help please

Parents
  • Hi
    thanks for your help and I do apologize for not clarifying the problem. I added my code but I do not know why it did not show there. I guess I did something wrong.
    here is my simple code.

    it is a simple code. it waits until a push button pressed, then it turns on a LED and sends 3 data to PC console with 3 different fucntion.
    1- MCU transmit fucntion.
    2- filling a string with sprintf function and sending with MCU transmit function.
    3- with printf function.

    I use this code to check how different functions work in STM32F0 and keil especially stdio library.
    it works but instead of having value 23 with printf function, I have two Small square on screen.
    I could not add the picture of my console but I guess you got what I mean.

    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "stm32f0xx_hal.h"
    #include "stdio.h"
    
    /* USER CODE BEGIN Includes */
    /* USER CODE END Includes */
    
    /* Private variables ---------------------------------------------------------*/
    UART_HandleTypeDef huart1;
    /* USER CODE BEGIN PV */
    
    /* Private variables ---------------------------------------------------------*/
    char buffer[20];
    char a[1] = "A";
    
    /* USER CODE END PV */
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_USART1_UART_Init(void);
    /* USER CODE BEGIN PFP */
    
    /* Private function prototypes -----------------------------------------------*/
    /* USER CODE END PFP */
    /* USER CODE BEGIN 0 */
    
    struct __FILE {int hndle;};
    FILE __stdout;
    FILE __stdin;
    FILE __stderr;
    int fputc(int ch, FILE *f)
    {
    while (!HAL_UART_GetState(&huart1));
    HAL_UART_Transmit(&huart1,(uint8_t *) ch, 20,100);
    return ch;
    }
    /* USER CODE END 0 */
    
    
    int main(void)
    {
    /* USER CODE BEGIN 1 */
    /* USER CODE END 1 */
    /* MCU Configuration----------------------------------------------------------*/
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();
    /* USER CODE BEGIN Init */
    /* USER CODE END Init */
    /* Configure the system clock */
    SystemClock_Config();
    /* USER CODE BEGIN SysInit */
    /* USER CODE END SysInit */
    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_USART1_UART_Init();
    /* USER CODE BEGIN 2 */
    HAL_UART_Transmit(&huart1,(uint8_t *) "Hello", 5, 10);
    /* USER CODE END 2 */
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
    if (HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_0) == 1){
    HAL_GPIO_WritePin(GPIOC,LD3_Pin,GPIO_PIN_SET);
    HAL_UART_Transmit(&huart1,(uint8_t *) "Hello\n\r", 7, 10);
    sprintf(buffer,"hello world");
    HAL_UART_Transmit(&huart1,(uint8_t *) buffer, 20, 10);
    printf("23");
    HAL_Delay(300);
    }
    else if (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == 0){
    HAL_GPIO_WritePin(GPIOC,LD3_Pin,GPIO_PIN_RESET);
    }
    
    }
    /* USER CODE END 3 */
    }
    

Reply
  • Hi
    thanks for your help and I do apologize for not clarifying the problem. I added my code but I do not know why it did not show there. I guess I did something wrong.
    here is my simple code.

    it is a simple code. it waits until a push button pressed, then it turns on a LED and sends 3 data to PC console with 3 different fucntion.
    1- MCU transmit fucntion.
    2- filling a string with sprintf function and sending with MCU transmit function.
    3- with printf function.

    I use this code to check how different functions work in STM32F0 and keil especially stdio library.
    it works but instead of having value 23 with printf function, I have two Small square on screen.
    I could not add the picture of my console but I guess you got what I mean.

    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "stm32f0xx_hal.h"
    #include "stdio.h"
    
    /* USER CODE BEGIN Includes */
    /* USER CODE END Includes */
    
    /* Private variables ---------------------------------------------------------*/
    UART_HandleTypeDef huart1;
    /* USER CODE BEGIN PV */
    
    /* Private variables ---------------------------------------------------------*/
    char buffer[20];
    char a[1] = "A";
    
    /* USER CODE END PV */
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_USART1_UART_Init(void);
    /* USER CODE BEGIN PFP */
    
    /* Private function prototypes -----------------------------------------------*/
    /* USER CODE END PFP */
    /* USER CODE BEGIN 0 */
    
    struct __FILE {int hndle;};
    FILE __stdout;
    FILE __stdin;
    FILE __stderr;
    int fputc(int ch, FILE *f)
    {
    while (!HAL_UART_GetState(&huart1));
    HAL_UART_Transmit(&huart1,(uint8_t *) ch, 20,100);
    return ch;
    }
    /* USER CODE END 0 */
    
    
    int main(void)
    {
    /* USER CODE BEGIN 1 */
    /* USER CODE END 1 */
    /* MCU Configuration----------------------------------------------------------*/
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();
    /* USER CODE BEGIN Init */
    /* USER CODE END Init */
    /* Configure the system clock */
    SystemClock_Config();
    /* USER CODE BEGIN SysInit */
    /* USER CODE END SysInit */
    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_USART1_UART_Init();
    /* USER CODE BEGIN 2 */
    HAL_UART_Transmit(&huart1,(uint8_t *) "Hello", 5, 10);
    /* USER CODE END 2 */
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
    if (HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_0) == 1){
    HAL_GPIO_WritePin(GPIOC,LD3_Pin,GPIO_PIN_SET);
    HAL_UART_Transmit(&huart1,(uint8_t *) "Hello\n\r", 7, 10);
    sprintf(buffer,"hello world");
    HAL_UART_Transmit(&huart1,(uint8_t *) buffer, 20, 10);
    printf("23");
    HAL_Delay(300);
    }
    else if (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == 0){
    HAL_GPIO_WritePin(GPIOC,LD3_Pin,GPIO_PIN_RESET);
    }
    
    }
    /* USER CODE END 3 */
    }
    

Children
No data