本帖最后由 shenxiaolin 于 2024-5-21 08:34 编辑
哎呀,今天终于更新了,昨天的时候遇到点问题,还是网友们帮忙找的问题,今天就直接上来说一下这个串口的事情哈,串口做为一个常用的通信接口,扮演着非常重要的角色,是对外信息的一个常见接口,APM32的开发板上是有两个对外串口可以使用的,串口一和串口2,通过板子上的跳线就可以实现串口介入DAP的选择。我们这里就简单一点,直接就上代码吧。
#include "DRV_UART.h"
#include "bsp_delay.h"
void USART_Init(void)
{
GPIO_Config_T GPIO_configStruct;
USART_Config_T usartConfigStruct;
RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_USART1);
GPIO_ConfigStructInit(&GPIO_configStruct);
USART_ConfigStructInit(&usartConfigStruct);//UART恢复默认
/* Connect PXx to USARTx_Tx */
GPIO_ConfigPinAF(GPIOA,GPIO_PIN_SOURCE_9 , GPIO_AF_USART1);
/* Connect PXx to USARTx_Rx */
GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_10, GPIO_AF_USART1);
/* Configure USART Tx as alternate function push-pull */
GPIO_configStruct.mode = GPIO_MODE_AF;
GPIO_configStruct.otype= GPIO_OTYPE_PP;
GPIO_configStruct.pupd = GPIO_PUPD_UP;
GPIO_configStruct.pin = GPIO_PIN_9;
GPIO_configStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_configStruct);
/* Configure USART Rx as input floating */
GPIO_configStruct.mode = GPIO_MODE_AF;
GPIO_configStruct.pin = GPIO_PIN_10;
GPIO_configStruct.pupd = GPIO_PUPD_UP;
GPIO_configStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_configStruct);
usartConfigStruct.baudRate = 115200;
usartConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
usartConfigStruct.mode = USART_MODE_TX_RX;
usartConfigStruct.parity = USART_PARITY_NONE;
usartConfigStruct.stopBits = USART_STOP_BIT_1;
usartConfigStruct.wordLength = USART_WORD_LEN_8B;
/* USART configuration */
USART_Config(USART1,&usartConfigStruct);
USART_Enable(USART1);
Delay_ms(50);
/* Enable USART1 RXBNE interrput */
USART_EnableInterrupt(USART1, USART_INT_RXBNE);
USART_ClearStatusFlag(USART1, USART_FLAG_RXBNE);
NVIC_EnableIRQRequest(USART1_IRQn,1,0);
}
void USART1_SendData(uint8_t *dat, uint32_t count)
{
while(count--)
{
while(USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);
USART_TxData(USART1, *dat++);
}
}
void USART1_IRQHandler(void)
{
/* USART1 Recieve Data */
if(USART_ReadStatusFlag(USART1, USART_FLAG_RXBNE) == SET)
{
USART_TxData( USART1,(uint8_t)USART_RxData(USART1));
USART_ClearStatusFlag(USART1, USART_FLAG_RXBNE);
}
}
void UART_Test(void)
{
unsigned char Data[]={"This is APM32F411 USART Demo!\r\n"};
USART1_SendData(Data,31);
}
我们这里测试的是直接上电之后先发送一个AMP的串口例程,然后中断接收到了数据之后直接返回传送回来。
|