这里使用的是uart0在开发板的右上角位置即PA13,PA14引脚
这里用的usb转ttl模块
TXD -> 开发板的RX
RXD -> 开发板的TX
因为jlink连了vcc和gnd,所以这个VCC和GND可以不连
在mf_config.c文件写入一下函数,官方例程随便找一个工程就有这个文件
void MF_UART0_Init(void)
{
FL_GPIO_InitTypeDef GPIO_InitStruct;
FL_UART_InitTypeDef UART0_InitStruct;
GPIO_InitStruct.pin = FL_GPIO_PIN_13;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL; //数字外设
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.pull = FL_ENABLE;
GPIO_InitStruct.remapPin = FL_DISABLE;
GPIO_InitStruct.analogSwitch = FL_DISABLE;
(void)FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.pin = FL_GPIO_PIN_14;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.pull = FL_DISABLE;
GPIO_InitStruct.remapPin = FL_DISABLE;
GPIO_InitStruct.analogSwitch = FL_DISABLE;
(void)FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
UART0_InitStruct.clockSrc = FL_CMU_UART0_CLK_SOURCE_APBCLK;
UART0_InitStruct.baudRate = 115200;
UART0_InitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;
UART0_InitStruct.dataWidth = FL_UART_DATA_WIDTH_8B;
UART0_InitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B;
UART0_InitStruct.parity = FL_UART_PARITY_NONE;
(void)FL_UART_Init(UART0, &UART0_InitStruct);
}
void MF_UART0_Interrupt_Init(void)
{
FL_UART_ClearFlag_RXBuffFull(UART0);
FL_UART_EnableIT_RXBuffFull(UART0);
// FL_UART_ClearFlag_TXShiftBuffEmpty(UART0); //没用到发送中断,注释掉了
// FL_UART_EnableIT_TXShiftBuffEmpty(UART0); //没用到发送中断,注释掉了
}
void MF_NVIC_Init(void)
{
FL_NVIC_ConfigTypeDef InterruptConfigStruct;
InterruptConfigStruct.preemptPriority = 0x02;
FL_NVIC_Init(&InterruptConfigStruct, UART0_IRQn);
}
void MF_Config_Init(void)
{
/* Initial UART0 */
MF_UART0_Init();
MF_UART0_Interrupt_Init();
/* Initial NVIC */
MF_NVIC_Init();
}
记得在main文件调用MF_Config_Init()函数啊
在工程中添加uart.c:
#include "main.h"
#include "uart.h"
uint8_t rx_flag;
uint8_t tmp08;
void UART0_IRQHandler(void)
{
uint32_t UART0RXBuffFullIT = 0;
uint32_t UART0RXBuffFullFlag = 0;
UART0RXBuffFullIT = FL_UART_IsEnabledIT_RXBuffFull(UART0);
UART0RXBuffFullFlag = FL_UART_IsActiveFlag_RXBuffFull(UART0);
//接收中断处理
if((UART0RXBuffFullIT == 0x01UL) && (UART0RXBuffFullFlag == 0x01UL))
{
//中断接收
tmp08 = FL_UART_ReadRXBuff(UART0); //接收中断标志可通过读取rxreg寄存器清除
rx_flag = 1; //接收标志置1
}
}
void UART0_SendChar(u8 ui)
{
FL_UART_WriteTXBuff(UART0, ui); //将发送数据写入发送寄存器
while(FL_SET != FL_UART_IsActiveFlag_TXBuffEmpty(UART0));
}
void UART0_SendNdata(u16 len,u8 *buf)
{
while(len--)
{
UART0_SendChar(*buf);
buf++;
}
}
void UART_RxProcessing(void){
if(rx_flag){ //如果接收到数据
rx_flag=0; //置0
UART0_SendChar(tmp08); //调用串口发送回去
}
}
最后在main文件的while循环里调用UART_RxProcessing()函数,串口收到一个字节的就发送回去