查询MM32G0001规格书,可以复用PA12与PA3作为USART1的TX/RX引脚
需要注意,配置引脚后得使用引脚复用函数GPIO_PinAFConfig设置相应的复用功能
void USART1_Init(void)
{
//相关GPIO与USART1时钟使能
RCC_APB1PeriphClockCmd(RCC_APB1ENR_USART1 , ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
//配置PA12 (TX) 引脚
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
//复用推挽输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//引脚配置好后得使用引脚复用函数设置复用功能
//复用AF1(USART_TX)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_1);
//配置PA3 (RX) 引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
//浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//复用AF1(USART_RX)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
//USART参数配置
USART_InitTypeDef USART_InitStructure;
//波特率
USART_InitStructure.USART_BaudRate = 115200;
//8位数据
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//1停止位
USART_InitStructure.USART_StopBits = USART_StopBits_1;
//无校验位
USART_InitStructure.USART_Parity = USART_Parity_No;
//无硬件流控
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//启用发送/接收
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
//使能USART1
USART_Cmd(USART1, ENABLE);
}
//包装一下USART1发送函数
void USART1_Send(uint8_t Byte)
{
USART_SendData(USART1,Byte);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}
//发送数组
void USART1_SendArray(uint8_t *Arrya,uint16_t Length)
{
uint16_t i;
for(i = 0; i < Length;i++)
{
USART1_Send(Arrya);
}
}
//发送字符串
void USART1_SendString(char *String)
{
uint8_t i;
for(i = 0; String != '\0'; i++)
{
USART1_Send(String);
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_67421329/article/details/149310594
|