AnsweredAssumed Answered

Help review S12G128 CAN code

Question asked by Hanson He Employee on Jun 22, 2017
Latest reply on Jun 23, 2017 by lama

Dear,

 

Could you help review below code, whether it have some config problem.

Crystal: 4MHz

CAN Baud: 250Kbps

MCU: S12G128 64pin

 

 

void CAN2_NewInit(void)

{

     /* Initialise the CAN */

    CANCTL1_CANE = 1;                   /* enable CAN module */

    CANCTL0 = 0x01;                     /* enter init mode */

    while(!(CANCTL1_INITAK));                /* wait for init mode */

 

    CANCTL1 = 0x80;                              /* enable CAN module, Loopback Mode, Ext OSC */

    CANBTR0 = 0xC1;                              /* sync jump - 4 Tq clocks, prescalar = 3 */

    CANBTR1 = 0x01;                              /* Tseg = 3, Tseg1 = 10, 1 sample per bit */

    CANIDAC = 0x10;                          /* four 16-bit filters */

 

    CANIDAR0 = 0x20;                    /* Filter 0, ID=0x100 Standard Identifier */

    CANIDMR0 = 0x00;

    CANIDAR1 = 0x00;

    CANIDMR1 = 0x07;                    /* AM[2:0] = 7 to receive standard identifiers */

 

    CANIDAR2 = 0x00;                    /* Filter 1, ID=0x0000 */

    CANIDMR2 = 0x00;

    CANIDAR3 = 0x00;

    CANIDMR3 = 0x07;                    /* AM[2:0] = 7 to receive standard identifiers */

 

    CANIDAR4 = 0x00;                        /* Filter 2, ID=0x0000 */

    CANIDMR4 = 0x00;

    CANIDAR5 = 0x00;

    CANIDMR5 = 0x07;                    /* AM[2:0] = 7 to receive standard identifiers */

 

    CANIDAR6 = 0x00;                        /* Filter 3, ID=0x0000 */

    CANIDMR6 = 0x00;

    CANIDAR7 = 0x00;

    CANIDMR7 = 0x07;                    /* AM[2:0] = 7 to receive standard identifiers */

 

    CANCTL0 = 0x00;                              /* exit init mode */

    while(CANCTL1_INITAK);                                              /* wait until module exits init mode */

 

    while(!(CANCTL0_SYNCH));                           /* wait for CAN module to synch */

 

    CANRFLG = 0xC3;                                                                          /* reset Rx flags */

}

//byte CAN2_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte Length,const byte *Data)

void CAN2_NewSendFrame(byte Length, byte *Data)

{

      byte txbuffer;

      byte txCounter = 0;

     

      while(txCounter < Length)

      {

       

          while (!CANTFLG);               /* Wait for empty Tx Buffer */

          CANTBSEL = CANTFLG;                      /* Select the empty Tx Buffer */

          txbuffer = CANTBSEL;            /* Save the empty buffer */

 

          CANTXIDR0 = 0x18;               /* load message id value to ID regs */  //18FF0C20

          CANTXIDR1 = 0xFF;

          CANTXIDR2 = 0x0C;

          CANTXIDR3 = 0x20;

         

          CANTXDSR0 = *Data;                         /* load data to send */

 

          CANTXDLR = 0x01;                                    /* set data length */

          CANTXTBPR = 0x80;                       /* set data buffer priority */

 

          CANTFLG = txbuffer;             /* start transmission */

 

          while(!(CANTFLG & txbuffer));   /* wait for Tx to complete */

         

          txCounter++;

      }

}

 

//byte CAN2_ReadFrame(dword *MessageID,byte *FrameType,byte *FrameFormat,byte *Length,byte *Data)

void CAN2_NewReceiveFrame(dword *MessageID, byte Length, byte *Data)

{

      byte rxCounter = 0;

      while(rxCounter < Length)

      {

          if(CANRFLG_RXF)                 /* has a message been received ? */

          {

              *Data = CANRXDSR0;            /* Display transmitted PORTB on LEDs */

              CANRFLG_RXF = 1;            /* Clear RXF */

          }

         

          rxCounter++;

      }

}

Outcomes