返回列表 发新帖我要提问本帖赏金: 10.00元(功能说明)

[AT32L021] 【AT-START-L021测评】05+使用AT32 Work Bench调试串口1

[复制链接]
 楼主| 聪聪哥哥 发表于 2024-12-31 20:48 | 显示全部楼层 |阅读模式
今天和大家分享一下使用AT官方的软件配置串口1的基本过程。
通用同步异步收发器(USART)是一个能通过多种不同的配置与使用不同的数据格式的外设进行通信的通用接口,同时支持异步全双工,异步半双工以及同步传输。USART 提供了可编程的波特率发生器,根据系统频率以及分频系数的不同,可产生高达 5MBits/s 的波特率,用户可以通过配置系统时钟以及分频系数以此产生所需要的特定通信频率。USART 除了支持标准的 NRZ 异步以及同步收发通信协议外,还支持一些常用的其他类型的串行通信协议,如 LIN(局域互联网),IrDA(红外数据组织)SIRENDEC 规范,ISO7816-3 标准的异步智能卡协议,以及CTS/RTS(Clear To Send/Request To Send)硬件流操作,RS485 和 Modbus。USART 还支持多处理器通信,以及可配置通过空闲帧或地址匹配唤醒的静默模式,以此搭建 USART网络,并且同时支持使用 DMA 进行数据的收发,以此实现高速通信。USART 支持双时钟域,PCLK有系统时钟分频后得到,USART CLK来源可以是 PCLK或 HICK或 LEXT,这使得 USART 可以工作在 DEEPSLEEP 模式,并支持低功耗唤醒功能。
1.png
二:软件配置过程:
2.png
看到AT32芯片支持TXD 和RXD引脚调换,这样我们即使PCB 绘制错了,我们可以使用该项功能,再也不用担心PCB绘制错误哦!
系统时钟参考之前的帖子,配置就可以。
三:生成的软件代码分享:
  1. void wk_usart1_init(void)
  2. {
  3.   /* add user code begin usart1_init 0 */

  4.   /* add user code end usart1_init 0 */

  5.   gpio_init_type gpio_init_struct;
  6.   gpio_default_para_init(&gpio_init_struct);

  7.   /* add user code begin usart1_init 1 */
  8.   crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
  9.   crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  10.   /* add user code end usart1_init 1 */

  11.   /* configure the TX pin */
  12.   gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1);
  13.   gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  14.   gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  15.   gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  16.   gpio_init_struct.gpio_pins = GPIO_PINS_9;
  17.   gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  18.   gpio_init(GPIOA, &gpio_init_struct);

  19.   /* configure the RX pin */
  20.   gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1);
  21.   gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  22.   gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  23.   gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  24.   gpio_init_struct.gpio_pins = GPIO_PINS_10;
  25.   gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  26.   gpio_init(GPIOA, &gpio_init_struct);

  27.   /* config usart1 clock source */
  28.   crm_usart_clock_select(CRM_USART1, CRM_USART_CLOCK_SOURCE_PCLK);

  29.   /* configure param */
  30.   usart_init(USART1, 115200, USART_DATA_8BITS, USART_STOP_1_BIT);
  31.   usart_transmitter_enable(USART1, TRUE);
  32.   usart_receiver_enable(USART1, TRUE);
  33.   usart_parity_selection_config(USART1, USART_PARITY_NONE);

  34.   usart_hardware_flow_control_set(USART1, USART_HARDWARE_FLOW_NONE);

  35.   /**
  36.    * Users need to configure USART1 interrupt functions according to the actual application.
  37.    * 1. Call the below function to enable the corresponding USART1 interrupt.
  38.    *     --usart_interrupt_enable(...)
  39.    * 2. Add the user's interrupt handler code into the below function in the at32l021_int.c file.
  40.    *     --void USART1_IRQHandler(void)
  41.    */

  42.   /* add user code begin usart1_init 2 */

  43.   /* add user code end usart1_init 2 */

  44.   usart_enable(USART1, TRUE);

  45.   /* add user code begin usart1_init 3 */

  46.   /* add user code end usart1_init 3 */
  47. }
我所使用的 图形化配置工具,版本号为
3.png
在串口的初始化,缺少 串口1 和PA引脚的时钟使能函数,需要添加下面代码:
  1.   /* add user code begin usart1_init 1 */
  2.   crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
  3.   crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  4.   /* add user code end usart1_init 1 */
实物测试如下:
4.png

建议:下个图像化配置软件工具,加上串口 和引脚的时钟使能函数部分。

打赏榜单

ArterySW 打赏了 10.00 元 2025-01-07
理由:作品优秀

muyichuan2012 发表于 2025-1-2 09:46 | 显示全部楼层
“建议:下个图像化配置软件工具,加上串口 和引脚的时钟使能函数部分。”
您好,串口和引脚的时钟使能函数在void wk_periph_clock_config(void)函数中调用的。
该函数在at32xxx_wk_config.c中定义,在main函数中调用的。
上述函数是workbench自动生成的,用户无需再次去开启时钟。

 楼主| 聪聪哥哥 发表于 2025-1-4 14:33 | 显示全部楼层
muyichuan2012 发表于 2025-1-2 09:46
“建议:下个图像化配置软件工具,加上串口 和引脚的时钟使能函数部分。”
您好,串口和引脚的时钟使能函数 ...

好的 我这边没在意 不好意思!!!
我只会加减乘除 发表于 2025-1-8 17:07 | 显示全部楼层
TXD和RXD引脚翻转应该不是全系支持的吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则

85

主题

222

帖子

1

粉丝

85

主题

222

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部