哪位大神帮忙看一下STM32F1 can通讯会死机的问题

2019-12-16 22:36发布

单片机为STM32F103,CAN通讯波特率设置为250Kbps,使用库函数做的。

正常小数据通讯的时候没有问题,但是大量帧数据连续下发的时候,CAN有时候就收不到数据,这是怎么回事呢?哪位大婶帮帮忙。

以下是初始化程序
void CAN_Config(void)//初始化配置
{
        GPIO_InitTypeDef GPIO_InitStructure;
        CAN_InitTypeDef CAN_InitStructure;
        CAN_FilterInitTypeDef CAN_FilterInitStructure;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 ,ENABLE);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOD,&GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOD,&GPIO_InitStructure);
       
        GPIO_PinRemapConfig(GPIO_Remap2_CAN1,ENABLE);

        CAN_DeInit(CAN1);
        CAN_StructInit(&CAN_InitStructure);
        CAN_InitStructure.CAN_TTCM = DISABLE;
        CAN_InitStructure.CAN_ABOM = DISABLE;
        CAN_InitStructure.CAN_AWUM = DISABLE;
        CAN_InitStructure.CAN_NART = DISABLE;
        CAN_InitStructure.CAN_RFLM = DISABLE;
        CAN_InitStructure.CAN_TXFP = DISABLE;
        CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
        CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
        CAN_InitStructure.CAN_BS1 = CAN_BS1_8tq;
        CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;
        CAN_InitStructure.CAN_Prescaler = 9;
        CAN_Init(CAN1,&CAN_InitStructure);
       
        CAN_FilterInitStructure.CAN_FilterNumber = 0;
        CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
        CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
        CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
        CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
        CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
        CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;

        CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
        CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
        CAN_FilterInit(&CAN_FilterInitStructure);
        CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
}
以下是CAN中断程序:
void CAN1_RX0_IRQHandler(void)
{
        u8 i;
        CAN_Receive(CAN1,CAN_FIFO0,&CAN_Rr_Message);
        ID_Source = (CAN_Rr_Message.ExtId)>>21;
        ID_aim                = (CAN_Rr_Message.ExtId)>>13;
        ID_cnt                 =        (CAN_Rr_Message.ExtId)>>5;
        if((ID_Source ==  0x01 || ID_Source ==  0x00)        && ID_aim == 0xff)
        {
                for(i=0;i<CAN_Rr_Message.DLC;i++)
                {
                        USART1Buffer[i+8*ID_cnt]=CAN_Rr_Message.Data[i];
                }
                if(USART1Count > 2000)USART1Count = 0;       
                if((8*ID_cnt+CAN_Rr_Message.DLC)>USART1Count)USART1Count=8*ID_cnt + CAN_Rr_Message.DLC;
                if((CAN_Rr_Message.ExtId & 0x10)==0)
                {
                        CAN_Data_Handler();//此处为协议处理
                }
        }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。