STM32 CAN 正常模式接收失败
参照战舰的STM32实验25的历程进行修改,但是单片机之间通讯出了问题,一块用来发送数据,在CANL、CANH之间的另一端单片机收数据但是接收没法进中断。voidUSB_...
参照战舰的STM32实验25的历程进行修改 ,但是单片机之间通讯出了问题,一块用来发送数据,在CANL、CANH之间的另一端单片机收数据但是接收没法进中断。
void USB_LP_CAN1_RX0_IRQnHandler(void)
{
CanRxMsg RxMessage;
CAN_Receive(CAN1, 0, &RxMessage);
//for(uint8_t i=0;i<8;i++)
LCD_WriteEnglishSring(20,4,"succeed!");
}
uint8_t Can_Send_Msg(uint8_t* msg,uint8_t len)
{
uint8_t mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=0x1BC;
TxMessage.ExtId=0x12;
TxMessage.IDE=0;
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)==CANTXFAILED)&&(i<0XFFF))i++;
if(i>=0XFFF)return 1;
return 0;
}
uint8_t Can_Receive_Msg(uint8_t *buf)
{
uint32_t i;
CanRxMsg RxMessage;
if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0;
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
for(i=0;i<8;i++)
buf[i]=RxMessage.Data[i];
return RxMessage.DLC;
}
程序返回时:if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0;
始终为零。
while(1)
{
for(i=0;i<8;i++)
{
canbuf[i]=i*2;
}
res=Can_Send_Msg(canbuf,8);
if(!res) GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);
key=Can_Receive_Msg(buf);
} 展开
void USB_LP_CAN1_RX0_IRQnHandler(void)
{
CanRxMsg RxMessage;
CAN_Receive(CAN1, 0, &RxMessage);
//for(uint8_t i=0;i<8;i++)
LCD_WriteEnglishSring(20,4,"succeed!");
}
uint8_t Can_Send_Msg(uint8_t* msg,uint8_t len)
{
uint8_t mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=0x1BC;
TxMessage.ExtId=0x12;
TxMessage.IDE=0;
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)==CANTXFAILED)&&(i<0XFFF))i++;
if(i>=0XFFF)return 1;
return 0;
}
uint8_t Can_Receive_Msg(uint8_t *buf)
{
uint32_t i;
CanRxMsg RxMessage;
if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0;
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
for(i=0;i<8;i++)
buf[i]=RxMessage.Data[i];
return RxMessage.DLC;
}
程序返回时:if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0;
始终为零。
while(1)
{
for(i=0;i<8;i++)
{
canbuf[i]=i*2;
}
res=Can_Send_Msg(canbuf,8);
if(!res) GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);
key=Can_Receive_Msg(buf);
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询