1. 今天调试CAN, STM32F334C8T6,做一个生物PCR监测的项目,用到CAN,程序写完后,调试,发现发送只能发送16帧,而且不能接收,用的是USB2CAN
2. 检查程序初始化部分如下
u8 CAN1_Init(u16 BaudRate,u8 mode)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
//初始化GPIO
GPIO_InitTypeDef GPIO_InitStructure={
.GPIO_Pin = GPIO_Pin_12,
.GPIO_Mode = GPIO_Mode_AF, //复用功能
.GPIO_OType = GPIO_OType_PP, //推挽输出
.GPIO_Speed = GPIO_Speed_50MHz, //100MHz
.GPIO_PuPd = GPIO_PuPd_NOPULL, //上拉
};
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化PA11,PA12
GPIO_InitTypeDef GPIO_InitStructure11={
.GPIO_Pin = GPIO_Pin_11,
.GPIO_Mode = GPIO_Mode_AF, //复用功能
.GPIO_OType = GPIO_OType_PP, //推挽输出
.GPIO_Speed = GPIO_Speed_50MHz, //100MHz
.GPIO_PuPd = GPIO_PuPd_UP, //上拉
};
GPIO_Init(GPIOA, &GPIO_InitStructure11);//初始化PA11,PA12
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_9);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_9);
//CAN单元设置
CAN_InitTypeDef CAN_InitStructure={
.CAN_TTCM=DISABLE, //非时间触发通信模式
.CAN_ABOM=DISABLE, //软件自动离线管理
.CAN_AWUM=DISABLE, //睡眠模式通过软件唤醒
.CAN_NART=ENABLE, //禁止报文自动传送
.CAN_RFLM=DISABLE, //报文不锁定,新的覆盖旧的
.CAN_TXFP=DISABLE, //优先级由报文标识符决定
.CAN_Mode= mode, //模式设置
.CAN_SJW=CAN_SJW_1tq, //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位
.CAN_BS1=CAN_BS1_6tq, //CAN_BS2_5tq,Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
.CAN_BS2=CAN_BS2_5tq, //CAN_BS1_6tq,Tbs2范围CAN_BS2_1tq ~ CAN_BS2_8tq
.CAN_Prescaler=BaudRate, //12,BaudRate,分频系数(Fdiv)为brp+1
};
CAN_Init(CAN1, &CAN_InitStructure);// 初始化CAN1//配置过滤器
CAN_FilterInitTypeDef CAN_FilterInitStructure0={
.CAN_FilterNumber=CAN_Filter_FIFO0, //过滤器0
.CAN_FilterMode=CAN_FilterMode_IdMask,
.CAN_FilterScale=CAN_FilterScale_32bit, //32位
.CAN_FilterIdHigh=00, //16, 32位ID
.CAN_FilterIdLow=0x0000,
.CAN_FilterMaskIdHigh=0x0000, //0xff,32位MASK
.CAN_FilterMaskIdLow=0x0000,
.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0,//过滤器0关联到FIFO0
.CAN_FilterActivation=ENABLE, //激活过滤器0
};
CAN_FilterInit(&CAN_FilterInitStructure0); //滤波器初始化
CAN_FilterInitTypeDef CAN_FilterInitStructure1={
.CAN_FilterNumber=CAN_Filter_FIFO1, //过滤器1
.CAN_FilterMode=CAN_FilterMode_IdMask,
.CAN_FilterScale=CAN_FilterScale_32bit, //32位
.CAN_FilterIdHigh=0x00FF, //网络参数更改
.CAN_FilterIdLow=0x0000,
.CAN_FilterMaskIdHigh=0x00FF, //32位MASK
.CAN_FilterMaskIdLow=0x0000,
.CAN_FilterFIFOAssignment=CAN_Filter_FIFO1, //过滤器0关联到FIFO0
.CAN_FilterActivation=ENABLE, //激活过滤器0
};
CAN_FilterInit(&CAN_FilterInitStructure1); //滤波器初始化
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE); //FIFO1消息挂号中断允许
NVIC_InitTypeDef NVIC_InitStructure0={
.NVIC_IRQChannel = CAN1_RX0_IRQn,
.NVIC_IRQChannelPreemptionPriority = 1, // 主优先级为1
.NVIC_IRQChannelSubPriority = 0, // 次优先级为0
.NVIC_IRQChannelCmd = ENABLE,
};
NVIC_Init(&NVIC_InitStructure0);
CAN_ITConfig(CAN1,CAN_IT_FMP1,ENABLE); //FIFO1 消息挂号中断允许
NVIC_InitTypeDef NVIC_InitStructure1={
.NVIC_IRQChannel = CAN1_RX1_IRQn,
.NVIC_IRQChannelPreemptionPriority = 1, // 主优先级为1
.NVIC_IRQChannelSubPriority = 2, // 次优先级为0
.NVIC_IRQChannelCmd = ENABLE,
};
NVIC_Init(&NVIC_InitStructure1);
return 0;
}
波特率是250K,计算一下 36MHZ/分频/(1+6+5),CAN_SJW_1tq,CAN_BS1_6tq,CAN_BS2_5tq,刚好是250K
GPIO看了文档也是OK
滤波器设置也是参照我以前的407的(CAN OK),
3.检查了发送程序
u8 CAN_DataSend(MsgFrame const Msg)
{
u8 mbox;
u16 i=0;
CanTxMsg TxMessage;
BIT32 ExtID;
//源地址
ExtID.Bit8.Bit8[3] = 16;
//目的地址
ExtID.Bit8.Bit8[2] = Msg.MsgInfo.fAddr;
//指令
ExtID.Bit8.Bit8[1] = Msg.Bit8[3];
ExtID.Bit8.Bit8[0] = 0;
TxMessage.StdId = 0x0; // 标准标识符为0
TxMessage.ExtId = ExtID.Int>>3; // 设置扩展标示符(29位)
TxMessage.IDE = CAN_Id_Extended; // 使用扩展标识符
TxMessage.RTR = 0; // 消息类型为数据帧,一帧8位
TxMessage.DLC = 8; // 发送两帧信息
//拷贝数据
memcpy(&TxMessage.Data[i],&(Msg.Bit8[5]),8);
//传输数据
mbox= CAN_Transmit(CAN1, &TxMessage);
while((CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed)&&(i<0XFFF))
i++;//等待发送结束
if(i>=0XFFF)return 0;
return 1;
}
和以前也是一样的,同时,用逻辑分析器,看也有波形。也有数据,但为啥还是不能连续发送呢?有点迷茫
4. 还是再看看电路图吧(不是我画的,我同事画的),发现没有接120R电阻,以前记得要接120R,找同事要几个120R电阻
接上去,测试,发现OK。
5.测试一下接收,也是OK
void CAN1_RX0_IRQHandler(void)
{
if(CAN_GetFlagStatus(CAN1,CAN_FLAG_FMP0))
{
memset(&CANRxBuffer,0,sizeof(CanRxMsg));
CAN_Receive(CAN1, CAN_FIFO0, &CANRxBuffer);
if(CAN1FrameHandler!=NULL)
{
CAN1FrameHandler();
}
}
CAN_ClearFlag(CAN1,CAN_FLAG_FMP0);
}
6. 折腾了一天的问题,终于解决了。