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

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


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

帮我修正下代码。


```
#include "n32g43x.h"                    // Device header
#include "systick.h"

#include <stdio.h>

void GPIO_LED_Init()
{
    GPIO_InitType GPIO_InitStruct;
   
    RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPAEN, ENABLE);
   
    GPIO_InitStruct.Pin = GPIO_PIN_8;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_InitStruct.GPIO_Current = GPIO_DC_2mA;
   
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct);
}

void USART1_init()
{
    GPIO_InitType GPIO_InitStruct_Tx;
    GPIO_InitType GPIO_InitStruct_Rx;
   
   
//    /* --------------- USART1 默认端口的配置 配置开始 --------------- */
//    RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPAEN, ENABLE);
//   
//    // Tx PA9 复用输出推挽
//    GPIO_InitStruct_Tx.Pin            = GPIO_PIN_9;
//    GPIO_InitStruct_Tx.GPIO_Mode      = GPIO_Mode_AF_PP;
//    GPIO_InitStruct_Tx.GPIO_Alternate = GPIO_AF4_USART1;
//   
//    GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct_Tx);
//    // Rx PA10 输入浮空输入上拉
//    GPIO_InitStruct_Rx.Pin            = GPIO_PIN_10;
//    GPIO_InitStruct_Rx.GPIO_Pull      = GPIO_Pull_Up;
//    GPIO_InitStruct_Rx.GPIO_Alternate = GPIO_AF4_USART1;
//   
//    GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct_Rx);
//    /* --------------- USART1 默认端口的配置 配置结束 --------------- */
   
   
   
     /* 如果PA9和PA10端口被占用,则可以通过配置重映射端口 */
    RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_AFIOEN, ENABLE);         // 使能AFIO模块的时钟
   
    //配置重映射寄存器,使能重映射功能
//    GPIO_ConfigPinRemap(GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE6, GPIO_AF1_USART1);
//    GPIO_ConfigPinRemap(GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE7, GPIO_AF1_USART1);
   
    RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPBEN, ENABLE);

    // Tx PB6 复用输出推挽
    GPIO_InitStruct_Tx.Pin                 = GPIO_PIN_6;
    GPIO_InitStruct_Tx.GPIO_Mode           = GPIO_Mode_AF_PP;
    GPIO_InitStruct_Tx.GPIO_Alternate      = GPIO_AF4_USART1;
   
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStruct_Tx);
    // Rx PB7 输入浮空输入上拉
    GPIO_InitStruct_Rx.Pin                 = GPIO_PIN_7;
    GPIO_InitStruct_Rx.GPIO_Mode           = GPIO_Mode_Input;
    GPIO_InitStruct_Rx.GPIO_Pull           = GPIO_Pull_Up;
    GPIO_InitStruct_Tx.GPIO_Alternate      = GPIO_AF4_USART1;
   
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStruct_Rx);
    /* --------------- 端口映射配置结束 --------------- */


    USART_InitType USART_InitStruct;
   
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE);          // 开启时钟
   
    USART_InitStruct.BaudRate = 115200;                  // 波特率115200
    USART_InitStruct.Mode = USART_MODE_TX | USART_MODE_RX;  // 双向
    USART_InitStruct.WordLength = 8;       // 8位数据位
    USART_InitStruct.StopBits = USART_STPB_1;  // 1位停止位
    USART_InitStruct.Parity = USART_PE_NO;  // 无校验
   
    USART_Init(USART1, &USART_InitStruct);
   
    USART_Enable(USART1, ENABLE);   // 使能USART1  总开关
}

void MY_USART_SendBytes(USART_Module* USARTx, uint8_t *pData, uint16_t Size)
{
    for (uint16_t i = 0; i < Size; i++)
    {
        // #1. 等待发送数据寄存器空
        while(USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
        // #2. 将要发送的数据写入到发送数据寄存器
        USART_SendData(USARTx, pData);
    }
   
    // #3. 等待数据发送完成
    while(USART_GetFlagStatus(USARTx, USART_FLAG_TXC) == RESET);
}

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

    return (ch);
}

int main(void)
{
    RCC_ClocksType RCC_ClocksStruct;
    RCC_GetClocksFreqValue(&RCC_ClocksStruct);
   
    systick_config();
   
    USART1_init();
    GPIO_LED_Init();
   
   
//    uint8_t byTwoSend[] = {1,2,3,4,5};
//    MY_USART_SendBytes(USART1, byTwoSend, 5);
   
   
    printf("\r\nCK_SYS is %d", RCC_ClocksStruct.SysclkFreq);       // 打印当前的系统时钟
  
   
    while(1)
    {
        // #1. 等待接收数据寄存器RDR非空
        while (USART_GetFlagStatus(USART1, USART_FLAG_RXDNE) == RESET);
        // #2. 读取数据
        uint8_t byte_rcvd = USART_ReceiveData(USART1);
        // #3. 对数据进行处理
        if (byte_rcvd == '0')
            GPIO_WriteBit(GPIOA, GPIO_PIN_8, Bit_RESET);
        else if (byte_rcvd == '1')
            GPIO_WriteBit(GPIOA, GPIO_PIN_8, Bit_SET);
    }
}



```



您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

7

帖子

0

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