设置了波特率为500k的can通信,实测发送频率为1200hz左右,但有时候运行的时候can通信就会进入卡死状态,在发送函数can_transmit里面出不来,查看寄存器显示随机错误有时候隐性错误,有时候显性错误,一直找不到原因,设置了can错误中断,一直也进不去。不知道哪里有问题,前来请教一下
CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_Normal); //500K 通信模式改变
//can通信初始化
void CAN1_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
//使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能PORTA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟
//引脚复用映射配置
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1); //GPIOA11复用为CAN1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12复用为CAN1
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化PA11,PA12
//CAN单元设置
CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式
CAN_InitStructure.CAN_ABOM=ENABLE; //软件自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE;//睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位) 清除SLEEP位(软件清除)
CAN_InitStructure.CAN_NART=ENABLE; //使用报文自动传送
CAN_InitStructure.CAN_RFLM=DISABLE; //报文不锁定,新的覆盖旧的
CAN_InitStructure.CAN_TXFP=DISABLE; //DISABLE 优先级由报文标识符决定 ENABLE优先级由报文标识符决定
CAN_InitStructure.CAN_Mode= mode; //模式设置
CAN_InitStructure.CAN_SJW=tsjw; //重新同步跳跃宽度(Tsjw) tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq 重新同步跳跃宽度
CAN_InitStructure.CAN_BS1=tbs1; //Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=tbs2;//Tbs2范围CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler=brp; //分频系数(Fdiv)为brp+1
CAN_Init(CAN1, &CAN_InitStructure); // 初始化CAN1
//配置过滤器
CAN_FilterInitStructure.CAN_FilterNumber=0; //过滤器0 选择哪个筛选器 F4有28个筛选器 F1有14个筛选器
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //设置筛选器的工作模式 列表模式和掩码模式
// CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList; //设置筛选器的工作模式 列表模式和掩码模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32位 设置筛选器的位宽
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;////32位ID
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32位MASK 掩码
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器0关联到FIFO0 设置报文存储在哪个FIOFO里面
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //激活过滤器0 是否激活筛选器
CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
CAN_ITConfig(CAN1, CAN_IT_ERR,ENABLE); //开启错误中断
CAN_ITConfig(CAN1,CAN_IT_EWG|CAN_IT_EPV|CAN_IT_BOF|CAN_IT_LEC|CAN_IT_ERR,ENABLE);
}
//中断的配置
NVIC_InitStructure.NVIC_IRQChannel =CAN1_SCE_IRQn; /*配置CAN1_SCE_IRQn 为中断源*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /* 抢断优先级为0 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; /* 子优先级为2 */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /* 使能中断 */
NVIC_Init(&NVIC_InitStructure);
CAN_ITConfig(CAN1,CAN_IT_EWG|CAN_IT_EPV|CAN_IT_BOF|CAN_IT_LEC|CAN_IT_ERR,ENABLE);
//在错误中断里清除错误标志,重新初始化CAN模块
void CAN1_SCE_IRQHandler(void)
{
CAN_Error_Count++;
unsigned char Rx_Error_Cnt;
unsigned char Tx_Error_Cnt;
unsigned char Last_Error;
if(CAN_GetITStatus(CAN1,CAN_IT_EWG) == SET) //错误警告中断
{
T_status=1;
Tx_Error_Cnt = CAN_GetLSBTransmitErrorCounter(CAN1);
Rx_Error_Cnt = CAN_GetReceiveErrorCounter(CAN1);
if((Tx_Error_Cnt>=96)||(Rx_Error_Cnt>=96))
{
;
}
CAN_ClearITPendingBit(CAN1,CAN_IT_EWG);
}
if(CAN_GetITStatus(CAN1,CAN_IT_EPV) == SET) //错误被动中断
{
Tx_Error_Cnt = CAN_GetLSBTransmitErrorCounter(CAN1);
Rx_Error_Cnt = CAN_GetReceiveErrorCounter(CAN1);
if((Tx_Error_Cnt>127)||(Rx_Error_Cnt>127))
{
;
}
T_status=2;
CAN_ClearITPendingBit(CAN1,CAN_IT_EPV);
}
if(CAN_GetITStatus(CAN1,CAN_IT_BOF) == SET) //离线中断
{
T_status=3;
//CAN节点重启
CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_Normal); //500K 通信模式改变
CAN_ClearITPendingBit(CAN1,CAN_IT_BOF);
}
if(CAN_GetITStatus(CAN1,CAN_IT_LEC) == SET) //上次错误号中断
{
T_status=4;
Last_Error = CAN_GetLastErrorCode(CAN1);
if(Last_Error == 0x1) //位填充错误
{
T_Count2=6;
}
if(Last_Error == 0x2) //格式错误
{
T_Count2=1;
}
if(Last_Error == 0x3) //确认(ACK)错误
{
T_Count2=2;
}
if(Last_Error == 0x4) //隐性位错误
{
T_Count2=3;
}
if(Last_Error == 0x5) //显性位错误
{
T_Count2=4;
}
if(Last_Error == 0x6) //CRC错误
{
T_Count2=5;
}
CAN_ClearITPendingBit(CAN1,CAN_IT_LEC);
}
if(CAN_GetITStatus(CAN1,CAN_IT_ERR) == SET) //错误中断
{
T_status=6;
//CAN节点重启
CAN_ClearITPendingBit(CAN1,CAN_IT_ERR);
CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_Normal); //500K 通信模式改变
}
}