如何用STM32 Nucleo 开发板实现一个串口通信的例子.
1.安装软件
1>keil -- www.keil.com 下载(目前我使用的是学习评估版本,有32K代码限制)
2>STM32CubeMx -- www.st.com下载
3>串口调试助手自行下载.
2.详细实现步骤:
1>代码与配置文件见下面附件.
1.安装软件
1>keil -- www.keil.com 下载(目前我使用的是学习评估版本,有32K代码限制)
2>STM32CubeMx -- www.st.com下载
3>串口调试助手自行下载.
2.详细实现步骤:
1>代码与配置文件见下面附件.
3.Keil中代码增加与修改:
1>.添加头文件 #include "stdio.h"
2>.增加代码,实现printf功能:
/* USER CODE BEGIN PV */
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
3>.添加测试代码:
printf("The baund rate is:%dbps\r\n",115200);
printf("The file is:%s,line is:%d\r\n",__FILE__,__LINE__);
printf("The date is:%s\r\n",__DATE__);
printf("The file is:%s\r\n\r\n",__TIME__);
HAL_Delay(500);
4.编译下载,复位后:测试效果如下: