usart.h
#ifndef __USART_H
#define __USART_H
#include "ht32f5xxxx_usart.h"
#define USART_GPIO_GROUP (GPIO_PA)
#define USART_TX_PIN (GPIO_PIN_4)
#define USART_RX_PIN (GPIO_PIN_5)
#define USART_AFIO_MODE (AFIO_FUN_USART_UART)
//模式设置默认模式:AFIO_MODE_DEFAULT, AFIO_MODE_1~15 对应模式1~15
#define COM1_PORT (HT_USART1)
#define COM1_IRQn (USART0_IRQn)
void USART_Configuration(void);
void COM1_IRQHandler(void);
void Usart_Sendbyte(HT_USART_TypeDef* USARTx, u8 data);
void Usart_SendArray(HT_USART_TypeDef* USARTx, u8 *array,u8 num);
void Usart_SendStr(HT_USART_TypeDef* USARTx, uint8_t *str);
void USART_Tx(const char* TxBuffer, u32 length);
void USART_Rx(const char* RxBuffer, u32 length);
#endif
usart.c
#include "usart.h"
#include "ht32f5xxxx_gpio.h"
#include "ht32_board.h"
#include "ht32_board_config.h"
/**************************实现函数*******************************************
*******************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
CKCU_PeripClockConfig_TypeDef CKCUClock= {{0}};
CKCUClock.Bit.AFIO = 1;
COM1_CLK(CKCUClock) = 1; //开启时钟
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(USART_GPIO_GROUP, USART_TX_PIN, AFIO_FUN_USART_UART);
AFIO_GPxConfig(USART_GPIO_GROUP, USART_RX_PIN, AFIO_FUN_USART_UART);
/*
波特率 115200
长度: 8bits
停止位 1位
校验位 无
模式 正常模式
*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WORDLENGTH_8B;
USART_InitStructure.USART_StopBits = USART_STOPBITS_1;
USART_InitStructure.USART_Parity = USART_PARITY_NO;
USART_InitStructure.USART_Mode = USART_MODE_NORMAL;
USART_Init(COM1_PORT, &USART_InitStructure);
// 使能 COM1_PORT 发送和接收
USART_TxCmd(COM1_PORT, ENABLE);
USART_RxCmd(COM1_PORT, ENABLE);
//中断设置
NVIC_EnableIRQ(COM1_IRQn);
USART_IntConfig(COM1_PORT, USART_FLAG_RXDR , ENABLE);
USART_IntConfig(COM1_PORT, USART_FLAG_TXDE , ENABLE);
/* 设置FIFO接收等级 */
USART_RXTLConfig(COM1_PORT, USART_RXTL_04);
}
/*************************中断服务函数*******************************************/
void COM1_IRQHandler(void)
{
u8 data;
Usart_SendStr(COM1_PORT, "0\r\n");
if( USART_GetFlagStatus(COM1_PORT, USART_FLAG_RXDR ) ) //接收中断
{
data = USART_ReceiveData(COM1_PORT); //读取接收到的数据
printf("data = %c\n",data);
//将收到的数据发送回电脑
}
}
/*********************************************************************/
void USART_Tx(const char* TxBuffer, u32 length)
{
int i;
for (i = 0; i < length; i++)
{
while (!USART_GetFlagStatus(COM1_PORT, USART_FLAG_TXC));
USART_SendData(COM1_PORT, TxBuffer[i]);
}
}
/**************************发送一个字节*****************************************/
void Usart_Sendbyte(HT_USART_TypeDef* USARTx, u8 data)
{
USART_SendData(USARTx, data);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
}
/***************************发送数组******************************************/
void Usart_SendArray(HT_USART_TypeDef* USARTx, u8 *array,u8 num)
{
u8 i;
for( i = 0;i < num;i++)
{
Usart_Sendbyte(USARTx,*array);
array++;
}
}
/**************************发送字符串***************************************/
void Usart_SendStr(HT_USART_TypeDef* USARTx, uint8_t *str)
{
uint8_t i;
for(i = 0;str[i] != '\0';i++)
{
Usart_Sendbyte(USARTx,str[i]);
}
}
这是使用HT32F52352的USART通信的代码。USART是一种串行通信接口,可以用于发送和接收数据。下面是代码的详细解释:
-
USART_Configuration函数:该函数用于配置USART通信接口。首先,通过CKCU_PeripClockConfig函数打开AFIO和USART的时钟。然后,使用AFIO_GPxConfig函数配置USART的TX和RX引脚。接下来,使用USART_Init函数配置USART的波特率、数据位、停止位、校验位和工作模式。最后,使用USART_TxCmd和USART_RxCmd函数使能USART的发送和接收功能,并使用USART_IntConfig函数使能USART的中断。
-
COM1_IRQHandler函数:该函数是USART的中断处理程序,用于处理接收数据的中断。如果USART接收到数据,则将数据读取并打印到终端上。
-
USART_Tx函数:该函数用于发送数据。在该函数中,使用USART_GetFlagStatus函数检查USART是否准备好发送数据。如果USART准备好了,就使用USART_SendData函数发送一个字节的数据。
-
Usart_Sendbyte函数:该函数用于发送一个字节的数据。在该函数中,使用USART_SendData函数发送数据,并使用USART_GetFlagStatus函数检查USART是否准备好发送下一个字节的数据。
-
Usart_SendArray函数:该函数用于发送一个字节数组的数据。在该函数中,使用Usart_Sendbyte函数发送每个字节的数据。
-
Usart_SendStr函数:该函数用于发送一个字符串。在该函数中,使用Usart_Sendbyte函数发送每个字符的数据。
该代码是一个基本的USART通信样例代码,可以让您在HT32F52352上使用USART通信接口。
ht32_board_config.h
/*********************************************************************************************************//**
* @file USART/Interrupt/ht32_board_config.h
* @version $Rev:: 6283 $
* @date $Date:: 2022-10-14 #$
* @brief The header file of board configuration.
*************************************************************************************************************
* @attention
*
* Firmware Disclaimer Information
*
* 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
* proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
* other intellectual property laws.
*
* 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
* other than HOLTEK and the customer.
*
* 3. The program technical documentation, including the code, is provided "as is" and for customer reference
* only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
* the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
* the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
*
* <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
************************************************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------------------------------------*/
#ifndef __HT32_BOARD_CONFIG_H
#define __HT32_BOARD_CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
/* Settings ------------------------------------------------------------------------------------------------*/
#if defined(USE_HT32F52230_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART0
#endif
#if defined(USE_HT32F52241_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F52253_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN USART1
#endif
#if defined(USE_HT32F52341_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F52352_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN USART1
#endif
#if defined(USE_HT32F0008_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART0
#endif
#if defined(USE_HT32F50030_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F50230_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F50241_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F52354_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F0006_DVB)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 2
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 3
#define HTCFG_UART_IPN USART0
#endif
#if defined(USE_HT32F57341_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN USART0
#endif
#if defined(USE_HT32F57352_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN USART0
#endif
#if defined(USE_HT32F52367_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN USART1
#endif
#if defined(USE_BM53A367A_DVB)
#define _HTCFG_UART_TX_GPIOX A // D1
#define _HTCFG_UART_TX_GPION 8
#define _HTCFG_UART_RX_GPIOX A // D0
#define _HTCFG_UART_RX_GPION 10
#define HTCFG_UART_IPN USART0
#endif
#if defined(USE_HT32F50343_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F61141_SK) //PRELIMINARY_NOT_TEST
#define _HTCFG_UART_TX_GPIOX B
#define _HTCFG_UART_TX_GPION 0
#define _HTCFG_UART_RX_GPIOX B
#define _HTCFG_UART_RX_GPION 1
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F65240_DVB)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 3
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 1
#define HTCFG_UART_IPN USART0
#endif
#if defined(USE_HT32F65240_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART0
#endif
#if defined(USE_HT32F65232_SK) //PRELIMINARY_NOT_TEST
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART0
#endif
#if defined(USE_HT32F61355_SK) || defined(USE_HT32F61356_SK) || defined(USE_HT32F61357_SK)
#define _HTCFG_UART_TX_GPIOX C
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX C
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART0
#endif
#if defined(USE_HT32F54241_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN UART1
#endif
#if defined(USE_HT32F54253_SK)
#define _HTCFG_UART_TX_GPIOX A
#define _HTCFG_UART_TX_GPION 4
#define _HTCFG_UART_RX_GPIOX A
#define _HTCFG_UART_RX_GPION 5
#define HTCFG_UART_IPN USART1
#endif
#define HTCFG_UART_TX_GPIO_ID STRCAT2(GPIO_P, _HTCFG_UART_TX_GPIOX)
#define HTCFG_UART_RX_GPIO_ID STRCAT2(GPIO_P, _HTCFG_UART_RX_GPIOX)
#define HTCFG_UART_TX_AFIO_PIN STRCAT2(AFIO_PIN_, _HTCFG_UART_TX_GPION)
#define HTCFG_UART_RX_AFIO_PIN STRCAT2(AFIO_PIN_, _HTCFG_UART_RX_GPION)
#define HTCFG_UART_PORT STRCAT2(HT_, HTCFG_UART_IPN)
#define HTCFG_UART_IRQn STRCAT2(HTCFG_UART_IPN, _IRQn)
#define HTCFG_UART_IRQHandler STRCAT2(HTCFG_UART_IPN, _IRQHandler)
#define HTCFG_UART_RX_GPIO_CLK STRCAT2(P, _HTCFG_UART_RX_GPIOX)
#define HTCFG_UART_RX_GPIO_PORT STRCAT2(HT_GPIO, _HTCFG_UART_RX_GPIOX)
#define HTCFG_UART_RX_GPIO_PIN STRCAT2(GPIO_PIN_, _HTCFG_UART_RX_GPION)
#ifdef __cplusplus
}
#endif
#endif
main.c
int main(void)
{
GPIO_Configuration();
USART_Configuration();
while (1)
{
char ss = USART_ReceiveData(COM1_PORT);
char a = 'A',b = 'B';
if (ss == a)
{
Usart_SendStr(COM1_PORT, "a\r\n");
delay_ms(1000);
}
if (ss == b)
{
Usart_SendStr(COM1_PORT, "b\r\n");
delay_ms(1000);
}
}
}
在这段代码中,我们首先调用GPIO_Configuration和USART_Configuration函数来配置GPIO和USART通信接口。然后,在主循环中,我们使用USART_ReceiveData函数从USART接口中读取一个字节的数据,并将其存储在ss中。
接下来,我们定义了两个字符a和b,这些字符将用作对接收的字节进行比较的标志。
在接下来的代码中,我们使用if条件语句来检查接收到的字节是否等于字符a或b。如果是,我们使用Usart_SendStr函数向USART接口发送字符串“a”或“b”,并使用delay_ms函数等待1秒钟。
这段代码的作用是:当接收到字符‘A’时,向USART接口发送字符串“a”并等待1秒钟;当接收到字符‘B’时,向USART接口发送字符串“b”并等待1秒钟。
main.c(全)
/*********************************************************************************************************//**
* @file GPIO/Output/main.c
* @version $Rev:: 5805 $
* @date $Date:: 2022-04-12 #$
* @brief Main program.
*************************************************************************************************************
* @attention
*
* Firmware Disclaimer Information
*
* 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
* proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
* other intellectual property laws.
*
* 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
* other than HOLTEK and the customer.
*
* 3. The program technical documentation, including the code, is provided "as is" and for customer reference
* only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
* the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
* the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
*
* <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
************************************************************************************************************/
/* Includes ------------------------------------------------------------------------------------------------*/
#include "ht32.h"
#include "ht32_board.h"
#include "usart.h"
#include "delay.h"
#include<stdio.h>
#include<string.h>
//**************************************************************************************//
//**************************************************************************************//
/** @addtogroup HT32_Series_Peripheral_Examples HT32 Peripheral Examples
* @{
*/
/** @addtogroup GPIO_Examples GPIO
* @{
*/
/** @addtogroup Output
* @{
*/
/* Private function prototypes -----------------------------------------------------------------------------*/
void GPIO_Configuration(void);
void GPIO_OutputBit(void);
void GPIO_OutputData(void);
static void __Delay(u32 count);
/* Global functions ----------------------------------------------------------------------------------------*/
/*********************************************************************************************************//**
* @brief Main program.
* @retval None
***********************************************************************************************************/
int main(void)
{
GPIO_Configuration();
USART_Configuration();
while (1)
{
char ss = USART_ReceiveData(COM1_PORT);
char a = 'A',b = 'B';
if (ss == a)
{
Usart_SendStr(COM1_PORT, "a\r\n");
delay_ms(1000);
}
if (ss == b)
{
Usart_SendStr(COM1_PORT, "b\r\n");
delay_ms(1000);
}
}
}
/*********************************************************************************************************//**
* @brief Configure the GPIO as output mode.
* @retval None
***********************************************************************************************************/
void GPIO_Configuration(void)
{
{ /* Enable peripheral clock */
CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.PB = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
}
{ /* Configure GPIO as output mode */
/* Configure AFIO mode as GPIO */
AFIO_GPxConfig(GPIO_PB, AFIO_PIN_1, AFIO_FUN_GPIO);
/* Configure GPIO pull resistor */
GPIO_PullResistorConfig(HT_GPIOB, GPIO_PIN_1, GPIO_PR_DOWN);
/* Default value RESET/SET */
GPIO_WriteOutBits(HT_GPIOB, GPIO_PIN_1, RESET);
/* Configure GPIO direction as output */
GPIO_DirectionConfig(HT_GPIOB, GPIO_PIN_1, GPIO_DIR_OUT);
}
}
/*********************************************************************************************************//**
* @brief GPIO Output bit test
* @retval None
***********************************************************************************************************/
void GPIO_OutputBit(void)
{
GPIO_SetOutBits(HT_GPIOB, GPIO_PIN_1); // GPIO = HIGH
__Delay(500000);
GPIO_ClearOutBits(HT_GPIOB, GPIO_PIN_1); // GPIO = LOW
__Delay(5000000);
}
/*********************************************************************************************************//**
* @brief GPIO Output data test
* @retval None
***********************************************************************************************************/
void GPIO_OutputData(void)
{
u16 uOutputData;
uOutputData = GPIO_ReadOutData(HT_GPIOB);
uOutputData |= GPIO_PIN_1; // GPIO = HIGH
GPIO_WriteOutData(HT_GPIOB, uOutputData);
__Delay(500000);
uOutputData = GPIO_ReadOutData(HT_GPIOB);
uOutputData &= ~(GPIO_PIN_1); // GPIO = LOW
GPIO_WriteOutData(HT_GPIOB, uOutputData);
__Delay(5000000);
}
#if (HT32_LIB_DEBUG == 1)
/*********************************************************************************************************//**
* @brief Report both the error name of the source file and the source line number.
* @param filename: pointer to the source file name.
* @param uline: error line source number.
* @retval None
***********************************************************************************************************/
void assert_error(u8* filename, u32 uline)
{
/*
This function is called by IP library that the invalid parameters has been passed to the library API.
Debug message can be added here.
Example: printf("Parameter Error: file %s on line %d\r\n", filename, uline);
*/
while (1)
{
}
}
#endif
/* Private functions ---------------------------------------------------------------------------------------*/
/*********************************************************************************************************//**
* @brief delay function
* @param count: delay count for loop
* @retval None
***********************************************************************************************************/
static void __Delay(u32 count)
{
while (count--)
{
__NOP(); // Prevent delay loop be optimized
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
在上述中还需要延时函数,可使用下例:
https://blog.csdn.net/yandadzf/article/details/130831465?spm=1001.2014.3001.5501
但在通常情况下可能会使用两个串口
附上串口二的配置
#include "ht32f5xxxx_01.h"
#include "ht32f5xxxx_gpio.h"
#include "ht32f5xxxx_rcc.h"
#include "ht32f5xxxx_usart.h"
// 定义USART2的GPIO引脚和组
#define USART2_TX_PIN GPIO_PIN_0
#define USART2_RX_PIN GPIO_PIN_1
#define USART2_GPIO_GROUP GPIOB
#define USART2_AFIO_FUN AFIO_FUN_USART_UART
// 定义USART2的时钟
#define COM2_CLK(CKCUClock) (CKCUClock.Bit.USART2)
// 定义串口的波特率
#define USART_BAUDRATE 115200
// USART2的中断优先级
#define USART2_IRQ_PREEMPTION_PRIORITY 2
#define USART2_IRQ_SUB_PRIORITY 0
// USART2的接收缓冲区大小
#define USART2_RX_BUFFER_SIZE 256
// USART2的接收缓冲区
volatile uint8_t usart2RxBuffer[USART2_RX_BUFFER_SIZE];
volatile uint16_t usart2RxBufferIndex = 0;
// USART2的初始化函数
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
NVIC_InitTypeDef NVIC_InitStructure;
// 打开GPIOB和USART2的时钟
CKCUClock.Bit.AFIO = 1;
COM2_CLK(CKCUClock) = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
// 配置USART2的GPIO引脚
AFIO_GPxConfig(USART2_GPIO_GROUP, USART2_TX_PIN, USART2_AFIO_FUN);
AFIO_GPxConfig(USART2_GPIO_GROUP, USART2_RX_PIN, USART2_AFIO_FUN);
// 配置USART2的波特率、数据位、停止位、校验位和工作模式
USART_InitStructure.USART_BaudRate = USART_BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WORDLENGTH_8B;
USART_InitStructure.USART_Stop
Bits = USART_STOPBITS_1; USART_InitStructure.USART_Parity = USART_PARITY_NO;
USART_InitStructure.USART_Mode = USART_MODE_NORMAL; USART_Init(USART2,
&USART_InitStructure);
// 使能USART2的发送和接收功能 USART_TxCmd(USART2, ENABLE); USART_RxCmd(USART2, ENABLE);
// 配置USART2的中断 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USART2_IRQ_PREEMPTION_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = USART2_IRQ_SUB_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
// 使能USART2的接收中断 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); }
// USART2的中断处理函数 void USART2_IRQHandler(void) { if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { // 读取接收到的数据,存储到接收缓冲区中 usart2RxBuffer[usart2RxBufferIndex] = USART_ReceiveData(USART2);
// 更新接收缓冲区的索引
usart2RxBufferIndex++;
// 如果接收缓冲区已满,则重置索引
if (usart2RxBufferIndex >= USART2_RX_BUFFER_SIZE)
{
usart2RxBufferIndex = 0;
}
} }
int main(void) { // 配置USART2 USART2_Configuration();
while (1) { // 在主循环中,什么都不做 } }
这段代码是用于初始化HT32F52352的USART2串口通信功能,和设置接收中断处理函数。
在函数USART2_Configuration中,首先使用CKCU_PeripClockConfig函数打开AFIO和USART2的时钟,然后使用AFIO_GPxConfig函数配置USART2的TX和RX引脚。之后,使用USART_Init函数配置USART2的波特率、数据位、停止位、校验位和工作模式,并使能USART2的发送和接收功能。接着使用NVIC_Init函数配置USART2的中断,并使能USART2的接收中断。
在USART2_IRQHandler函数中,当USART2接收到数据时,该函数会被调用。在函数中,我们通过USART_GetITStatus函数检查接收中断是否已经被触发。如果接收到数据,则调用USART_ReceiveData函数读取数据,并将其存储到接收缓冲区中。接着更新接收缓冲区索引,并检查缓冲区是否已满。如果缓冲区已满,则重置索引。
在主函数中,我们调用USART2_Configuration函数来初始化USART2通信接口,并进入死循环等待接收中断触发。
总的来说,这段代码的作用是使HT32F52352能够使用USART2与其他设备进行串口通信。接收中断处理函数可以在接收到数据时将数据存储在接收缓冲区中。
需要注意的是,HT32F52352的串口1可以直接连接到电脑上进行通信。但是,在连接时需要更改跳线帽的设置,以便正确设置串口的连接方式。在实际使用中,一定要注意检查并更改跳线帽的设置。
需要修改的跳线帽位置为图中绿色框图 红色框图与电脑相连
HT32F52352与电脑通信时候连接AB
HT32F52352与其他设备通信时候连接BC
注意:烧录程序后,需给单片机断电后,再供电才能实现功能,或者点复位按钮