dirty123 发表于 2024-4-22 21:44

【N32G430C8L7_STB开发板】串口打印

本帖最后由 dirty123 于 2024-4-22 21:45 编辑

      串口日志在我们平时开发中会经常用到,本篇讲述N32G430C8L7串口打印。
一.硬件接口
      开发板通过J5端子连接MCU,起到烧录口和串口连接的作用。在开发板原理图中可看到如下

图1:串口连接
      查阅数据手册,N32G430C8L7 的串口引脚配置:PA9--TX,PA10--RX
图2:串口引脚配置

二.代码与配置
1.引脚初始化
void GPIO_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;

    /* Initialize GPIO_InitStructure */
    GPIO_Structure_Initialize(&GPIO_InitStructure);   

    /* Configure USARTx Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTx_TxPin;
    GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Alternate = USARTx_Tx_GPIO_AF;
    GPIO_Peripheral_Initialize(USARTx_GPIO, &GPIO_InitStructure);   

    /* Configure USARTx Rx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTx_RxPin;
    GPIO_InitStructure.GPIO_Alternate = USARTx_Rx_GPIO_AF;
    GPIO_Peripheral_Initialize(USARTx_GPIO, &GPIO_InitStructure);
}
2.串口初始化,配置波特率
    /* USARTy and USARTz configuration ------------------------------------------------------*/
    USART_InitStructure.BaudRate            = 115200;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity            = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USARTx */
    USART_Initializes(USARTx, &USART_InitStructure);
    /* Enable the USARTx */
    USART_Enable(USARTx);3.编译器配置。这里勾选上Use MicroLIB
图3:编译器配置
4.串口重映射,可用printf输出串口日志
/**
*\*\name    fputc.
*\*\fun   retarget the C library printf function to the USART
*\*\param   ch
*\*\param   f
*\*\returnnone
**/
int fputc(int ch, FILE* f)
{
    USART_Data_Send(USARTx, (uint8_t)ch);
    while (USART_Flag_Status_Get(USARTx, USART_FLAG_TXDE) == RESET)
      ;

    return (ch);
}
5.在main函数加入串口日志
图4:串口日志代码

三.编译烧录测试
      编译烧录后,打开串口,按RESET键开发板重启,可看到有日志输出,如下
图5:日志输出

      至此,实现N32G430C8L7_STB开发板串口打印输出。


yinxiangxv 发表于 2024-4-28 16:56

USARTx_TxPin,这里的这个引脚在原理图中对应的是那个引脚呢?原理图中看到的是USART1?

yinxiangxv 发表于 2024-4-28 16:57

开发包中的文件里面有这个代码GPIO_InitStructure.Pin            = USARTy_TxPin;,那这里的USARTy对应原理图中的那个?

dirty123 发表于 2024-4-28 23:27

yinxiangxv 发表于 2024-4-28 16:57
开发包中的文件里面有这个代码GPIO_InitStructure.Pin            = USARTy_TxPin;,那这里的USARTy对应原 ...

PA9,文章由粉红色标记。还有就是可以自己顺着原理图理,结合硬件知道SDK这样配置才可打印

xysr007 发表于 2024-5-8 13:52

我用 vscode编译,没有 use MicroLIB 这个选项,怎么重定向

chenqianqian 发表于 2024-5-9 08:05

不用重定向也可以自己实现,用C语言的格式化字符串函数。
页: [1]
查看完整版本: 【N32G430C8L7_STB开发板】串口打印