需要购买USB转串口芯片,安装串口驱动
串口检测软件
USART0.h
#ifndef USART0_H_ #define USART0_H_ #include "system_init.h" #include <stdint.h> #define STD_OFF (0U) #define STD_ON (1U) #define POLLING (0U) #define INTERRUPT (1U) #define ASYNCH (0U) #define SYNCH (1U) #define USART_TX_PROCESSING INTERRUPT #define USART_RX_PROCESSING INTERRUPT #define USART_TX_DMA STD_ON #define USART_TX_DMA STD_ON #define USART_WORKING_MODE ASYNC /** * @brief :初始化USART0的GPIO * @retval :PA3 -- USART0_TX * PA2 -- USART0_RX */ void Usart_Gpio_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; //将PA3重映射模式配置为TX引脚 GPIO_InitStructure.m_Mode = GPIO_MODE_RMP; GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP; GPIO_InitStructure.m_PullDown = GPIO_NOPULL; GPIO_InitStructure.m_PullUp = GPIO_NOPULL; GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED; GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_3; GPIO_Configuration(GPIOA_SFR, &GPIO_InitStructure); //将PA2重映射模式配置为RX引脚,配置为上拉模式 GPIO_InitStructure.m_PullUp = GPIO_PULLUP; GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_7; GPIO_Configuration(GPIOE_SFR, &GPIO_InitStructure); /* 配置PA3重新映射功能至AF3 */ GPIO_Pin_RMP_Config(GPIOA_SFR, GPIO_Pin_Num_3, GPIO_RMP_AF3); /* 配置PA2重新映射功能至AF3 */ GPIO_Pin_RMP_Config(GPIOE_SFR, GPIO_Pin_Num_7, GPIO_RMP_AF3); } /** * @brief :USART发送数据 * @param in :USARTx :指向USART内存结构的指针,值为 * USART0_SFR * USART1_SFR * USART2_SFR * USART5_SFR * Usart_Sdu: Data : 指向发送数据的指针 * Length : 传输数据长度 */ void USART_Send(USART_SFRmap *USARTx, uint8_t *Databuf, uint32_t length) { for (uint32_t i = 0; i < length; i++) USART_SendData(USARTx, Databuf[i]);//串行传输 } /** * @brief :配置USART模式 * @param in :USARTx:指向USART内存结构的指针,值为 * USART0_SFR * USART1_SFR * USART2_SFR * USART5_SFR */ void USART_Mode_Config(USART_SFRmap *USARTx) { USART_InitTypeDef USART_InitStructure; USART_Struct_Init(&USART_InitStructure); //将Usart设置为异步模式 USART_InitStructure.m_Mode = USART_MODE_FULLDUPLEXASY; USART_InitStructure.m_TransferDir = USART_DIRECTION_FULL_DUPLEX; USART_InitStructure.m_WordLength = USART_WORDLENGTH_8B; USART_InitStructure.m_StopBits = USART_STOPBITS_1; //设置波特率为115200 USART_InitStructure.m_BaudRateBRCKS = USART_CLK_HFCLK; USART_InitStructure.m_BaudRateInteger = 8; USART_InitStructure.m_BaudRateNumerator = 1; USART_InitStructure.m_BaudRateDenominator = 13; //使能send USART_Reset(USARTx); USART_Configuration(USARTx, &USART_InitStructure); USART_Passageway_Select_Config(USARTx, USART_U7816R_PASSAGEWAY_TX0); USART_RESHD_Enable(USARTx, TRUE); USART_Transmit_Data_Enable(USARTx, TRUE); USART_Cmd(USARTx, TRUE); } /** * @brief :配置USART初始化 * @param in : * USARTx :指向USART内存结构的指针 USART0_SFR * USART1_SFR * USART2_SFR * USART5_SFR * Peripheral :中断矢量编号 * INT_USART0 * INT_USART1 * INT_USART2 * INT_USART5 */ void USART_Int_Config(USART_SFRmap *USARTx, InterruptIndex Peripheral) { USART_Receive_Data_Enable(USARTx, TRUE);//重置USARTx发送BUF中断位 USART_RDR_INT_Enable(USARTx, TRUE);//USARTx RDR中断启用 USART_Receive_Idle_Frame_Config(USARTx, TRUE); USART_IDLE_INT_Enable(USARTx, TRUE);//配置USART以接收空闲帧中断标志生成模式 INT_Interrupt_Enable(Peripheral, TRUE);//中断启用外围或内核中断向量 } #endif /* USART0_H_ */
设置中断函数
kf_it.c
#include "system_init.h" //Usart0接收数据计数 volatile uint32_t TransmitCount=0; extern uint8_t UsartTxBuffer[]; extern uint8_t UsartRxBuffer[]; void __attribute__((interrupt)) _USART0_exception (void) { static uint32_t ReceiveCount = 0; //如果获取USART数据就绪中断标志的状态 if(USART_Get_Receive_BUFR_Ready_Flag(USART0_SFR)) UsartRxBuffer[ReceiveCount++] = USART_ReceiveData(USART0_SFR); //如果获取接收空闲帧的USART中断标志 if (USART_Get_Receive_Frame_Idel_Flag(USART0_SFR)) { USART_Clear_Idle_INT_Flag(USART0_SFR);//清除空闲中断标志 TransmitCount = ReceiveCount;//获取接收的数据的长度 ReceiveCount = 0; } }
设置主函数,先向PC发送10个hellowirld,然后接收PC发送的数据,并再发送给PC
main.c
#include"system_init.h" #include"kf32a156_gpio.h" #include"USART0.h" uint8_t Usart_Send_Sdu[] = {"HelloWorld"}; //Usart发送数据数组和接收数据数组 uint8_t UsartTxBuffer[100]; uint8_t UsartRxBuffer[100]; extern volatile uint32_t TransmitCount ; int main() { SystemInit(120);///<初始化系统时钟为120M systick_delay_init(120);///<设置SysTick定时器作为延迟功能,输入频率为120M Usart_Gpio_Init();//设置Usart的Tx和Rx口——PA3为TX,PA2为RX USART_Mode_Config(USART0_SFR);//配置USART模式 USART_Int_Config(USART0_SFR,INT_USART0);//配置USART初始化 INT_Priority_Group_Config(INT_PRIORITY_GROUP_3VS1);//配置中断优先级组,默认值为3VS1 INT_All_Enable(TRUE);//全局屏蔽中断启用位 for(int i=0;i<10;i++){ USART_Send(USART0_SFR, Usart_Send_Sdu, sizeof(Usart_Send_Sdu)); systick_delay_ms(250); } while(1){ //如果接收到PC数据,将数据发回去 if (TransmitCount) { memcpy(UsartTxBuffer, UsartRxBuffer, TransmitCount);//将数据从UsartRxBuffer复制到TransmitCount USART_Send(USART0_SFR, UsartTxBuffer, TransmitCount); TransmitCount = 0;//清除传输计数 } } }
标签:KungFu32A156MQT,USART,USARTx,SFR,PC,InitStructure,串口,GPIO,USART0 From: https://www.cnblogs.com/LPworld/p/17095572.html