[N32G43x] 关于国民技术n32G43的端口重映射问题【迫在眉头】

[复制链接]
624|4
aademac 发表于 2025-9-22 17:35 | 显示全部楼层 |阅读模式

我想开启开启USART1引脚重映射(从PA9/PA10映射到PB6/PB7)。但我始终还是无法成功。 国民技术欠缺函数帮助文档,这个让我很难解决。玛法各位大佬帮我,导致问题出在哪?

帮我修正下代码。

  1. #include "n32g43x.h"                    // Device header
  2. #include "systick.h"

  3. #include <stdio.h>

  4. void GPIO_LED_Init()
  5. {
  6.     GPIO_InitType GPIO_InitStruct;
  7.    
  8.     RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPAEN, ENABLE);
  9.    
  10.     GPIO_InitStruct.Pin = GPIO_PIN_8;
  11.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
  12.     GPIO_InitStruct.GPIO_Current = GPIO_DC_2mA;
  13.    
  14.     GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct);
  15. }

  16. void USART1_init()
  17. {
  18.     GPIO_InitType GPIO_InitStruct_Tx;
  19.     GPIO_InitType GPIO_InitStruct_Rx;
  20.    
  21.    
  22. //    /* --------------- USART1 默认端口的配置 配置开始 --------------- */
  23. //    RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPAEN, ENABLE);
  24. //   
  25. //    // Tx PA9 复用输出推挽
  26. //    GPIO_InitStruct_Tx.Pin            = GPIO_PIN_9;
  27. //    GPIO_InitStruct_Tx.GPIO_Mode      = GPIO_Mode_AF_PP;
  28. //    GPIO_InitStruct_Tx.GPIO_Alternate = GPIO_AF4_USART1;
  29. //   
  30. //    GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct_Tx);
  31. //    // Rx PA10 输入浮空输入上拉
  32. //    GPIO_InitStruct_Rx.Pin            = GPIO_PIN_10;
  33. //    GPIO_InitStruct_Rx.GPIO_Pull      = GPIO_Pull_Up;
  34. //    GPIO_InitStruct_Rx.GPIO_Alternate = GPIO_AF4_USART1;
  35. //   
  36. //    GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct_Rx);
  37. //    /* --------------- USART1 默认端口的配置 配置结束 --------------- */
  38.    
  39.    
  40.    
  41.      /* 如果PA9和PA10端口被占用,则可以通过配置重映射端口 */
  42.     RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_AFIOEN, ENABLE);         // 使能AFIO模块的时钟
  43.    
  44.     //配置重映射寄存器,使能重映射功能
  45. //    GPIO_ConfigPinRemap(GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE6, GPIO_AF1_USART1);
  46. //    GPIO_ConfigPinRemap(GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE7, GPIO_AF1_USART1);
  47.    
  48.     RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPBEN, ENABLE);

  49.     // Tx PB6 复用输出推挽
  50.     GPIO_InitStruct_Tx.Pin                 = GPIO_PIN_6;
  51.     GPIO_InitStruct_Tx.GPIO_Mode           = GPIO_Mode_AF_PP;
  52.     GPIO_InitStruct_Tx.GPIO_Alternate      = GPIO_AF4_USART1;
  53.    
  54.     GPIO_InitPeripheral(GPIOB, &GPIO_InitStruct_Tx);
  55.     // Rx PB7 输入浮空输入上拉
  56.     GPIO_InitStruct_Rx.Pin                 = GPIO_PIN_7;
  57.     GPIO_InitStruct_Rx.GPIO_Mode           = GPIO_Mode_Input;
  58.     GPIO_InitStruct_Rx.GPIO_Pull           = GPIO_Pull_Up;
  59.     GPIO_InitStruct_Tx.GPIO_Alternate      = GPIO_AF4_USART1;
  60.    
  61.     GPIO_InitPeripheral(GPIOB, &GPIO_InitStruct_Rx);
  62.     /* --------------- 端口映射配置结束 --------------- */


  63.     USART_InitType USART_InitStruct;
  64.    
  65.     RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE);          // 开启时钟
  66.    
  67.     USART_InitStruct.BaudRate = 115200;                  // 波特率115200
  68.     USART_InitStruct.Mode = USART_MODE_TX | USART_MODE_RX;  // 双向
  69.     USART_InitStruct.WordLength = 8;       // 8位数据位
  70.     USART_InitStruct.StopBits = USART_STPB_1;  // 1位停止位
  71.     USART_InitStruct.Parity = USART_PE_NO;  // 无校验
  72.    
  73.     USART_Init(USART1, &USART_InitStruct);
  74.    
  75.     USART_Enable(USART1, ENABLE);   // 使能USART1  总开关
  76. }

  77. void MY_USART_SendBytes(USART_Module* USARTx, uint8_t *pData, uint16_t Size)
  78. {
  79.     for (uint16_t i = 0; i < Size; i++)
  80.     {
  81.         // #1. 等待发送数据寄存器空
  82.         while(USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
  83.         // #2. 将要发送的数据写入到发送数据寄存器
  84.         USART_SendData(USARTx, pData[i]);
  85.     }
  86.    
  87.     // #3. 等待数据发送完成
  88.     while(USART_GetFlagStatus(USARTx, USART_FLAG_TXC) == RESET);
  89. }

  90. /* retarget the C library printf function to the USART */
  91. int fputc(int ch, FILE* f)
  92. {
  93.     // #1. 等待发送数据寄存器空
  94.     while (USART_GetFlagStatus(USART1, USART_FLAG_TXDE) == RESET);
  95.     // #2. 写入发数据寄存器当中
  96.     USART_SendData(USART1, (uint8_t)ch);
  97.         ;

  98.     return (ch);
  99. }

  100. int main(void)
  101. {
  102.     RCC_ClocksType RCC_ClocksStruct;
  103.     RCC_GetClocksFreqValue(&RCC_ClocksStruct);
  104.    
  105.     systick_config();
  106.    
  107.     USART1_init();
  108.     GPIO_LED_Init();
  109.    
  110.    
  111. //    uint8_t byTwoSend[] = {1,2,3,4,5};
  112. //    MY_USART_SendBytes(USART1, byTwoSend, 5);
  113.    
  114.    
  115.     printf("\r\nCK_SYS is %d", RCC_ClocksStruct.SysclkFreq);       // 打印当前的系统时钟
  116.   
  117.    
  118.     while(1)
  119.     {
  120.         // #1. 等待接收数据寄存器RDR非空
  121.         while (USART_GetFlagStatus(USART1, USART_FLAG_RXDNE) == RESET);
  122.         // #2. 读取数据
  123.         uint8_t byte_rcvd = USART_ReceiveData(USART1);
  124.         // #3. 对数据进行处理
  125.         if (byte_rcvd == '0')
  126.             GPIO_WriteBit(GPIOA, GPIO_PIN_8, Bit_RESET);
  127.         else if (byte_rcvd == '1')
  128.             GPIO_WriteBit(GPIOA, GPIO_PIN_8, Bit_SET);
  129.     }
  130. }




技术工程师 发表于 2025-9-24 08:42 | 显示全部楼层
我记得用户手册里面有个AFIO章节,把GPIO_Alternate改成对应的不就行了
dffzh 发表于 2025-9-24 09:03 | 显示全部楼层
国民技术官网上没有demo例程吗?
qianwang1001 发表于 2025-9-28 18:03 | 显示全部楼层
USART1选用PB6,PB7作为TX/RX,重映射为AF0
99ca6707-63da-4101-9b05-a172a966e5f8.png
 楼主| aademac 发表于 2025-10-4 06:58 | 显示全部楼层
qianwang1001 发表于 2025-9-28 18:03
USART1选用PB6,PB7作为TX/RX,重映射为AF0

感谢,终于成功了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

9

帖子

0

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