请各位前辈多多指教。
现象描述:两块板子A B的数据通过CAN将数据送给C板子,但是收到的数据刷新几次后就停几次然后再刷新,如此反复;也就是中间有一段时间数据没有采集出来,程序如下:
can代码:
u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u8 mode,u16 brp)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//CAN单元设置
CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式
CAN_InitStructure.CAN_ABOM=DISABLE; //软件自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE; //睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
CAN_InitStructure.CAN_NART=ENABLE; //禁止报文自动传送
CAN_InitStructure.CAN_RFLM=DISABLE; //报文不锁定,新的覆盖旧的
CAN_InitStructure.CAN_TXFP=DISABLE; //优先级由报文标识符决定
CAN_InitStructure.CAN_Mode= mode; //模式设置: mode:0,普通模式;1,回环模式;
//设置波特率
CAN_InitStructure.CAN_SJW=tsjw; //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
CAN_InitStructure.CAN_BS1=tbs1; //Tbs1=tbs1+1个时间单位CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=tbs2; //Tbs2=tbs2+1个时间单位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
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //屏蔽位模式
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_FIFO1;//过滤器0关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//激活过滤器0
CAN_FilterInit(&CAN_FilterInitStructure); //滤波器初始化
#if CAN_RX0_INT_ENABLE
CAN_ITConfig(CAN1,CAN_IT_FMP1,ENABLE); //FIFO0消息挂号中断允许.
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 主优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 次优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
return 0;
}
#if CAN_RX0_INT_ENABLE
void USB_LP_CAN1_RX0_IRQHandler(void)//中断服务函数
{
CanRxMsg RxMessage;
CAN_Receive(CAN1, 1, &RxMessage);
}
#endif
u8 Can_Send_Msg(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=0x12;
TxMessage.ExtId=0;
TxMessage.IDE=CAN_Id_Standard;
TxMessage.RTR=0;//CAN_RTR_Data;
TxMessage.DLC=len;
for(i=0;i<len;i++)
TxMessage.Data[i]=msg[i];
mbox= CAN_Transmit(CAN1, &TxMessage);
i=0;
while((CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed)&&(i<0XFFF))i++; //等待发送结束
if(i>=0XFFF)return 1;
return 0;
}
u8 Can_Receive_Msg(u8 *buf)
{
u32 i;
CanRxMsg RxMessage;
if( CAN_MessagePending(CAN1,CAN_FIFO1)==0)return 0; //没有接收到数据,直接退出
CAN_Receive(CAN1, CAN_FIFO1, &RxMessage);//读取数据
if(RxMessage.StdId==0x12)
{
for(i=0;i<8;i++)
buf[i]=RxMessage.Data[i];
}
return RxMessage.DLC;
}
u8 Can_Receive_Msg1(u8 *buf)
{
u32 i;
CanRxMsg RxMessage;
if( CAN_MessagePending(CAN1,CAN_FIFO1)==0)return 0; //没有接收到数据,直接退出
CAN_Receive(CAN1, CAN_FIFO1, &RxMessage);//读取数据
if(RxMessage.StdId==0x13)
{
for(i=0;i<8;i++)
buf[i]=RxMessage.Data[i];
}
return RxMessage.DLC;
}
main 代码:
while(1)
{
Can_Receive_Msg(PA1);
Can_Receive_Msg1(PA2);
for(i=0;i<8;i++)
{
sprintf(str,"%d",PA1[i] );
LcdPrintString((i+4)*32,100,0x02,0xf1f0,0x0800," ");
LcdPrintString((i+4)*32,100,0x02,0xf1f0,0x0800,str);
}
for(i=0;i<8;i++)
{
sprintf(str,"%d",PA2[i] );
LcdPrintString((i+4)*32,150,0x02,0xf1f0,0x0800," ");
LcdPrintString((i+4)*32,150,0x02,0xf1f0,0x0800,str);
}
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
delay_ms(500);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
delay_ms(500);
}
请问代码有问题吗
多对1 应该 是要类似 下面 这个 图吗?
这个 可能 要 用到CAN的高层协议,仅用 CAN 大概是 比较 难 实现的
CAN总线高层协议CANWeb开发资料见:
http://www.openedv.com/forum.php?mod=viewthread&tid=109854
一周热门 更多>