STM32でprintf
STM32のUART通信で、数値を出力しようとすると上手くいかない。
(文字化けする)
printfを導入して、それで出力すると上手くいく。
STM32マイコンで、printfを使用できるようにする。
ideのコンソール:
https://taku-myconstudy.blogspot.com/2021/04/stm32f401-nucleo.html
code:c
/* USER CODE BEGIN Includes */
//printfを使用するための、ヘッダをインクルード
#include <stdio.h>
/* USER CODE END Includes */
/* USER CODE BEGIN 0 */
int __io_putchar(int c) {
if( c == '\n' ) {
int b = '\r';
HAL_UART_Transmit(&huart2,(uint8_t*)&b, 1, 1);
}
HAL_UART_Transmit(&huart2,(uint8_t*)&c, 1, 1);
return 0;
}
/* USER CODE END 0 */
/* USER CODE BEGIN 2 */
//標準出力stdoutにNULLを指定
setbuf(stdout,NULL);
//上記内容までが、printfを使用するために必要。
//xに10を格納する。
int x;
x = 10;
//testをprintfで出力する。
printf("test\n");
//x = 10をprintfで出力する。
printf("x=%d\n",x);
/* USER CODE END 2 */
ファイル、時間などの記載を入れる。
code:c
printf("%010u:%s:%04d:%s\n", GetTickCount(), __FILE__, __LINE__,__func__);
code:c
#include <limits.H>
static unsigned int tickCount = 0;
unsigned int GetTickCount()
{
return tickCount;
}
void UpdateTickCount()
{
if( tickCount <UINT_MAX){
tickCount++;
}
}
SWV_ITM_Data_Console.txt