【STM32-代码】

发布于:2025-04-17 ⋅ 阅读:(88) ⋅ 点赞:(0)

■ printf() 输出到uart1

static UART_HandleTypeDef * g_HDebugUART = &huart1;

int fputc(int c, FILE *f) 
{
    (void)f;
    HAL_UART_Transmit(g_HDebugUART, (const uint8_t *)&c, 1, DEBUG_UART_TIMEOUT);
    return c;
}

int fgetc(FILE *f)
{
    uint8_t ch = 0;
    (void)f;

    /* Clear the Overrun flag just before receiving the first character */
    __HAL_UART_CLEAR_OREFLAG(g_HDebugUART);

    /* Wait for reception of a character on the USART RX line and echo this
    * character on console */
    HAL_UART_Receive(g_HDebugUART, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
    HAL_UART_Transmit(g_HDebugUART, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
    return ch;    
}

■ sscanf()

typedef struct rtc_time {
  int tm_sec;	//seconds [0,59]
  int tm_min;	//minutes [0,59]
  int tm_hour;  //hour [0,23]
  int tm_mday;  //day of month [1,31]
  int tm_mon;   //month of year [1,12]
  int tm_year; // since 1970
  int tm_wday; // sunday = 0
}t_rtc;

t_rtc timeval;
char timeStr[21];
UartReadLine(timeStr, sizeof(timeStr));  //从串口中获取一个字符串

retval = sscanf((char *)timeStr, "%2d/%2d/%2d,%2d:%2d:%2d", &timeval.tm_year, &timeval.tm_mon, \
                &timeval.tm_mday, &timeval.tm_hour, &timeval.tm_min, &timeval.tm_sec);
timeval.tm_year = timeval.tm_year >= 70 ? (1900 + timeval.tm_year) : (2000 + timeval.tm_year);
if (retval < 6)
{
    PrintfResp("\r\nPlease input correct value\r\n");
    break;
}


网站公告

今日签到

点亮在社区的每一天
去签到