21ic问答首页 - N32G455 RS485中断接收函数进入不了,请问是什么原因呀?
N32G455 RS485中断接收函数进入不了,请问是什么原因呀?
RS485能够发送数据,但是接收数据时一直进不了RS485中断函数,请大佬们指点一下void RS485_Init(void)
{
GPIO_InitType GPIO_InitStruct;
USART_InitType USART_InitStruct;
NVIC_InitType NVIC_InitStruct;
// 使能时钟(USART2在APB1总线上)
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART2, ENABLE);
// 配置方向控制引脚PA1
GPIO_InitStruct.Pin = RS485_EN_GPIO_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(RS485_EN_GPIO_PORT, &GPIO_InitStruct);
GPIO_ResetBits(RS485_EN_GPIO_PORT, RS485_EN_GPIO_PIN); // 默认接收模式
// 配置USART2 TX引脚PA2(复用推挽输出)
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct);
// 配置USART2 RX引脚PA3(浮空输入)
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct);
// USART参数配置
USART_InitStruct.BaudRate = 9600; // 波特率;
USART_InitStruct.WordLength = USART_WL_8B;
USART_InitStruct.StopBits = USART_STPB_1;
USART_InitStruct.Parity = USART_PE_NO;
USART_InitStruct.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStruct.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USART2, &USART_InitStruct);
// 使能接收中断
USART_ConfigInt(USART2, USART_INT_RXDNE, ENABLE);
USART_ConfigInt(USART2, USART_INT_IDLEF, ENABLE);
// 配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// 使能USART
USART_Enable(USART2, ENABLE);
}
// 发送函数
void RS485_SendData(uint8_t *pData, uint16_t len)
{
// 设置为发送模式
GPIO_SetBits(RS485_EN_GPIO_PORT, RS485_EN_GPIO_PIN);
// 阻塞式发送数据
for (uint16_t i = 0; i < len; i++)
{
//
while (USART_GetFlagStatus(USART2, USART_FLAG_TXDE) == RESET)
; // 等待发送完成
USART_SendData(USART2, pData[i]);
}
// 确保最后一位发送完成
while (USART_GetFlagStatus(USART2, USART_FLAG_TXC) == RESET)
;
// 恢复接收模式
GPIO_ResetBits(RS485_EN_GPIO_PORT, RS485_EN_GPIO_PIN);
}
// 接收缓冲区和索引
#define RX_BUF_SIZE 128
volatile uint8_t rxBuffer[RX_BUF_SIZE];
volatile uint16_t rxIndex = 0;
void RS485_IRQHandler(void)
{
if (USART_GetIntStatus(USART2, USART_FLAG_RXDNE) != RESET)
{
rxBuffer[rxIndex++] = USART_ReceiveData(USART2);
}
if (USART_GetIntStatus(USART2, USART_FLAG_IDLEF) != RESET)
{
if (rxBuffer[0] == 0xAA)
{
printf("rxBuffer = %x\r\n", rxBuffer[0]);
}
}
rxIndex = 0;
USART_ClrIntPendingBit(USART2, USART_INT_IDLEF);// 清除空闲中断
/*清除空闲中断*/
USART2->STS;
USART2->DAT;
}
{
GPIO_InitType GPIO_InitStruct;
USART_InitType USART_InitStruct;
NVIC_InitType NVIC_InitStruct;
// 使能时钟(USART2在APB1总线上)
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART2, ENABLE);
// 配置方向控制引脚PA1
GPIO_InitStruct.Pin = RS485_EN_GPIO_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(RS485_EN_GPIO_PORT, &GPIO_InitStruct);
GPIO_ResetBits(RS485_EN_GPIO_PORT, RS485_EN_GPIO_PIN); // 默认接收模式
// 配置USART2 TX引脚PA2(复用推挽输出)
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct);
// 配置USART2 RX引脚PA3(浮空输入)
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct);
// USART参数配置
USART_InitStruct.BaudRate = 9600; // 波特率;
USART_InitStruct.WordLength = USART_WL_8B;
USART_InitStruct.StopBits = USART_STPB_1;
USART_InitStruct.Parity = USART_PE_NO;
USART_InitStruct.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStruct.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USART2, &USART_InitStruct);
// 使能接收中断
USART_ConfigInt(USART2, USART_INT_RXDNE, ENABLE);
USART_ConfigInt(USART2, USART_INT_IDLEF, ENABLE);
// 配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// 使能USART
USART_Enable(USART2, ENABLE);
}
// 发送函数
void RS485_SendData(uint8_t *pData, uint16_t len)
{
// 设置为发送模式
GPIO_SetBits(RS485_EN_GPIO_PORT, RS485_EN_GPIO_PIN);
// 阻塞式发送数据
for (uint16_t i = 0; i < len; i++)
{
//
while (USART_GetFlagStatus(USART2, USART_FLAG_TXDE) == RESET)
; // 等待发送完成
USART_SendData(USART2, pData[i]);
}
// 确保最后一位发送完成
while (USART_GetFlagStatus(USART2, USART_FLAG_TXC) == RESET)
;
// 恢复接收模式
GPIO_ResetBits(RS485_EN_GPIO_PORT, RS485_EN_GPIO_PIN);
}
// 接收缓冲区和索引
#define RX_BUF_SIZE 128
volatile uint8_t rxBuffer[RX_BUF_SIZE];
volatile uint16_t rxIndex = 0;
void RS485_IRQHandler(void)
{
if (USART_GetIntStatus(USART2, USART_FLAG_RXDNE) != RESET)
{
rxBuffer[rxIndex++] = USART_ReceiveData(USART2);
}
if (USART_GetIntStatus(USART2, USART_FLAG_IDLEF) != RESET)
{
if (rxBuffer[0] == 0xAA)
{
printf("rxBuffer = %x\r\n", rxBuffer[0]);
}
}
rxIndex = 0;
USART_ClrIntPendingBit(USART2, USART_INT_IDLEF);// 清除空闲中断
/*清除空闲中断*/
USART2->STS;
USART2->DAT;
}
赞0
1、Debug仿真,单步调试,看相关寄存器和接收状态标志等值;
2、用示波器测试Rx的波形,看下是否有数据;
3、如果有波形,用逻辑分析仪的UART总线模式分析一下数据帧格式。
评论
2025-04-25
您需要登录后才可以回复 登录 | 注册