文章目录[隐藏]
无DMA标准库版
1.串口通信初始化代码(USART7)
1. .c文件中的初始化函数
/**
* @breif 串口初始化函数
*/
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(USART_RX_GPIO_CLK|USART_TX_GPIO_CLK,ENABLE);
/* 使能 USART 时钟 */
USART_PeriphClockCmd(USART_CLK, ENABLE);
/* GPIO初始化 */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* 配置Tx引脚为复用功能 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = USART_TX_PIN ;
GPIO_Init(USART_TX_GPIO_PORT, &GPIO_InitStructure);//USARTE
/* 配置Rx引脚为复用功能 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = USART_RX_PIN;
GPIO_Init(USART_RX_GPIO_PORT, &GPIO_InitStructure);
/* 连接 PXx 到 USARTx_Tx*/
GPIO_PinAFConfig(USART_RX_GPIO_PORT,USART_RX_SOURCE,USART_RX_AF);
/* 连接 PXx 到 USARTx__Rx*/
GPIO_PinAFConfig(USART_TX_GPIO_PORT,USART_TX_SOURCE,USART_TX_AF);
/* 配置串USART 模式 */
/* 波特率设置:USART_BAUDRATE */
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模式控制:同时使能接收和发送 */
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* 完成USART初始化配置 */
USART_Init(USART_NUM, &USART_InitStructure);
/* 使能串口 */
USART_Cmd(USART_NUM, ENABLE);
USART_ITConfig(USART_NUM, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART_IRQ ;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
2. .h文件中的宏定义
//引脚定义
/*******************************************************/
#define USART_NUM UART7
#define USART_CLK RCC_APB1Periph_UART7
#define USART_PeriphClockCmd RCC_APB1PeriphClockCmd
#define USART_BAUDRATE 115200 //串口波特率
#define USART_RX_GPIO_PORT GPIOE
#define USART_RX_GPIO_CLK RCC_AHB1Periph_GPIOE
#define USART_RX_PIN GPIO_Pin_7
#define USART_RX_AF GPIO_AF_UART7
#define USART_RX_SOURCE GPIO_PinSource7
#define USART_TX_GPIO_PORT GPIOE
#define USART_TX_GPIO_CLK RCC_AHB1Periph_GPIOE
#define USART_TX_PIN GPIO_Pin_8
#define USART_TX_AF GPIO_AF_UART7
#define USART_TX_SOURCE GPIO_PinSource8
#define USART_IRQHandler UART7_IRQHandler
#define USART_IRQ UART7_IRQn
/************************************************************/
虽然但是宏定义改一下更好
2.串口通信发送函数
union Send_Data{int data[2];char char_data[8];} send_data;//这里是用union作为数据的存储方式
void send_data(void)
{
//定义发送的数据
send_data.data[0] = (int)(Kinematics.yaw.actual_angle*100);
send_data.data[1] = (int)(Kinematics.pitch.actual_angle*100);
//printf("发送的数据为:");
Usart_SendByte(USART_NUM,'*');//开始标志位
//发送数据主体
for(int i=0;i<8;i++)
{
Usart_SendByte( USART_NUM,send_data.char_data[i]);
}
Usart_SendByte(USART_NUM,';');//结束标志位
}
3.串口通信接收函数(中断函数)
static char ucTemp;
union Recive_data{int sint_data[2];char char_data[8];}resive_data;
void USART_IRQHandler(void)
{
static int i=0;
static uint8_t start_recive_flag=0;
if(USART_GetITStatus(USART_NUM, USART_IT_RXNE))//接收中断,接收区非空,表示已接收到数据
{
ucTemp = USART_ReceiveData(USART_NUM);
//主要数据接收
if(start_recive_flag == 1)
{
resive_data.char_data[i] = ucTemp;
i++;
}
if(ucTemp == '*') //开始标志位
{
start_recive_flag=1;
}
else if(ucTemp == ';') //结束标志位
{
//printf("接收成功");
//云台模式
//control_mode = resive_data.sint_data[0];
//底盘速度
//Kinematics.wheel1.target_speed.linear_vel = resive_data.sint_data[1];
//Kinematics.wheel2.target_speed.linear_vel = resive_data.sint_data[1];
//yaw
Kinematics.yaw.target_angle = ((float)(resive_data.sint_data[0]))/100;
//pitch
Kinematics.pitch.target_angle = ((float)(resive_data.sint_data[1]))/100;
//波弹轮
//Kinematics.trigger.target_angular = (float)(resive_data.sint_data[4]);
//摩擦轮
//Kinematics.fric.target_angular = (float)(resive_data.sint_data[5]);
start_recive_flag=0;
i = 0;
}
}
}
无DMAhal库版
1.CubeMX配置
1.串口的设置
注意:如果直接配置UART7时,但是默认GPIO口不是PE7,8时,可以直接在串口设置中点击设置开启UART7
2.A板的时钟设置
2.串口初始化代码
(默认配置完成,不用管他)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @attention
*
* Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
UART_HandleTypeDef huart7;
/* UART7 init function */
void MX_UART7_Init(void)
{
/* USER CODE BEGIN UART7_Init 0 */
/* USER CODE END UART7_Init 0 */
/* USER CODE BEGIN UART7_Init 1 */
/* USER CODE END UART7_Init 1 */
huart7.Instance = UART7;
huart7.Init.BaudRate = 115200;
huart7.Init.WordLength = UART_WORDLENGTH_8B;
huart7.Init.StopBits = UART_STOPBITS_1;
huart7.Init.Parity = UART_PARITY_NONE;
huart7.Init.Mode = UART_MODE_TX_RX;
huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart7.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart7) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART7_Init 2 */
/* USER CODE END UART7_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==UART7)
{
/* USER CODE BEGIN UART7_MspInit 0 */
/* USER CODE END UART7_MspInit 0 */
/* UART7 clock enable */
__HAL_RCC_UART7_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
/**UART7 GPIO Configuration
PE8 ------> UART7_TX
PE7 ------> UART7_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_UART7;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/* UART7 interrupt Init */
HAL_NVIC_SetPriority(UART7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART7_IRQn);
/* USER CODE BEGIN UART7_MspInit 1 */
/* USER CODE END UART7_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==UART7)
{
/* USER CODE BEGIN UART7_MspDeInit 0 */
/* USER CODE END UART7_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_UART7_CLK_DISABLE();
/**UART7 GPIO Configuration
PE8 ------> UART7_TX
PE7 ------> UART7_RX
*/
HAL_GPIO_DeInit(GPIOE, GPIO_PIN_8|GPIO_PIN_7);
/* UART7 interrupt Deinit */
HAL_NVIC_DisableIRQ(UART7_IRQn);
/* USER CODE BEGIN UART7_MspDeInit 1 */
/* USER CODE END UART7_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
c
/* USER CODE END 1 */
3.发送和接收(中断)函数
具体发送和接收机制和标准库是一样的
1.发送函数
union Send_Data{int data[2];char char_data[8];} send_data;
uint8_t send_data_quan[10];
void Send_data()
{
send_data.data[0] = int_get[0];
send_data.data[1] = int_get[1];
send_data_quan[0] = '*';
for(int i =1;i<9;i++)
{
send_data_quan[i] = send_data.char_data[i-1];
}
send_data_quan[9] = ';';
//printf("发送的数据为:");c
HAL_UART_Transmit(&huart7,(uint8_t*)send_data_quan,10,1000);//发送消息
}
2.接收函数(中断)
1.代码
union Get_Data{int data[2];char char_data[8];} Get_data;
int int_get[2];
int iii=-1;
void UART7_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart7);
if(__HAL_UART_GET_FLAG( &huart7, UART_FLAG_RXNE ) != RESET)
{
uint8_t ch=(uint16_t) READ_REG(huart7.Instance->DR);
int_get[0] = Get_data.data[0];
int_get[1] = Get_data.data[1];
if(iii !=-1)
{
Get_data.char_data[iii] = ch;
iii++;
}
if(ch=='*')//开始位
{
iii = 0;
}
else if(ch == ';')//结束位
{
iii = -1;
}
__HAL_UART_CLEAR_FLAG(&huart7,UART_FLAG_RXNE);
}
__HAL_UART_ENABLE_IT(&huart7, UART_IT_ERR);
__HAL_UART_ENABLE_IT(&huart7,UART_IT_RXNE);
/* USER CODE END UART7_IRQn 1 */
}
2.注意事项
如果你进不去中断函数,可以参考一下这个博客:
(1条消息) 解决CUBEMX配置生成的UART无法进入中断的问题_arenascat的博客-CSDN博客
4.串口的初始化
我当时是直接把发送写在主函数里面的,主函数的代码如下
int main(void)
{
HAL_Init();
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);
SystemClock_Config();
MX_GPIO_Init();
MX_UART7_Init();
HAL_UART_Transmit_IT(&huart7 ,(uint8_t*)aTxStartMessages,sizeof(aTxStartMessages)); //sizeof()可读取目标长度
__HAL_UART_ENABLE_IT(&huart7, UART_IT_ERR);
//主函数一直发送
while (1)
{
Send_data();
HAL_Delay(10);
}
}
版权声明:本文为CSDN博主「九柳」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jiu_liu/article/details/122641778
无DMA标准库版
1.串口通信初始化代码(USART7)
1. .c文件中的初始化函数
/**
* @breif 串口初始化函数
*/
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(USART_RX_GPIO_CLK|USART_TX_GPIO_CLK,ENABLE);
/* 使能 USART 时钟 */
USART_PeriphClockCmd(USART_CLK, ENABLE);
/* GPIO初始化 */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* 配置Tx引脚为复用功能 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = USART_TX_PIN ;
GPIO_Init(USART_TX_GPIO_PORT, &GPIO_InitStructure);//USARTE
/* 配置Rx引脚为复用功能 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = USART_RX_PIN;
GPIO_Init(USART_RX_GPIO_PORT, &GPIO_InitStructure);
/* 连接 PXx 到 USARTx_Tx*/
GPIO_PinAFConfig(USART_RX_GPIO_PORT,USART_RX_SOURCE,USART_RX_AF);
/* 连接 PXx 到 USARTx__Rx*/
GPIO_PinAFConfig(USART_TX_GPIO_PORT,USART_TX_SOURCE,USART_TX_AF);
/* 配置串USART 模式 */
/* 波特率设置:USART_BAUDRATE */
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模式控制:同时使能接收和发送 */
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* 完成USART初始化配置 */
USART_Init(USART_NUM, &USART_InitStructure);
/* 使能串口 */
USART_Cmd(USART_NUM, ENABLE);
USART_ITConfig(USART_NUM, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART_IRQ ;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
2. .h文件中的宏定义
//引脚定义
/*******************************************************/
#define USART_NUM UART7
#define USART_CLK RCC_APB1Periph_UART7
#define USART_PeriphClockCmd RCC_APB1PeriphClockCmd
#define USART_BAUDRATE 115200 //串口波特率
#define USART_RX_GPIO_PORT GPIOE
#define USART_RX_GPIO_CLK RCC_AHB1Periph_GPIOE
#define USART_RX_PIN GPIO_Pin_7
#define USART_RX_AF GPIO_AF_UART7
#define USART_RX_SOURCE GPIO_PinSource7
#define USART_TX_GPIO_PORT GPIOE
#define USART_TX_GPIO_CLK RCC_AHB1Periph_GPIOE
#define USART_TX_PIN GPIO_Pin_8
#define USART_TX_AF GPIO_AF_UART7
#define USART_TX_SOURCE GPIO_PinSource8
#define USART_IRQHandler UART7_IRQHandler
#define USART_IRQ UART7_IRQn
/************************************************************/
虽然但是宏定义改一下更好
2.串口通信发送函数
union Send_Data{int data[2];char char_data[8];} send_data;//这里是用union作为数据的存储方式
void send_data(void)
{
//定义发送的数据
send_data.data[0] = (int)(Kinematics.yaw.actual_angle*100);
send_data.data[1] = (int)(Kinematics.pitch.actual_angle*100);
//printf("发送的数据为:");
Usart_SendByte(USART_NUM,'*');//开始标志位
//发送数据主体
for(int i=0;i<8;i++)
{
Usart_SendByte( USART_NUM,send_data.char_data[i]);
}
Usart_SendByte(USART_NUM,';');//结束标志位
}
3.串口通信接收函数(中断函数)
static char ucTemp;
union Recive_data{int sint_data[2];char char_data[8];}resive_data;
void USART_IRQHandler(void)
{
static int i=0;
static uint8_t start_recive_flag=0;
if(USART_GetITStatus(USART_NUM, USART_IT_RXNE))//接收中断,接收区非空,表示已接收到数据
{
ucTemp = USART_ReceiveData(USART_NUM);
//主要数据接收
if(start_recive_flag == 1)
{
resive_data.char_data[i] = ucTemp;
i++;
}
if(ucTemp == '*') //开始标志位
{
start_recive_flag=1;
}
else if(ucTemp == ';') //结束标志位
{
//printf("接收成功");
//云台模式
//control_mode = resive_data.sint_data[0];
//底盘速度
//Kinematics.wheel1.target_speed.linear_vel = resive_data.sint_data[1];
//Kinematics.wheel2.target_speed.linear_vel = resive_data.sint_data[1];
//yaw
Kinematics.yaw.target_angle = ((float)(resive_data.sint_data[0]))/100;
//pitch
Kinematics.pitch.target_angle = ((float)(resive_data.sint_data[1]))/100;
//波弹轮
//Kinematics.trigger.target_angular = (float)(resive_data.sint_data[4]);
//摩擦轮
//Kinematics.fric.target_angular = (float)(resive_data.sint_data[5]);
start_recive_flag=0;
i = 0;
}
}
}
无DMAhal库版
1.CubeMX配置
1.串口的设置
注意:如果直接配置UART7时,但是默认GPIO口不是PE7,8时,可以直接在串口设置中点击设置开启UART7
2.A板的时钟设置
2.串口初始化代码
(默认配置完成,不用管他)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @attention
*
* Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
UART_HandleTypeDef huart7;
/* UART7 init function */
void MX_UART7_Init(void)
{
/* USER CODE BEGIN UART7_Init 0 */
/* USER CODE END UART7_Init 0 */
/* USER CODE BEGIN UART7_Init 1 */
/* USER CODE END UART7_Init 1 */
huart7.Instance = UART7;
huart7.Init.BaudRate = 115200;
huart7.Init.WordLength = UART_WORDLENGTH_8B;
huart7.Init.StopBits = UART_STOPBITS_1;
huart7.Init.Parity = UART_PARITY_NONE;
huart7.Init.Mode = UART_MODE_TX_RX;
huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart7.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart7) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART7_Init 2 */
/* USER CODE END UART7_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==UART7)
{
/* USER CODE BEGIN UART7_MspInit 0 */
/* USER CODE END UART7_MspInit 0 */
/* UART7 clock enable */
__HAL_RCC_UART7_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
/**UART7 GPIO Configuration
PE8 ------> UART7_TX
PE7 ------> UART7_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_UART7;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/* UART7 interrupt Init */
HAL_NVIC_SetPriority(UART7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART7_IRQn);
/* USER CODE BEGIN UART7_MspInit 1 */
/* USER CODE END UART7_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==UART7)
{
/* USER CODE BEGIN UART7_MspDeInit 0 */
/* USER CODE END UART7_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_UART7_CLK_DISABLE();
/**UART7 GPIO Configuration
PE8 ------> UART7_TX
PE7 ------> UART7_RX
*/
HAL_GPIO_DeInit(GPIOE, GPIO_PIN_8|GPIO_PIN_7);
/* UART7 interrupt Deinit */
HAL_NVIC_DisableIRQ(UART7_IRQn);
/* USER CODE BEGIN UART7_MspDeInit 1 */
/* USER CODE END UART7_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
c
/* USER CODE END 1 */
3.发送和接收(中断)函数
具体发送和接收机制和标准库是一样的
1.发送函数
union Send_Data{int data[2];char char_data[8];} send_data;
uint8_t send_data_quan[10];
void Send_data()
{
send_data.data[0] = int_get[0];
send_data.data[1] = int_get[1];
send_data_quan[0] = '*';
for(int i =1;i<9;i++)
{
send_data_quan[i] = send_data.char_data[i-1];
}
send_data_quan[9] = ';';
//printf("发送的数据为:");c
HAL_UART_Transmit(&huart7,(uint8_t*)send_data_quan,10,1000);//发送消息
}
2.接收函数(中断)
1.代码
union Get_Data{int data[2];char char_data[8];} Get_data;
int int_get[2];
int iii=-1;
void UART7_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart7);
if(__HAL_UART_GET_FLAG( &huart7, UART_FLAG_RXNE ) != RESET)
{
uint8_t ch=(uint16_t) READ_REG(huart7.Instance->DR);
int_get[0] = Get_data.data[0];
int_get[1] = Get_data.data[1];
if(iii !=-1)
{
Get_data.char_data[iii] = ch;
iii++;
}
if(ch=='*')//开始位
{
iii = 0;
}
else if(ch == ';')//结束位
{
iii = -1;
}
__HAL_UART_CLEAR_FLAG(&huart7,UART_FLAG_RXNE);
}
__HAL_UART_ENABLE_IT(&huart7, UART_IT_ERR);
__HAL_UART_ENABLE_IT(&huart7,UART_IT_RXNE);
/* USER CODE END UART7_IRQn 1 */
}
2.注意事项
如果你进不去中断函数,可以参考一下这个博客:
(1条消息) 解决CUBEMX配置生成的UART无法进入中断的问题_arenascat的博客-CSDN博客
4.串口的初始化
我当时是直接把发送写在主函数里面的,主函数的代码如下
int main(void)
{
HAL_Init();
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);
SystemClock_Config();
MX_GPIO_Init();
MX_UART7_Init();
HAL_UART_Transmit_IT(&huart7 ,(uint8_t*)aTxStartMessages,sizeof(aTxStartMessages)); //sizeof()可读取目标长度
__HAL_UART_ENABLE_IT(&huart7, UART_IT_ERR);
//主函数一直发送
while (1)
{
Send_data();
HAL_Delay(10);
}
}
版权声明:本文为CSDN博主「九柳」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jiu_liu/article/details/122641778
暂无评论