cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 CAN bus: A sends data to B nodes.

JWang.31
Associate II

I have 2 nodes, A and B, they set filters for both, now A is sending data frame, B can receive the data from A. but A originally can receive data from B, but after around 1-3 mins the receiving data mainly from itself, minorly from B. but if i reset the board, node A will receive B's data for a while then change to the status:(majority its own data, nimority B's data).

#include "can_config.h"
#include "usart.h"
 
uint32_t mask_id = 0x03;
uint16_t CAN_ID;
uint8_t CAN_DATA0,CAN_DATA1,CAN_DATA2,CAN_DATA3,CAN_DATA4,CAN_DATA5,CAN_DATA6,CAN_DATA7;
uint8_t CanFlag; //,Display
void CAN_Configuration(void)
{
  CAN_InitTypeDef        CAN_InitStructure;  // CAN mode struct
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;  // CAN filter struct
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); // enable can1 clock.
	
  NVIC_Configuration();
  GPIO_Configuration(); // configure the can IO
	
  /* CAN register init */
  CAN_DeInit(CAN1);
  CAN_StructInit(&CAN_InitStructure);
 
  /* CAN cell init */
  CAN_InitStructure.CAN_TTCM = DISABLE; /* ʱ¼ä´¥·¢½ûÖ¹, ʱ¼ä´¥·¢£ºCANÓ²¼þµÄÄÚ²¿¶¨Ê±Æ÷±»¼¤»î£¬²¢ÇÒ±»ÓÃÓÚ²úÉúʱ¼ä´�? */
  CAN_InitStructure.CAN_ABOM = ENABLE; /* ×Ô¶¯Àë�?ß½ûÖ¹£¬×Ô¶¯Àë�?ߣºÒ»µ©Ó²¼þ¼à¿Øµ½128´Î11¸öÒþ�?Ô룬¾�?×Ô¶¯�?˳öÀë�?ß״̬¡£ÔÚÕâÀïÒªÈí¼þÉ趨ºó²ÅÄÜ�?˳ö */
  CAN_InitStructure.CAN_AWUM = ENABLE;  /* ×Ô¶¯»½�?ѽûÖ¹£¬Ó�?±¨ÎÄÀ´µÄʱºò×Ô¶¯�?˳ö�?�?Ãß	*/
  CAN_InitStructure.CAN_NART = DISABLE; /* ±¨ÎÄÖØ´«, Èç¹û´íÎóÒ»Ö±´«µ½³É¹¦Ö¹£¬·ñÔòÖ»´«Ò»´Î */
  CAN_InitStructure.CAN_RFLM = DISABLE; /* receive FIFO lock, 1-- after locking, newly coming data will be ignored.0--½ÓÊÕµ½�?µı¨ÎÄÔò¸²¸ÇÇ°Ò»±¨ÎÄ	*/
  CAN_InitStructure.CAN_TXFP = DISABLE;  /* FIFO priority, 0 means decide by identifier, 1 means decided by request sequence	*/
	//CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack; /*better for testing. when 2 boards communication, then it is mode_normal 	*/
  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; /* ģʽ	*/
	//CAN_InitStructure.CAN_Mode = CAN_Mode_Silent ;
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;      /* ÖØ�?Â�?¬²½Ìø¿í£¬Ö»Ó�?canÓ²¼þ´¦ÓÚ³õʼ»¯Ä£Ê½Ê±²ÅÄÜ·ÃÎÊÕâ¸ö¼Ä´æÆ÷ */
  CAN_InitStructure.CAN_BS1 = CAN_BS1_5tq;      /* ʱ¼ä¶Î1 */
  CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;      /* ʱ¼ä¶Î2 */
  CAN_InitStructure.CAN_Prescaler = 7;         /* ²¨ÌØÂÊÔ¤·ÖƵÊý */  
 
  /* ²¨ÌØÂʼÆËã·½·¨ */
  /* CANbps= Fpclk/((BRP+1)*((Tseg1+1)+(Tseg2+1)+1)  ´Ë´¦¼ÆËãΪ  CANbps=36000000/(45*(4+3+1))=100kHz */   														  //´Ë´¦Tseg1+1 = CAN_BS1_8tp
  /* ÅäÖôó·½�?ò: Tseg1>=Tseg2  Tseg2>=tq; Tseg2>=2TSJW */
  // **** configure 
	CAN_Init(CAN1,&CAN_InitStructure);
  if (CAN_Init(CAN1,&CAN_InitStructure) == CANINITFAILED) 		
  {
    /* ³õʼ»¯Ê±�?ÈÉèÖÃCAN_MCRµÄ³õʼ»¯Î» */  														
    /* È»ºó²é¿´Ó²¼þÊÇ·ñÕæµÄÉèÖÃ�?ËCAN_MSRµÄ³õʼ»¯Î»À´È·È�?ÊÇ·ñ½øÈë�?˳õʼ»¯Ä£Ê½  */		 													                  
  }	
	CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;  /* Äܹ»�?¨¹ý¸Ã¹ýÂËÆ÷µÄ±¨ÎÄ´æµ½fifo0Ö�? */
  CAN_FilterInitStructure.CAN_FilterNumber=0;     /* ¹ýÂËÆ÷0 */
  CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;  /* can be IDlist,IDmask, the mask is relatively complex*/
  CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; /* 32λ */
 
  
	CAN_FilterInitStructure.CAN_FilterIdHigh= ((((u32)0x2616>16;		//ҪɸѡµÄID¸ßλ 
	CAN_FilterInitStructure.CAN_FilterIdLow= (((u32)0x2616


_legacyfs_online_stmicro_images_0693W00000Kcaem.png

0 REPLIES 0