CAN receive data is not correct,Why?

CANculator is a software development tool used for easier CAN baud rate calculation and manipulation of the CAN bus parameters.
Post Reply
Author
Message
lglgang
Posts: 40
Joined: 03 May 2013 04:07

CAN receive data is not correct,Why?

#1 Post by lglgang » 26 Aug 2020 09:44

the mcu is stm32f103ze

we use CANculator to create the code.

and ref the help code with the mikro c for arm.


We use CAN.
but we can only send can data to pc from mcu.
or receive data from pc.
the send data from mcu is correct , I can see the data is ok in pc.
But, when I use mcu to receive the CAN data from PC with can card.
The data we receive from pc through mcu all is wrong.
include that:
Msg_Rcvd = CAN1Read(0, &Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags);

the Rx_ID is not correct.
the RxTx_Data is not correct too.

We try my best to change some method, but I can not get any correct result.

Code: Select all

sbit RED_LED at GPIOB_ODR.B5;

sbit MOTOR_PUL at GPIOC_ODR.B9;
sbit MOTOR_DIR at GPIOC_ODR.B8;



/*Place/Copy this part in declaration section*/
const unsigned int SJW = 1;
const unsigned int BRP = 4;
const unsigned int PHSEG1 = 7;
const unsigned int PHSEG2 = 2;
const unsigned int PROPSEG = 8;
const unsigned int CAN_CONFIG_FLAGS =
	_CAN_CONFIG_AUTOMATIC_RETRANSMISSION &
	_CAN_CONFIG_RX_FIFO_NOT_LOCKED_ON_OVERRUN &
	_CAN_CONFIG_TIME_TRIGGERED_MODE_DISABLED &
	_CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER &
	_CAN_CONFIG_WAKE_UP;



 unsigned int Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags;  // can flags
unsigned int Rx_Data_Len;                                    // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const unsigned long ID_1st = 0xa1, ID_2nd = 0xa1;              // node IDs
const unsigned long ID_3rd = 0xa1, ID_4th = 0xa1;              // node IDs
unsigned long Rx_ID;
unsigned long Rx_ID1;
char txt[12];
unsigned long count1,counta;
long i;
unsigned char flag_run;
long tmpa,tmpb;


void main() {
Can_Send_Flags = _CAN_TX_XTD_FRAME & _CAN_TX_NO_RTR_FRAME;
//Can_Rcv_Flags=0;

UART2_Init(9600);
 i=1;
 counta=20000;
 flag_run=1;

  GPIO_Digital_Output(&GPIOB_BASE, _GPIO_PINMASK_5);
  GPIO_Digital_Output(&GPIOC_BASE, _GPIO_PINMASK_8 | _GPIO_PINMASK_9);
  
  GPIOE_ODR = 0;
/*Place/Copy this part in init section*/
//CAN1InitializeAdvanced(SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CAN_CONFIG_FLAGS, &_GPIO_MODULE_CAN1_PB89);
  /*Place/Copy this part in init section*/
CAN1InitializeAdvanced(SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CAN_CONFIG_FLAGS, &_GPIO_MODULE_CAN1_PB89);
CAN1SetOperationMode(_CAN_OperatingMode_Initialization);                    // set CONFIGURATION mode
//CANSetFilterScale32(0, _CAN_FILTER_ENABLED & _CAN_FILTER_ID_MASK_MODE & _CAN_FILTER_XTD_MSG, ID_1st, -1);

  CAN1SetOperationMode(_CAN_OperatingMode_Normal);               // set NORMAL mode

  RED_LED=1;
  while (1) {
  //i++;
  count1++;
  //RxTx_Data[0]=0xa1;
  //CAN1Write(ID_2nd, RxTx_Data, 8, Can_Send_Flags);
  
   Msg_Rcvd = CAN1Read(0, &Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
   
    if ((Rx_ID == 0xa1) || Msg_Rcvd) {                                       // if message received check id
      //GPIOE_ODR  = RxTx_Data[0] << 8;                                          // id correct, output data at PORTE
      //RxTx_Data[0]++ ; 
      delay_ms(200);                                                        // increment received data
      CAN1Write(ID_2nd, RxTx_Data, 1, Can_Send_Flags);                         // send incremented data back
    }
    
    
  Msg_Rcvd = CAN1Read(0, &Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags);
  Rx_ID1=Rx_ID;
  if(RxTx_Data[0]==0xa1)
  {
    RED_LED=0;
  }
  if(RxTx_Data[0]==0xa2)
  {
    RED_LED=1;
  }
                                                         // endless loop
  //  Msg_Rcvd = CAN1Read(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
     if(count1<counta)
     {
       MOTOR_PUL=flag_run;
     }
     else
     {
       MOTOR_PUL=0;
     }
     if(count1>2*counta)
     {
       count1=0;
     }

    if (Rx_ID != 0) {                                       // if message received check id
      //GPIOE_ODR  = RxTx_Data[0] << 8;                                          // id correct, output data at PORTE
      //RxTx_Data[0]++ ;
      //Rx_ID=0;
      if(RxTx_Data[0]==0) //cw
      {
      MOTOR_DIR=1;
      tmpa= RxTx_Data[1];
      tmpb= RxTx_Data[2];
      counta=(tmpa<<16)+(tmpb <<8)+RxTx_Data[3];
      count1=0;
      }
      if(RxTx_Data[0]==1)  //ccw
      {
        MOTOR_DIR=0;
        tmpa= RxTx_Data[1];
      tmpb= RxTx_Data[2];
      counta=(tmpa<<16)+(tmpb <<8)+RxTx_Data[3];
      count1=0;
      }
      if(RxTx_Data[0]==2)  //stop
      {
         flag_run=0;
         counta=100;
      }
                               RxTx_Data[0]=0x31;
                               RxTx_Data[1]=0x32;
                               RxTx_Data[2]=0x33;
                               RxTx_Data[3]=0x34;

      //RED_LED=0;                                                       // increment received data
      CAN1Write(ID_3rd, RxTx_Data, 8, Can_Send_Flags);                         // send incremented data back
    }
  }
}
anybody can give me some example?
thanks.

Post Reply

Return to “CAN Calculator”