AT32F415C8串口1,发送完后就进入接收中断求帮分析
USART1配置普通收发模式,发送完后就进入接收中断 ,发送几个数据就进入几次。USART1 刚开始重映射用了PB6 PB7搞了一天都还是不是,用复用PA10,PA9也是这问题。上代码:void GpioCon**(void)
{
RCC_APB2PeriphClockCmd( RCC_APB2PERIPH_GPIOA
| RCC_APB2PERIPH_GPIOB
| RCC_APB2PERIPH_GPIOC
| RCC_APB2PERIPH_GPIOF
| RCC_APB2PERIPH_AFIO , ENABLE); //打开外设时钟
//TXD1
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_9 ;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//RXD1
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_10 ;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Usart1Con**(void)
{
USART_InitType USART_InitStructure;
NVIC_InitType NVIC_InitStructure;
USART_ClockInitType USART_ClockInitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2PERIPH_USART1, ENABLE);
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
/*Configure UART param*/
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_INTConfig(USART1, USART_INT_RDNE, ENABLE); //开启接收中断
//USART_ClearFlag(USART1, USART_INT_RDNE); //清除USARTx的挂起标志
USART_Cmd(USART1, ENABLE); //使能USART
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_SEND_DATA(u8 * data ,u8 datalen)
{
u8 t =0;
if(USART1_TXOK_FLAG == 1)
{
for(t=0;t<datalen;t++)
{
USART_SendData(USART1, data); //向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TDE)!=SET); //等待发送结束
}
USART1_TXOK_FLAG = 0;
}
}
void USART1_IRQHandler(void)
{
u8 u8InChar;
static u8 sezi_cnt = 0;
if(USART_GetITStatus(USART1,USART_INT_RDNE) != RESET )
{
u8InChar = USART_ReceiveData(USART1);
SYS_LED_TURN;
DomeTxCnt++;
/****************************************************************************************************/
//PollReceHandle(u8InChar); //并机通讯接收处理
/****************************************************************************************************/
//USART_INTConfig(USART1, USART_INT_RDNE, DISABLE);
}
} 经仿真发现发送一次数据 RDNE 位会被置1,导致进入接收中断!怎解? 1、检查一下是否PA10确实有收到数据?
2、如果没有收到数据,RDNE就置起了,请尝试用官方BSP的代码,看是否还有同样的问题。 仿真的时候,中断接收吗?还是发送的时候,中断发送?
vt2017 发表于 2021-11-14 10:29
1、检查一下是否PA10确实有收到数据?
2、如果没有收到数据,RDNE就置起了,请尝试用官方BSP的代码,看是否 ...
确定了PA10没有接收信号的,根据官方的例程代码,没有开启那个UART1时钟后,收发正常了,但是偶尔会掉数据。 根据官方例程,写了初始化,发现不用USART时钟的,收发也能正常,但是发送偶尔会掉数据,由于时间有限没有再去深究改用了串口2先用着先
void Usart1Con**(void)
{
USART_InitType USART_InitStructure;
NVIC_InitType NVIC_InitStructure;
USART_ClockInitType USART_ClockInitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2PERIPH_USART1, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*Configure UART param*/
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ClearFlag(USART1, USART_INT_RDNE); //清除USARTx的挂起标志
USART_Cmd(USART1, ENABLE); //使能USART
USART_INTConfig(USART1, USART_INT_RDNE, ENABLE); //开启接收中断
}
发送到电脑串口助手的数据
【2021-49-22 08:49:06 878】03 03 00 20 00 01 84 22
【2021-49-22 08:49:07 778】04 03 00 20 00 01 85 95
【2021-49-22 08:49:08 678】01 03 00 20 00 01 85 C0
【2021-49-22 08:49:09 578】02 03 00 20 00 01 85 F3
【2021-49-22 08:49:10 478】03 03 00 20 00 01 84 22
【2021-49-22 08:49:11 378】06 03 00 20 00 01 85 95
【2021-49-22 08:49:12 278】01 03 00 20 00 01 85 C0
【2021-49-22 08:49:13 178】02 03 00 20 00 01 85 FE
【2021-49-22 08:49:14 072】03 20
【2021-49-22 08:49:14 128】20 00 01 84 22
【2021-49-22 08:49:14 975】04 03 00 20 00 01
【2021-49-22 08:49:15 009】58 F9
【2021-49-22 08:49:15 878】01 03 00 20 00 01 85 C0
【2021-49-22 08:49:16 778】02 03 00 20 00 01 85 F3
【2021-49-22 08:49:17 678】03 03 00 20 00 01 84 23
【2021-49-22 08:49:18 578】04 03 00 20 00 01 85 95
【2021-49-22 08:49:19 477】01 03 00 20 00 01 85 C0
【2021-49-22 08:49:20 377】02 03 00 20 00 01 D8
【2021-49-22 08:49:21 272】03 20
【2021-49-22 08:49:21 309】20 00 01 84 22
【2021-49-22 08:49:22 177】04 03 00 20 00 01 85 95
【2021-49-22 08:49:23 075】01 03 00 20 00 01
【2021-49-22 08:49:23 109】18
【2021-49-22 08:49:23 977】03 03 00 20 00 01 85 F3
【2021-49-22 08:49:24 877】03 03 00 20 00 01 84 22
【2021-49-22 08:49:25 776】04 03 00 20 00 01
【2021-49-22 08:49:25 811】58 F9
【2021-49-22 08:49:26 671】40 20
rhp2005 发表于 2021-11-22 09:09
根据官方例程,写了初始化,发现不用USART时钟的,收发也能正常,但是发送偶尔会掉数据,由于时间有限没有 ...
不开时钟是肯定不能用的,你最好调试看看RCC寄存器里面 USART时钟是不是在你没发现的地方被打开了 vt2017 发表于 2021-11-22 09:19
不开时钟是肯定不能用的,你最好调试看看RCC寄存器里面 USART时钟是不是在你没发现的地方被打开了 ...
我说的USART时钟是这个
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
页:
[1]