2017-09-15 01:38 PM
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
:)
2017-09-18 02:53 AM
sprintf creates data in the buffer not output to the console. Show more complete example.
2017-09-18 03:02 AM
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
2017-09-18 07:59 AM
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 */}//******************************************************************************
2017-09-18 09:00 AM
,
,
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 callbackfunction
. 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 */}