各位大佬,小弟目前在用stm32f103ze的芯片做can通讯,用周立功的USB—Can——e-u做接收端,500k波特率。但是我现在通过while循环,连续发ID为0x1314,0x1311的帧,帧间间隔为10ms(systick产生)。发现收到的can信号出现丢帧(通过时间标识鉴定),有时连着几个0x1314, 有时又相反;还有就是即使接收的信号按0x1314,0x1311顺序排列,但时间间隔不对(只有几十微秒)。我的can发送代码如下:
can单元初始化(关于波特率设置)
/*CANµ¥Ôª³õʼ»¯*/
CAN_InitStructure.CAN_TTCM=DISABLE; //MCR-TTCM ¹Ø±Õʱ¼ä´¥·¢Í¨ÐÅģʽʹÄÜ
CAN_InitStructure.CAN_ABOM=ENABLE; //MCR-ABOM ×Ô¶¯ÀëÏß¹ÜÀí
CAN_InitStructure.CAN_AWUM=ENABLE; //MCR-AWUM ʹÓÃ×Ô¶¯»½ÐÑģʽ
CAN_InitStructure.CAN_NART=DISABLE; //MCR-NART ½ûÖ¹±¨ÎÄ×Ô¶¯ÖØ´« DISABLE-×Ô¶¯ÖØ´«
CAN_InitStructure.CAN_RFLM=DISABLE; //MCR-RFLM ½ÓÊÕFIFO Ëø¶¨Ä£Ê½ DISABLE-Òç³öʱб¨ÎĻḲ¸ÇÔÓб¨ÎÄ
CAN_InitStructure.CAN_TXFP=DISABLE; //MCR-TXFP ·¢ËÍFIFOÓÅÏȼ¶ DISABLE-ÓÅÏȼ¶È¡¾öÓÚ±¨Îıêʾ·û
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //Õý³£¹¤×÷ģʽ
CAN_InitStructure.CAN_SJW=CAN_SJW_2tq; //BTR-SJW ÖØÐÂͬ²½ÌøÔ¾¿í¶È 2¸öʱ¼äµ¥Ôª
CAN_InitStructure.CAN_BS1=CAN_BS1_11tq; //BTR-TS1 ʱ¼ä¶Î1 Õ¼ÓÃÁË5¸öʱ¼äµ¥Ôª
CAN_InitStructure.CAN_BS2=CAN_BS2_6tq; //BTR-TS1 ʱ¼ä¶Î2 Õ¼ÓÃÁË3¸öʱ¼äµ¥Ôª
CAN_InitStructure.CAN_Prescaler =8; ////BTR-BRP ²¨ÌØÂÊ·ÖƵÆ÷ ¶¨ÒåÁËʱ¼äµ¥ÔªµÄʱ¼ä³¤¶È 36/(1+11+6)/4=0.5Mbps
CAN_Init(CAN1, &CAN_InitStructure);
}
can发送函数
uint8_t CAN_SetMsg(uint32_t id,uint16_t data)
{
uint16_t i=0;
uint8_t TxMailbox;
// TxMessage.StdId=0x10;
TxMessage.ExtId=id; //ʹÓõÄÀ©Õ¹ID
TxMessage.IDE=CAN_ID_EXT; //À©Õ¹Ä£Ê½
TxMessage.RTR=CAN_RTR_DATA; //·¢Ë͵ÄÊÇÊý¾Ý
TxMessage.DLC=2; //Êý¾Ý³¤¶ÈΪ1×Ö½Ú
TxMessage.Data[0]=(data&0xff00)>>8; //È¡Êý¾Ý¸ß8λ·ÅÈëdata[0]
TxMessage.Data[1]=(data&0xff); //È¡Êý¾ÝµÍ8λ·ÅÈëdata[1]
TxMailbox=CAN_Transmit(CAN1, &TxMessage);
i=0;
while((CAN_TransmitStatus(CAN1, TxMailbox)==CAN_TxStatus_Failed)&&(i<0XFFF))i++; //µÈ´ý·¢ËͽáÊø
if(i>=0XFFF)return 1;
delay_ms(10);
return 0;
}
main函数的连环发送
while(1)
{
uint8_t state=0;
/*ÉèÖÃҪͨ¹ýCAN·¢Ë͵ÄÐÅÏ¢*/
state=CAN_SetMsg(0x1314,ADC_ConvertedValue);
//LED1_ON;
//delay_ms(10);
state=CAN_SetMsg(0x1311,ADC_ConvertedValue);
}
求助各位,你们can通讯调试的时候碰到过这种问题吗?你们在这种发送多id的帧信号(程序一运行就开始以帧间隔为10ms发送)时是如何发送的啊?中断发送能解决这个问题吗?
|