1.CubeMX配置
一般的单片机开发板都默认usart1做完串口通讯 勾选异步通讯方式打开串口使用默认的波特率 115200即可
3.代码分析
/*****************************************************
*function: 写字符文件函数
*param1: 输出的字符
*param2: 文件指针
*return: 输出字符的 ASCII 码
******************************************************/
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&Printf_UART, (uint8_t*)&ch,1,10);
return ch;
}
/*****************************************************
*function: 读字符文件函数
*param1: 文件指针
*return: 读取字符的 ASCII 码
******************************************************/
int fgetc(FILE *f)
{
uint8_t ch = 0;
HAL_UART_Receive(&Printf_UART, (uint8_t*)&ch, 1, 10);
return (int)ch;
}
void LED_Thread(void const *argument)
{
for (;;)
{
LED_ON('R');
printf("\r\nLED 红\r\n");
osDelay(500);
LED_OFF('R');
LED_ON('G');
printf("\r\nLED 绿\r\n");
osDelay(500);
LED_OFF('G');
LED_ON('B');
printf("\r\nLED 蓝\r\n");
osDelay(500);
LED_OFF('B');
}
}