使用正点原子探索板,HAL库开发。打算将UART2的TX和RX短接测试UART2的接收中断程序是否正常(发送正常,已测试)。但是将TX和RX短接后断开,TX和RX都是只输出高电平,且RX并没有收到任何报文。请问如何让RX正常接收报文?
以下是相关文件的头文件
#ifndef JUDGE_UART
#define JUDGE_UART
#include "changeable for user.h"
typedef union
{
uint8_t result[4];
uint32_t input;
}
Message_TRX_Helper;
typedef struct
{
uint32_t data[3];
uint8_t mask;
}
Judge_TX_DATA;
typedef struct
{
Judge_TX_DATA DataIn;
uint8_t DataOut[20];
}
Judge_TX_Mailbox;
extern Judge_TX_Mailbox Judge_TX_Mailbox1; //
extern uint8_t Judge_RX_Mailbox1; //
extern Message_TRX_Helper Message_TRX_Helper1;
void Judge_Communicate_Init(void); //
void Judge_Transmit(void); //
void Judge_Receive(void); //
#endif
以下是相关文件的源文件
#include "judge_uart.h"
#include "CRC.h"
#include "test for creater.h"
//Judge_Communicate_Init
UART_HandleTypeDef JudgeCom;
//Judge_Transmit/Receive
Judge_TX_Mailbox Judge_TX_Mailbox1;
Message_TRX_Helper Message_TRX_Helper1;
uint8_t Judge_RX_Mailbox1;
void Judge_Communicate_Init(void)
{
GPIO_InitTypeDef JudgeCom_Tx_Pin;
GPIO_InitTypeDef JudgeCom_Rx_Pin;
//GPIO
__HAL_RCC_GPIOA_CLK_ENABLE();
JudgeCom_Tx_Pin.Pin=UART_TX_PIN;
JudgeCom_Tx_Pin.Mode=GPIO_MODE_AF_PP;
JudgeCom_Tx_Pin.Pull=GPIO_PULLUP;
JudgeCom_Tx_Pin.Speed=GPIO_SPEED_FREQ_HIGH;
JudgeCom_Tx_Pin.Alternate=GPIO_AF7_USART2;
HAL_GPIO_Init(UART_TX_GPIO,&JudgeCom_Tx_Pin);
JudgeCom_Rx_Pin.Pin=UART_RX_PIN;
JudgeCom_Rx_Pin.Mode=GPIO_MODE_AF_PP;
JudgeCom_Rx_Pin.Pull=GPIO_PULLUP;
JudgeCom_Rx_Pin.Speed=GPIO_SPEED_FREQ_HIGH;
JudgeCom_Rx_Pin.Alternate=GPIO_AF7_USART2;
HAL_GPIO_Init(UART_RX_GPIO,&JudgeCom_Rx_Pin);
//UART
__HAL_RCC_USART2_CLK_ENABLE();
JudgeCom.Instance = USART2;
JudgeCom.Init.BaudRate = 115200;
JudgeCom.Init.WordLength = UART_WORDLENGTH_8B;
JudgeCom.Init.StopBits = UART_STOPBITS_1;
JudgeCom.Init.Parity = UART_PARITY_NONE;
JudgeCom.Init.Mode = UART_MODE_TX_RX;
JudgeCom.Init.HwFlowCtl = UART_HWCONTROL_NONE;
JudgeCom.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&JudgeCom);
HAL_UART_Receive_IT(&JudgeCom,&Judge_RX_Mailbox1,1);
}
/***************************************
Judge_Transmit
***************************************/
void Judge_Transmit(void)
{
//略
}
/***************************************
Judge_Receive
***************************************/
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
__HAL_UART_ENABLE_IT(&JudgeCom,UART_IT_RXNE);
HAL_NVIC_EnableIRQ(USART2_IRQn);
HAL_NVIC_SetPriority(USART2_IRQn,3,3);
}
void USART1_IRQHandler(void)
{
uint32_t timeout=0;
HAL_UART_IRQHandler(&JudgeCom); //
timeout=0;
while (HAL_UART_GetState(&JudgeCom) != HAL_UART_STATE_READY)
{
timeout++;
if(timeout>HAL_MAX_DELAY) break;
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
delay_init(168);
HAL_UART_Receive_IT(&JudgeCom,&Judge_RX_Mailbox1,1);
if(Judge_RX_Mailbox1 != 0x00)
{
HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_9);
}
}
以下是主函数
int main()
{
HAL_Init();
Stm32_Clock_Init(336,8,2,7);
delay_init(168);
Judge_Communicate_Init();
delay_ms(50);
Judge_TX_Mailbox1.DataIn.data[0]=0x5555;
Judge_TX_Mailbox1.DataIn.data[1]=0x5555;
Judge_TX_Mailbox1.DataIn.data[2]=0x5555;
Judge_TX_Mailbox1.DataIn.mask=0x55;
Judge_Communicate_Init();
while(1)
{
Judge_Transmit();
delay_us(10);
}
}
推测是中断函数出了问题
我尝试用keil5调试,但是发送之后所有图标变灰色,用正点原子给的串口中断例程调试出现相同现象,推测是keil5无法进入中断。 |