概述:已经搞了一个星期了,现在不知道如何下手了
一共3块板子,其中板子A、B给板子C发送数据,当不连接板子A/B时,C板子能正常接收B/A板子数据,当同时接A B板子时,C板子接收数据不正常,下面为代码:
A板子main.c代码
while(1)
{
Average();
for(i=0;i<5;i++)
{
pa1[i]=i+cnt;
}
for(i=0;i<5;i++)
{
pa2[i]=i+10+cnt;
}
CAN1_Send_Msg(pa1,5,0x01);
CAN1_Send_Msg(pa2,5,0x02);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delay_ms(500);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
delay_ms(500);
cnt++;
if(cnt>10)
cnt=0;
}
B板子main.c代码
while(1)
{
Average();
for(i=0;i<5;i++)
{
pa1[i]=i+cnt;
}
for(i=0;i<5;i++)
{
pa2[i]=i+10+cnt;
}
CAN1_Send_Msg(pa1,5,0x03);
CAN1_Send_Msg(pa2,5,0x04);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delay_ms(500);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
delay_ms(500);
cnt++;
if(cnt>30)
cnt=20;
}
}
can.c代码
u8 CAN1_Send_Msg(float* msg,u8 len,u16 ID)
{
u8 mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=ID;
TxMessage.ExtId=0;
TxMessage.IDE=CAN_Id_Standard;
TxMessage.RTR=0;
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;
}
C板子can.c代码
u8 Can_Receive_Msg(float *buf1,float *buf2,float *buf3,float *buf4)
{
u32 i;
CanRxMsg RxMessage;
if( CAN_MessagePending(CAN1,CAN_FIFO1)==0)return 0; //没有接收到数据,直接退出
CAN_Receive(CAN1, CAN_FIFO1, &RxMessage);//读取数据
if(RxMessage.StdId==0x01)
{
for(i=0;i<5;i++)
buf1[i]=RxMessage.Data[i];
CAN_FIFORelease(CAN1, CAN_FIFO1);
}
CAN_Receive(CAN1, CAN_FIFO1, &RxMessage);
if(RxMessage.StdId==0x02)
{
for(i=0;i<5;i++)
buf2[i]=RxMessage.Data[i];
CAN_FIFORelease(CAN1, CAN_FIFO1);
}
CAN_Receive(CAN1, CAN_FIFO1, &RxMessage);
if(RxMessage.StdId==0x03)
{
for(i=0;i<5;i++)
buf3[i]=RxMessage.Data[i];
CAN_FIFORelease(CAN1, CAN_FIFO1);
}
CAN_Receive(CAN1, CAN_FIFO1, &RxMessage);
if(RxMessage.StdId==0x04)
{
for(i=0;i<5;i++)
buf4[i]=RxMessage.Data[i];
CAN_FIFORelease(CAN1, CAN_FIFO1);
}
return RxMessage.DLC;
}
C板子main.c代码
while(1)
{
Can_Receive_Msg(PA11,PA12,PA21,PA22);
for(i=0;i<5;i++)
{
sprintf(str,"%.2f",PA11[i] );
LcdPrintString(192,35*i+50,0x02,0xf1f0,0x0800," ");
LcdPrintString(192,35*i+50,0x02,0xf1f0,0x0800,str);
sprintf(str,"%.2f",PA12[i] );
LcdPrintString(192*2,35*i+50,0x02,0xf1f0,0x0800," ");
LcdPrintString(192*2,35*i+50,0x02,0xf1f0,0x0800,str);
}
for(i=0;i<5;i++)
{
sprintf(str,"%.2f",PA21[i] );
LcdPrintString(0,35*i+50,0x02,0xf1f0,0x0800," ");
LcdPrintString(0,35*i+50,0x02,0xf1f0,0x0800,str);
sprintf(str,"%.2f",PA22[i] );
LcdPrintString(100,35*i+50,0x02,0xf1f0,0x0800," ");
LcdPrintString(100,35*i+50,0x02,0xf1f0,0x0800,str);
}
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
delay_ms(500);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
delay_ms(500);
}
请教高手,这是什么原因
一周热门 更多>