510
个编辑
更改
无编辑摘要
== 10-串口打印实验 ==
串口打印实验,给大家展示的是如何配置STM32L476一个有效的硬件串口功能,并且顺带给大家介绍了如何去配置一个格式化打印函数printf()。
=== STM32L476 UART简介 ===
USART主要功能:
•全双工异步通信
•NRZ标准格式(标记/空格)
•可配置的过采样方法16或8,以提供速度和速度之间的灵活性
时钟容差
•通用可编程发送和接收波特率高达10 Mbit / s时
时钟频率为80 MHz,过采样为8
•双时钟域允许:
- USART功能和从停止模式唤醒
- 独立于PCLK重新编程的便捷波特率编程
•自动波特率检测
•可编程数据字长(7,8或9位)
•可编程数据顺序,具有MSB优先或LSB优先移位
•可配置的停止位(1或2个停止位)
•同步模式和时钟输出,用于同步通信
•单线半双工通信
•使用DMA进行持续通信
•使用集中式DMA将接收/发送的字节缓冲在保留的SRAM中
•发送器和接收器的独立使能位
•独立的信号极性控制,用于发送和接收
•可交换Tx / Rx引脚配置
•调制解调器和RS-485收发器的硬件流控制
•通信控制/错误检测标志
•奇偶校验控制:
- 传输奇偶校验位
- 检查接收数据字节的奇偶校验
•带有标志的14个中断源
•多处理器通信
如果地址不匹配,USART进入静音模式。
•从静音模式唤醒(通过空闲线路检测或地址标记检测)
''<span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span>''
=== 硬件设计 ===
==== gyu_usartc ====
<syntaxhighlight lang="c++" line="1" start="37">
}
</syntaxhighlight>使能GPIOC以及TIM3的时钟,并且配置PC6为TIM3的通道1。配置串口硬件,使能GPIOA以及USART1的时钟,配置PA9和PA10为串口的TX及RX引脚。<syntaxhighlight lang="c++" line="1" start="67">
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
}
}
</syntaxhighlight>定时器周期中断回调函数,TIM3的自动装载值装满一次,进入一次此回调(本工程配置的参数是10ms进入一次)。配置fputc()函数,用于格式化打印,当我们进行了如下代码配置,就可以调用printf()函数去格式化打印调试信息。<syntaxhighlight lang="c++" line="1" start="99">#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) PUTCHAR_PROTOTYPE{ // 配置格式化输出到串口USART1 HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return ch;} </syntaxhighlight> == 11-串口中断实验 == == 12-串口DMA实验 == == 13-TFT显示屏实验 == == 14-二维码显示实验 == == 15-RNG随机发生器实验 == === STM32L476 随机发生器简介 ===''<span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span>''=== 硬件设计 ===选择STM32L4引脚PA9和PA10作为串口,当我们将拨码开关SW1拨到USB一端时,此串口通过CH340芯片转成USB接口,用于向电脑上打印一些调试信息。 ''<span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span>''=== 实验准备 ===# 使用miniUSB线及10pin排线,通过Jlink仿真器连接PC端和开发板。# 使用miniUSB线,连接PC与开发板USB接口。# 将SW1拨到USB端,SW2拨到MCU。# 使用Keil打开基础实验 10-串口打印实验工程。# 使用Xshell打开miniUSB虚拟出的COM口# 下载程序,并完成功能测试。 === 实验验证 ===下载完成后,我们打开miniUSB虚拟出的COM口,可以看到串口周期性的打印计数值。=== 源码详解 ===本节中的源码说明,仅针对此例程中的重要功能,详细的源码介绍请大家参照代码后的注释。 ==== stm32l4xx_hal_conf.h ====此文件位于“09-红外线接收实验\Inc”路径中,主要用途是选择使能此例程使用到的库文件。 此例程我们主要给大家展示STM32L4的串口功能,所以我们宏定义中打开UART相关的。<syntaxhighlight lang="c" line="1" start="103">// 使能的宏#define HAL_MODULE_ENABLED // 芯片#define HAL_FLASH_MODULE_ENABLED // Flash#define HAL_PWR_MODULE_ENABLED // 电源#define HAL_RCC_MODULE_ENABLED // 时钟#define HAL_CORTEX_MODULE_ENABLED // NVIC #define HAL_GPIO_MODULE_ENABLED // GPIO#define HAL_DMA_MODULE_ENABLED // DMA#define HAL_UART_MODULE_ENABLED // UART</syntaxhighlight> ==== main.c ====main函数,我们的例程由此处开始执行,首先调用HAL_Init()函数初始化我们的模块,接着调用SystemClock_Config()函数初始化此例程用到的时钟,具体有哪些时钟被初始化,在gyu_util.c部分有详细说明。 接下来我们初始化串口UASRT1。 在while()循环中,我们每隔100ms通过格式化输出"TimeCount = xx:xx:xx"。 <syntaxhighlight lang="c++" line="1" start="34">int main(void){ uint32_t hour = 0; uint32_t minute = 0; uint32_t second = 0; /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ // 重置所有外设、flash界面以及系统时钟 HAL_Init(); // 配置系统时钟(包含振荡器、系统时钟、总线时钟等等) SystemClock_Config(); // 初始化串口USART1 MX_USART1_UART_Init(); // while (1) { // 模拟时钟计时,这边的1s实际只是100ms HAL_Delay(100); // 100ms延时 printf("TimeCount = %02d:%02d:%02d\r\n",hour,minute,second); // 格式化输出"TimeCount = xx:xx:xx" // 时分秒计数 second++; if(second == 60) { second = 0; minute++; } if(minute == 60) { minute = 0; hour++; } if(hour == 24) { hour = 0; } }}</syntaxhighlight>==== gyu_util.c ====时钟初始化函数,用于配置我们模块运行的系统时钟、AHB高性能总线时钟、APB外设总线时钟以及单个外设的时钟。 主要包含了三个部分的初始化配置。 1.内部或者外部振荡器选择,也就是选择时钟信号的来源,是内部振荡,还是外部晶振。
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
</syntaxhighlight>
== 16-RTC实时时钟实验 ==
[[分类:NB-IOT]]
[[分类:NBDK-L4]]
[[分类:教程]]
__强显目录__