diff --git a/book/02:STM32的硬件电路/01.md b/book/02:STM32的硬件电路/01.md index f7e74ef..e4346cb 100644 --- a/book/02:STM32的硬件电路/01.md +++ b/book/02:STM32的硬件电路/01.md @@ -163,9 +163,9 @@ VBUS是TYPE-C的5V,VSWD是调试接口SWD的5V,通过防倒灌电路使得 下载 [stm32cubeide](https://www.st.com.cn/zh/development-tools/stm32cubeide.html) 。 -另外,后续的开发中会使用到串口,推荐开源的 xtools +另外,后续的开发中会使用到串口,推荐开源的 xtools(在我的Linux系统中运行有问题),或者是 ScriptCommunicator。 -下载 [xtools](https://gitee.com/x-tools-author/x-tools) 。 +下载 [xtools](https://gitee.com/x-tools-author/x-tools) ,[ScriptCommunicator](https://github.com/szieke/ScriptCommunicator_serial-terminal?tab=readme-ov-file) > 注意,虽然下载不需要用户,但是使用需要登录!否则无法下载相应的芯片资源,无法进行编译!在下图中 MyST 菜单中登录后才能进行相应的开发。 @@ -487,6 +487,9 @@ LSE和HSE全部都是灰色,而LSI和HSI都是蓝色,这表示目前芯片 在 main.c 函数的大约 100 行左右修改代码如下: ```c + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + char *msg = "hello world!\n"; while (1) { /* USER CODE END WHILE */ @@ -495,8 +498,88 @@ LSE和HSE全部都是灰色,而LSI和HSI都是蓝色,这表示目前芯片 HAL_UART_Transmit(&huart2, msg, strlen(msg), 100); } + /* USER CODE END 3 */ +} +``` + +编译后发现有三个警告, + +``` +09:10:24 Build Finished. 0 errors, 3 warnings. (took 757ms) +``` + +虽然警告一般不影响我们的程序,但我们还是试图解决。一直向上面,看看警告的具体内容: + +``` +../Core/Src/main.c:103:49: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration] + 103 | HAL_UART_Transmit(&huart2, msg, strlen(msg), 100); + | ^~~~~~ +../Core/Src/main.c:21:1: note: include '' or provide a declaration of 'strlen' + 20 | #include "main.h" + +++ |+#include +``` + +这里其实已经给了我们解决方案,因为strlen函数是在 string.h 中,而没有引入这个头文件(string.h应该是缺省引入的),需要添加头文件的引用。注意,这个引用必须在特定的位置,否则STM32CubeMX重新生成代码的时候会删除。 + +来到main.c的第22行左右:加入引用: + +```c +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +#include +/* USER CODE END Includes */ +``` + +下一个警告是: + +``` +../Core/Src/main.c:103:44: warning: pointer targets in passing argument 2 of 'HAL_UART_Transmit' differ in signedness [-Wpointer-sign] + 103 | HAL_UART_Transmit(&huart2, msg, strlen(msg), 100); + | ^~~ + | | + | char * +In file included from ../Core/Inc/stm32f1xx_hal_conf.h:338, + from ../Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal.h:29, + from ../Core/Inc/main.h:30, + from ../Core/Src/main.c:20: +../Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_uart.h:749:79: note: expected 'const uint8_t *' {aka 'const unsigned char *'} but argument is of type 'char *' + 749 | HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout); ``` +HAL_UART_Transmit 这个函数负责向串口发送数据,其第二个参数的类型是 uint8_t * 类型,但是我们传入的是 char * 类型。虽然这两个类型是兼容的,我们还是最好改一下。有多种方案,现在我们直接用强制类型转换。 + +```c + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + char *msg = "hello world!\n"; + while (1) { + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + HAL_Delay(1000); + HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), 100); + + } + /* USER CODE END 3 */ +} +``` + +> 如果把 msg 的类型改成 uint8_t* 任然会出现警告,自己试试为什么? + +好了,完美了,没有任何警告。 + +## 5.4. 运行效果 + +把程序下载到开发板,打开串口工具,注意波特率等参数一定和开发板的串口参数一致: + +![image-20240903092357537](./img/image-20240903092357537.png) + +最后我们成功的收到了串口发送的数据: + +![image-20240903092434632](./img/image-20240903092434632.png) + + + # 6. 实践任务 1. 按照本章的介绍,独自完成 Blink 点亮小灯的程序; diff --git a/book/02:STM32的硬件电路/img/image-20240903092357537.png b/book/02:STM32的硬件电路/img/image-20240903092357537.png new file mode 100644 index 0000000..4e24c56 Binary files /dev/null and b/book/02:STM32的硬件电路/img/image-20240903092357537.png differ diff --git a/book/02:STM32的硬件电路/img/image-20240903092434632.png b/book/02:STM32的硬件电路/img/image-20240903092434632.png new file mode 100644 index 0000000..fbaa612 Binary files /dev/null and b/book/02:STM32的硬件电路/img/image-20240903092434632.png differ