本帖最后由 llycom 于 2010-5-4 10:43 编辑
用STM32中断接收USART2的数据,置位接收标志,在主程序中进行处理。程序可以运行,但一般过20分钟左右就死机了,原来以为是收发对象有问题,用计算机进行验证后发现是STM32的问题。而STM32的中断设置都是与网上的例程没有区别,不知道什么原因。我的中断大概是100MS左右一次,每次接收7个字节的数据(MODBUS协议)
最近发现在接收中断里加一个延时程序,就可以不死机,一般要延时5MS左右才行。请大家帮忙分析一下,谢谢了!
下面是程序代码:
中断处理:
void USART2_IRQHandler(void){
//处理接收到的数据
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
Rs485Rx[Rs485Rxcont]=USART_ReceiveData(USART2);
Rs485Rxcont++;
if(Rs485Rxcont>=7)
{
Usart2_Get_Flag=TRUE;
Delay(3);
}
// Clear the USART2 Receive interrupt
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
}
时钟:
/* Enable USART2 and GPIOA clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
中断优先级:
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO:
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.3) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART2:
*/
USART_InitStructure.USART_BaudRate = 19200;
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_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //开串口2接收中断
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
中断处理:
if(Usart2_Get_Flag)
{
GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(1));
printf("%1c%1c%1c%1c%1c%1c%1c",0x03,0x03,0x02,0x02,0x04,0xC1,0x27);
GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(0));
Rs485Rxcont=0;
Usart2_Get_Flag=FALSE;
} |