cancel
Showing results for 
Search instead for 
Did you mean: 

sprintf function does not work in keil compiler

Ali Rezaei
Associate
Posted on September 15, 2017 at 22:38

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  

:)

4 REPLIES 4
Posted on September 18, 2017 at 11:53

sprintf creates data in the buffer not output to the console.  Show more complete example.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Tilen MAJERLE
ST Employee
Posted on September 18, 2017 at 12:02

When using sprintf function, you must declare buffer where sprint function will format output string and then you can send it via UART.

Something similar to:

char buffer[10];
sprintf(buffer, 'Hello

');
HAL_UART_Transmit(&huart, (uint8_t *)buffer, strlen(buffer), 100);�?�?�?�?

If you want to output data directly, then take a look at printf and not sprintf. In this case, you will have to implement fputc callback function.

Best regards,

Tilen

Posted on September 18, 2017 at 16:59

HAL_UART_Transmit(&huart1,(uint8_t *) ch, 20,100); // NO NOT LIKE THIS

HAL_UART_Transmit(&huart1,(uint8_t *) &ch, 1,100); // You need a POINTER to the variable and sending ONE character

HAL_UART_Transmit(&huart1,(uint8_t *) buffer, strlen(buffer), 100); // Get the length right, the timeout long enough

//******************************************************************************

// Hosting of stdio functionality through USART1

//******************************************************************************

void outchar(int x)

{

  while((USART1->ISR & USART_ISR_TXE) == 0);

  USART1->TDR = x;

}

//******************************************************************************

int inchar(void)

{

  while((USART1->ISR & USART_ISR_RXNE) == 0);

  return((int)USART1->RDR & 0xFF);

}

//******************************************************************************

#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)

{

  outchar(ch);

  return(ch);

}

int fgetc(FILE *f)

{

  return(inchar());

}

int ferror(FILE *f)

{

  /* Your implementation of ferror */

  return EOF;

}

void _ttywrch(int ch)

{

  outchar(ch);

}

void _sys_exit(int return_code)

{

label:  goto label;  /* endless loop */

}

//******************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 18, 2017 at 16:00

 ,

 ,

Hi

thanks for your help. here is my simple code with works with sprintf function as you explained but still have a problem with printf

function

. I guess I did something wrong in fputc callback

function

. Do you have any idea? ,

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

function

.

1- MCU transmit

function

.

2- filling a string and sending with MCU transmit

function

.

3- with printf

function

.

it is just to check how different

functions

work in STM32F0 and keil.

it works but instead of having value 23 with printf function, I have two Small square on screen.

/* 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 */

}