本帖最后由 1575485998 于 2017-5-15 11:09 编辑
两块板子互发数据,数据打包完以后,接收到的数据,一共8位,最后两位异常。求助。
下面是发送代码函数和接收函数。
[mw_shl_code=c,true]void CAN_Send_Msg(u32 id,u8 *msg,u8 len)
{
u8 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=id;//0-7FF
TxMessage.ExtId=0x00;//0-7FFFFFFF
TxMessage.IDE=CAN_ID_STD;
TxMessage.RTR=CAN_RTR_Data;
TxMessage.DLC=len;
for(i=0;i<len;i++)
{
TxMessage.Data
=msg;
}
CAN_Transmit(CAN1,&TxMessage);
}[/mw_shl_code]
接收中断函数
[mw_shl_code=c,true]void USB_LP_CAN1_RX0_IRQHandler(void) //接收中断
{
u8 i=0;
CanRxMsg RxMessage;
if(CAN_GetITStatus(CAN1,CAN_IT_FMP0)!=RESET)
{
CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);
can_rx_flag=0;
for(i=0;i<RxMessage.DLC;i++)
{
CAN_BUF=RxMessage.Data;
}
LED0=!LED0;
LED1=!LED1;
}
else
{
can_rx_flag=0xff;
}
}[/mw_shl_code]
主要问题就是,主机发送一个数据,例如ARM1DO33 从机接受到的数据却是ARM1DO 后面两位乱码。不知道什么原因,调试发现,用CAN_Transimit(CAN1,&TxMessage)发送过去的数据是正常的,
但是接收方的CAN_Reveive()函数,进去以后发现得到的值最后两位已经乱了。
下面是CAN配置函数。[mw_shl_code=applescript,true]void CAN_Mode_Init(u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_DeInit(CAN1);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
CAN_StructInit(&CAN_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
CAN_InitStructure.CAN_TTCM=ENABLE;
CAN_InitStructure.CAN_ABOM=ENABLE;
CAN_InitStructure.CAN_AWUM=ENABLE;
CAN_InitStructure.CAN_NART=ENABLE;
CAN_InitStructure.CAN_RFLM=ENABLE;
CAN_InitStructure.CAN_TXFP=ENABLE;
CAN_InitStructure.CAN_Prescaler=4;
CAN_InitStructure.CAN_Mode=mode;
CAN_InitStructure.CAN_SJW=CAN_SJW_2tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;
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; //过滤器高16位
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; //过滤器低16位
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0Xffe0;//过滤器屏蔽高16位
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0X0006; //过滤器屏蔽低16位
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}[/mw_shl_code]
这样是调试图。主机一直发送UUUUUUU 从机接受的数据前6位正常,第7、8位异常。
一周热门 更多>