如何用STM32 Nucleo 开发板实现一个串口通信的例子

如何用STM32 Nucleo 开发板实现一个串口通信的例子.

12

1.安装软件
 1>keil             -- www.keil.com 下载(目前我使用的是学习评估版本,有32K代码限制)
 2>STM32CubeMx -- www.st.com下载
 3>串口调试助手自行下载.

2.详细实现步骤:
 1>代码与配置文件见下面附件.

1
2
3
4
5
6

7
3.Keil中代码增加与修改:
1>.添加头文件 #include "stdio.h"
8

 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;
}

9
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);

10

4.编译下载,复位后:测试效果如下:

11



技术文档

类型标题档案
硬件IOC

★博文内容均由个人提供,与平台无关,如有违法或侵权,请与网站管理员联系。

★博文作者未开放评论功能