This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Basic C question for CAN implementation

What I am trying to do is load a CAN frame with some calculated data and transmit it every 1s. Additionally I want to compare what I am sending with an incoming message, compare each byte and if they are not equal replace the value of the transmit byte value with it's corresponding value on the received frame. With the code below I am transmitting correctly but even though I am sending the 'receive' frame with different data I am not changing what I am sending. Help?

//initializing CAN control registers

 C1CSR = 0x41; // initializes CAN controller
 C1BTR = 0x3453; //50Kb baudrate
 BFLD(DP4,0x0020,0x0000);
 BFLD(DP4,0x0040,0x0040);
 C1GMS = 0xFFFF;

 //initializing CAN messgae object and registers
 CAN_OBJ[0].MCR = 0x5695;
 CAN_OBJ[0].UAR = 0x60D0;//ID=0x683
 CAN_OBJ[0].LAR = 0x0000;
 CAN_OBJ[0].MCFG = 0x68; //DLC=6,DIR=1(TX), StdId

//initializing data for CAN_OBJ[0]
 CAN_OBJ[0].Data[0] = 0x00;
 CAN_OBJ[0].Data[1] = 0x00;
 CAN_OBJ[0].Data[2] = 0x00;
 CAN_OBJ[0].Data[3] = 0x00;
 CAN_OBJ[0].Data[4] = 0x00;
 CAN_OBJ[0].Data[5] = 0x00;
 CAN_OBJ[0].Data[6] = 0x00;
 CAN_OBJ[0].Data[7] = 0x00;
//end of data bytes



//initializing CAN_OBJ[1]
CAN_OBJ[1].MCR = 0x5695;
CAN_OBJ[1].UAR = 0xE0DA; //ID of 0x6D7
CAN_OBJ[1].LAR = 0x0000;
CAN_OBJ[1].MCFG = 0x60; //DLC = 6, DIR=0(RX), StdID
CAN_OBJ[1].Data[0] = 0x00;
CAN_OBJ[1].Data[1] = 0x00;
CAN_OBJ[1].Data[2] = 0x00;
CAN_OBJ[1].Data[3] = 0x00;
CAN_OBJ[1].Data[4] = 0x00;
CAN_OBJ[1].Data[5] = 0x00;
CAN_OBJ[1].Data[6] = 0x00;
CAN_OBJ[1].Data[7] = 0x00;

//end of initializing CAN_OBJ[1]



//Following data objects are not used but still must be initialized. byte MSGVAL is set to invalid
 CAN_OBJ[2].MCR = 0x5555;//message object invalid
 CAN_OBJ[3].MCR = 0x5555;//message object invalid
 CAN_OBJ[4].MCR = 0x5555;//message object invalid
 CAN_OBJ[5].MCR = 0x5555;//message object invalid
 CAN_OBJ[6].MCR = 0x5555;//message object invalid
 CAN_OBJ[7].MCR = 0x5555;//message object invalid
 CAN_OBJ[8].MCR = 0x5555;//message object invalid
 CAN_OBJ[9].MCR = 0x5555;//message object invalid
 CAN_OBJ[10].MCR = 0x5555;//message object invalid
 CAN_OBJ[11].MCR = 0x5555;//message object invalid
 CAN_OBJ[12].MCR = 0x5555;//message object invalid
 CAN_OBJ[13].MCR = 0x5555;//message object invalid
 CAN_OBJ[14].MCR = 0x5555;//message object invalid

// Resetting the CSR otherwise the module would get into an initialization loop
C1CSR = 0x0000;
RTCinit();

}







void CAN_NQS_time_date(void)
{
 CAN_OBJ[0].Data[0] = time.hour;
 CAN_OBJ[0].Data[1] = time.minute;
 CAN_OBJ[0].Data[2] = time.day;
 CAN_OBJ[0].Data[3] = time.month;
 CAN_OBJ[0].Data[4] = time.year1;
 CAN_OBJ[0].Data[5] = time.year2;
 CAN_OBJ[0].Data[6] = 0x00;
 CAN_OBJ[0].Data[7] = 0x00;


for (i=0; i=7;i++)
        {
                if (CAN_OBJ[0].Data[i] != CAN_OBJ[1].Data[i])
                        {
                                CAN_OBJ[0].Data[i] = CAN_OBJ[1].Data[i];
                        }
                else
                        {
                                CAN_OBJ[0].Data[i] = CAN_OBJ[0].Data[i];
                        }
                break;
        }



 CAN_OBJ[0].MCR = 0xe7ff;
}

  • First off, even if you do store the new values and transmit them are you sure you are using the new values for your calculations then on?
    It maybe more helpful that you store the received values in an array in your actual calculation file and do the comparison there. That way you can use the new values for your calculations as well. That brings up a question on my side.
    Can I store values in an integer array? For ex, using AJ's code can I store the hex values from the received frames in an integer array? Would the type conversion occur automatically or do I have to do hard code it?

  • I don't know anything about the chip you are using, so I can't help with the receive/transmit problems.

    In this code - what do you think the second assign does? It assigns from itself to itself. Is that a hard requirement of the CAN controller?

    if (CAN_OBJ[0].Data[i] != CAN_OBJ[1].Data[i]) {
        CAN_OBJ[0].Data[i] = CAN_OBJ[1].Data[i];
    } else {
        CAN_OBJ[0].Data[i] = CAN_OBJ[0].Data[i];
    }
    

    But wouldn't it be easier to just have:

    for (i = 0; i < 8; i++) {
        CAN_OBJ[0].Data[i] = CAN_OBJ[1].Data[i];
    }
    


    Since you don't seem to care if the data differs or not, why even bother to compare? Just copy all contents of the received data into the transmit buffer.