CAN problem, beginner

General discussion on mikroC.
Post Reply
Author
Message
PimI
Posts: 8
Joined: 30 Aug 2011 08:14

CAN problem, beginner

#1 Post by PimI » 08 Sep 2011 08:21

Dear,

I'm trying to make a connection between two pic18f458's over CAN bus. I'm using two mcp2551 to connect to the bus system. I'm experiencing difficulty's and I'm not sure what it is that i'm doing wrong.

When the first controller starts sending, i'm seeing the same data over and over on my scope ( on the txd of mcp2551 ). On the receive of my second pic (receiver) i see the same data comming in (so i assume bus is connected correctly). when I look at the tx of my second pic i see that on init it sends data 1 time, right after the first transmit of the initiating pic. (seems like it's answering but nothing further happens). when i look at the rx of my first pic i see roughtly the same data that is send by that same pic. (guessing that it is an error frame or it's trying to re-send over and over the same message because no ack is given by receiver)

If someone know's what i'm doing wrong and is able to help me i'd be very gratefull


tx (first pic) code:

Code: Select all

unsigned short aa, aa1, len, aa2;
unsigned char data[8];
long id;
unsigned short zr;


void main(){
     PORTC = 0;
     TRISC = 0;
     PORTD = 0;
     TRISD = 0;
     aa = 0;
     aa1 = 0;
     aa2 = 0;
     
     aa1 = CAN_TX_PRIORITY_0 &
           CAN_TX_XTD_FRAME &
           CAN_TX_NO_RTR_FRAME;
           
     aa =  CAN_CONFIG_SAMPLE_THRICE &
           CAN_CONFIG_PHSEG2_PRG_ON &
           CAN_CONFIG_STD_MSG &
           CAN_CONFIG_DBL_BUFFER_ON &
           CAN_CONFIG_VALID_XTD_MSG &
           CAN_CONFIG_LINE_FILTER_OFF;
           
     data[0] = 0;
     
     // init CAN
     CANInitialize(1,1,3,3,1,aa);
     
     // set can to config mode
     CANSetOperationMode(CAN_MODE_CONFIG,0xFF);
     
     id = -1;
     Delay_ms(5000);
     //set all mask1 bits to ones
     CANSetMask(CAN_MASK_B1,ID,CAN_CONFIG_XTD_MSG);
     //set all mask2 bits to ones
     CANSetMask(CAN_MASK_B2,ID,CAN_CONFIG_XTD_MSG);
     // set id of filter B1_F1 to 3
     CANSetFilter(CAN_FILTER_B2_F3,3,CAN_CONFIG_XTD_MSG);
     // set can to normal mode
     CANSetOperationMode(CAN_MODE_NORMAL,0xFF);
     
     //PORTD = 0xFF;
     id = 12111;
     CANWrite(id,data,1,aa1);
     
     while(1){
              zr = CANRead(&id, data , &len, &aa2);
              if((id == 3) && zr){
                  PORTD = 0xAA;
                  PORTC = data[0];
                  data[0]++;

                  delay_ms(18);
                  CANWrite(id, data, 2 , aa1);
              }
     }
 }

rx ( second pic ) code:

Code: Select all

unsigned short aa, aa1, len, aa2;
unsigned char data[8];
long id;
unsigned short zr, cont, oldstate;


void main(){
     PORTC = 0;
     TRISC = 0;
     PORTD = 0;
     TRISD = 0;
     aa = 0;
     aa1 = 0;
     aa2 = 0;

     aa1 = CAN_TX_PRIORITY_0 &
           CAN_TX_XTD_FRAME &
           CAN_TX_NO_RTR_FRAME;

     aa =  CAN_CONFIG_SAMPLE_THRICE &
           CAN_CONFIG_PHSEG2_PRG_ON &
           CAN_CONFIG_STD_MSG &
           CAN_CONFIG_DBL_BUFFER_ON &
           CAN_CONFIG_VALID_XTD_MSG &
           CAN_CONFIG_LINE_FILTER_OFF;

     data[0] = 0;

     // init CAN
     CANInitialize(1,1,3,3,1,aa);

     // set can to config mode
     CANSetOperationMode(CAN_MODE_CONFIG,0xFF);

     id = -1;

     //set all mask1 bits to ones
     CANSetMask(CAN_MASK_B1,ID,CAN_CONFIG_XTD_MSG);
     //set all mask2 bits to ones
     CANSetMask(CAN_MASK_B2,ID,CAN_CONFIG_XTD_MSG);
     // set id of filter B1_F1 to 3
     CANSetFilter(CAN_FILTER_B2_F3,12111,CAN_CONFIG_XTD_MSG);
     // set can to normal mode
     CANSetOperationMode(CAN_MODE_NORMAL,0xFF);

     //PORTD = 0xFF;
     id = 3;
     //CANWrite(id,data,1,aa1);

     while(1){
              oldstate = 0;
              zr = CANRead(&id, data , &len, &aa2);
              if((id == 12111) & zr){
                  PORTD = 0xAA;
                  PORTC = data[0];
                  data[0]++;

                  delay_ms(20);
                  CANWrite(id, data, 2 , aa1);
              }
     }
 }

Thank you

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: CAN problem, beginner

#2 Post by p.erasmus » 08 Sep 2011 08:41

You are sending XTD message why are you configuring for STD message
Your Config should look like this

Code: Select all

 Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &    // form value to be used
                   _CAN_CONFIG_PHSEG2_PRG_ON &    // with CANInit
                   _CAN_CONFIG_XTD_MSG &
                   _CAN_CONFIG_DBL_BUFFER_ON &
                   _CAN_CONFIG_VALID_XTD_MSG &
                   _CAN_CONFIG_LINE_FILTER_OFF;

You check if a message is receive like this

Code: Select all


Msg_Rcvd = CANRead(&MsgIDRx ,Rx_Data , &Rx_Data_Len,&Can_Rcv_Flags);
     if(Msg_Rcvd!=0)
   {
     Msg_Rcvd = 0;
  switch (MsgIDRx)
  {
      case 12111:
       // your code to hanle message
    break;
   // next case 

   }// end switch
  }//end if

P.Erasmus
Saratov,Russia
--------------------------------------------------------------

PimI
Posts: 8
Joined: 30 Aug 2011 08:14

Re: CAN problem, beginner

#3 Post by PimI » 08 Sep 2011 10:08

p.erasmus thanks for replying.

I've implemented my code the way you suggested, but i'm not seeing any difference if i'm honest.

For debugging i've connected tx of first pic to rx of second and tx of second to rx of first.
i've added some pictures, the first one is the signal that i see on tx(1) -> rx(2). second is the signal on the line tx(2) -> rx(1)

I noticed that the second picture has a signal that is (a lot) weaker. In the very beginning (init) there are some stronger bit's but this will weaken very soon.
I've added a picture of the way i've connected the 2nd controller, the first one is on a easypic5 board with portb pulled-up but i am not expecting wrong connections there.

both controllers are on a 8 mhz frequenty. i've added pullups to the second controller.
again the code as it is now.

i really don't know anymore where to look, thanks again for any advice

code first:

Code: Select all

unsigned short aa, aa1, len, aa2;
unsigned char data[8];
long id;
unsigned short zr;


void main(){
     PORTC = 0;
     TRISC = 0;
     PORTD = 0;
     TRISD = 0;
     aa = 0;
     aa1 = 0;
     aa2 = 0;
     
     aa1 = CAN_TX_PRIORITY_0 &
           CAN_TX_XTD_FRAME &
           CAN_TX_NO_RTR_FRAME;
           
     aa = CAN_CONFIG_SAMPLE_THRICE &    // form value to be used
          CAN_CONFIG_PHSEG2_PRG_ON &    // with CANInit
          CAN_CONFIG_XTD_MSG &
          CAN_CONFIG_DBL_BUFFER_ON &
          CAN_CONFIG_VALID_XTD_MSG &
          CAN_CONFIG_LINE_FILTER_OFF;
           
     data[0] = 0;
     
     // init CAN
     CANInitialize(1,1,3,3,1,aa);
     
     // set can to config mode
     CANSetOperationMode(CAN_MODE_CONFIG,0xFF);
     
     id = -1;

     //set all mask1 bits to ones
     CANSetMask(CAN_MASK_B1,id,CAN_CONFIG_XTD_MSG);
     //set all mask2 bits to ones
     CANSetMask(CAN_MASK_B2,id,CAN_CONFIG_XTD_MSG);
     // set id of filter B1_F1 to 3
     CANSetFilter(CAN_FILTER_B2_F3,3,CAN_CONFIG_XTD_MSG);
     // set can to normal mode
     CANSetOperationMode(CAN_MODE_NORMAL,0xFF);
     

     id = 12111;
     CANWrite(id,data,1,aa1);
     
     while(1){

      zr = CANRead(&id ,data , &len,&aa2);
       if(zr!=0)
       {
           zr = 0;
              switch (id)
              {
                  case 3:
                   PORTD = 0xAA;
                   PORTC = data[0];
                   data[0]++;

                   delay_ms(1000);
                   CANWrite(id, data, 2 , aa1);
                break;
               // next case

               }// end switch
        }//end if
       }
   }
code second (rx)

Code: Select all

unsigned short aa, aa1, len, aa2;
unsigned char data[8];
long id;
unsigned short zr, cont, oldstate;


void main(){
     PORTC = 0;
     TRISC = 0;
     PORTD = 0;
     TRISD = 0;
     aa = 0;
     aa1 = 0;
     aa2 = 0;

     aa1 = CAN_TX_PRIORITY_0 &
           CAN_TX_XTD_FRAME &
           CAN_TX_NO_RTR_FRAME;

     aa = CAN_CONFIG_SAMPLE_THRICE &    // form value to be used
          CAN_CONFIG_PHSEG2_PRG_ON &    // with CANInit
          CAN_CONFIG_XTD_MSG &
          CAN_CONFIG_DBL_BUFFER_ON &
          CAN_CONFIG_VALID_XTD_MSG &
          CAN_CONFIG_LINE_FILTER_OFF;

     data[0] = 0;

     // init CAN
     CANInitialize(1,1,3,3,1,aa);

     // set can to config mode
     CANSetOperationMode(CAN_MODE_CONFIG,0xFF);

     id = -1;

     //set all mask1 bits to ones
     CANSetMask(CAN_MASK_B1,id,CAN_CONFIG_XTD_MSG);
     //set all mask2 bits to ones
     CANSetMask(CAN_MASK_B2,id,CAN_CONFIG_XTD_MSG);
     // set id of filter B1_F1 to 3
     CANSetFilter(CAN_FILTER_B2_F3,12111,CAN_CONFIG_XTD_MSG);
     // set can to normal mode
     CANSetOperationMode(CAN_MODE_NORMAL,0xFF);

     //PORTD = 0xFF;
     id = 3;
     //CANWrite(id,data,1,aa1);

     while(1){

     zr = CANRead(&id ,data , &len,&aa2);
     if(zr!=0)
     {
         zr = 0;
            switch (id)
            {
                case 12111:
                 PORTD = 0xAA;
                 PORTC = data[0];
                 data[0]++;

                 delay_ms(1000);
                 CANWrite(id, data, 2 , aa1);
              break;
             // next case

             }// end switch
      }//end if
     }
 }
Attachments
pic2
pic2
IMAG0043.jpg (45.54 KiB) Viewed 4594 times
second signal
second signal
sig2.PNG (251.95 KiB) Viewed 4594 times
first signal
first signal
sig1.PNG (228.72 KiB) Viewed 4594 times

PimI
Posts: 8
Joined: 30 Aug 2011 08:14

Re: CAN problem, beginner

#4 Post by PimI » 09 Sep 2011 09:10

o i forgot to say i've added capacitors on the vcc/vdd's and only 1 pullup per line ( so total of 2 pullups ) is added.

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: CAN problem, beginner

#5 Post by p.erasmus » 09 Sep 2011 10:27

both controllers are on a 8 mhz frequenty. i've added pullups to the second controller.
There should be no Pullups on any CAN lines ,what baud rate are you trying to achieve with the 8Mhz XT ?
The second picture shows a good CAN signal ,the first is a bit confusing as when you send something from the first pic your Tx should look
as the second picture ,
post a schematic of your CAN bus wireing and connections ,the code should work I dont see any issues looking at it but I do need to test it to
make sure which I might be able to do on the weekend ,now I am at work have to keep the boss happy
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

PimI
Posts: 8
Joined: 30 Aug 2011 08:14

Re: CAN problem, beginner

#6 Post by PimI » 09 Sep 2011 12:46

p.erasmus, again thanks for your help

for debugging i'm not using the bus itself (so no mcp2551's involved). I've just hooked up the tx of the first controller to the rx of the second controller. Because of the fact that these lines are " in rest " active, and a zero is pulled down, i added pullup resistors to those wires.

I assumed that the standard can baud rate is 125 kbps? or am i wrong in assuming?

Yes, i too am confused by the effect on the first picture. On init it will show like a half of a transfer, then it breaks down and this "rubbish" will only be on the line. Best guess is that this is noise of the powersupply but i've added 100uF capacitor on all vcc/vdd's.
I am assuming that the "breakdown" of the signal has something to do with the fact that the controller can not get the total message on the bus as an other node is transferring again.
this is what i read about it in the datasheet:
If the message has not yet started transmission, or if the message started but is interrupted by loss
of arbitration or an error, the abort will be processed
in the scematic there is no exact 18f458 but ive chosen the closest match as there was nog pic18f458 for eagle. RB2 is on 18f458 canTX and RB3 is canRX

thanks again for your help
Attachments
cicruit1
cicruit1
circuit.PNG (39.23 KiB) Viewed 4574 times
circuit.PNG
circuit.PNG (39.23 KiB) Viewed 4574 times

PimI
Posts: 8
Joined: 30 Aug 2011 08:14

Re: CAN problem, beginner

#7 Post by PimI » 12 Sep 2011 12:15

Ok i realised that it is no good to test without full setup so i added the 2 mcp2551's. They are connected as they should, im posting complete schematic with this post..

weird thing is that i found another post of you in another topic where you've tested the code of this other bloke that had problems with his can. You say that the code there is tested positive but when i do run the exact same code on my system it won't work either.. gives the same rubbish as it does for almost a week now.. i'm desperate to get this to work.. thanks for all your help!
Attachments
second halve circuit
second halve circuit
c2.PNG (23.27 KiB) Viewed 4570 times
first halve circuit
first halve circuit
c1.PNG (40.89 KiB) Viewed 4570 times

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: CAN problem, beginner

#8 Post by p.erasmus » 12 Sep 2011 19:56

@PimI

Can you make only a small schematic with your CAN ,MCP2551 and the 2 micro's with the exact values you are useing as you mentioned PIC18F458 micros the schematic shows 4455 micros
the CAN bus show 680 Ohm resistors and some caps ,if this is the true values it wil never work
Read the Bosch 2.0 CAN specification regading the physical layer

Another point is you can never test CAN only with 1 controller or just the bus (you can test the CAN in loop back mode but this is a bit advance and the mikro lib does not support this)
you need to get the hardware wired correctly configure the micros for the baud rate and then it will work
I am quite sure you have a hardware problem.

you can not assume your baud rate is 125Kb/sec it depends on the Oscillator frequency and the number of TQ's you set in the init function

lets start this was post the schematic and we get the hardware running then we make a small code snending 1 message which the second controller should Receive and blink an led from there
your CAN will be working
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

PimI
Posts: 8
Joined: 30 Aug 2011 08:14

Re: CAN problem, beginner

#9 Post by PimI » 14 Sep 2011 13:49

@p.erasmus

I've fixed the problem. The capacitors in the bus where no good, and i've ordered new mcp's so i think one of those where destroyed. Thanks for al the help. Here is the code that i'm 100% sure of that is working


node 1:

Code: Select all

/*
* Project_name:
     Can 2st node
* Copyright:
     (c) Mikroelektronika, 2010.
* Revision History
     20101116:
    - Initial release;
    - 20101116(RR);
* Description:
     This is code that shows simple connection of two CAN modules. This is for 2st node.
         Modules can only work with MCUs that have CAN module integratet.
* Test configuration:
     MCU:                 ac:PIC18F458
                          http://ww1.microchip.com/downloads/en/DeviceDoc/41159e.pdf

     Dev.Board:           ac:EasyPIC6
                          http://www.mikroe.com/en/tools/easypic6/

     Oscillator:          External Clock 08.0000 MHz

     Ext. Modules:        ac:CAN_Board
                          http://www.mikroe.com/eng/products/view/128/can-1-board/

     SW:                  mikroC PRO for PIC v4.15
                          http://www.mikroe.com/en/compilers/mikroc/pro/pic/

* NOTES:
  - For regular functionality MCU must work on 8.0000 MHz oscillator.
  - Connect CAN Board on PortB and turn On Switches 2 and 6 on CAN Board
  - Turn On PortC LEDs (SW9.3)
*/

// End LCD module connections


unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags
unsigned char Rx_Data_Len;                                   // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const long ID_1st = 12111u, ID_2nd = 3;                       // node IDs
long Rx_ID;

void main() {

  PORTC = 0;                                                // clear PORTC
  TRISC = 0;                                                // set PORTC as output

  Can_Init_Flags = 0;                                       //
  Can_Send_Flags = 0;                                       // clear flags
  Can_Rcv_Flags  = 0;                                       //

  Can_Send_Flags = CAN_TX_PRIORITY_0 &                     // form value to be used
                   CAN_TX_XTD_FRAME &                      //     with CANWrite
                   CAN_TX_NO_RTR_FRAME;

  Can_Init_Flags = CAN_CONFIG_SAMPLE_THRICE &              // form value to be used
                   CAN_CONFIG_PHSEG2_PRG_ON &              // with CANInit
                   CAN_CONFIG_XTD_MSG &
                   CAN_CONFIG_DBL_BUFFER_ON &
                   CAN_CONFIG_VALID_XTD_MSG &
                   CAN_CONFIG_LINE_FILTER_OFF;


  CANInitialize(1,3,3,3,1,Can_Init_Flags);                  // initialize external CAN module




  CANSetOperationMode(CAN_MODE_CONFIG,0xff);               // set CONFIGURATION mode



  CANSetMask(CAN_MASK_B1,-1,CAN_CONFIG_XTD_MSG);          // set all mask1 bits to ones
  CANSetMask(CAN_MASK_B2,-1,CAN_CONFIG_XTD_MSG);          // set all mask2 bits to ones
  CANSetFilter(CAN_FILTER_B2_F3,ID_1st,CAN_CONFIG_XTD_MSG);// set id of filter B2_F3 to 1st node ID



  CANSetOperationMode(CAN_MODE_NORMAL,0);               // set NORMAL mode
  
  delay_ms(5000);
  CANWrite(ID_2nd, RxTx_Data, 1, Can_Send_Flags);


  while (1)
  {                                                              // endless loop
    Msg_Rcvd = CANRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
    if ((Rx_ID == ID_1st) && Msg_Rcvd)
    {                                                                     // if message received check id

      PORTC = RxTx_Data[0];
      RxTx_Data[0]++ ;                                                     // increment received data
      CANWrite(ID_2nd, RxTx_Data, 2, Can_Send_Flags);                      // send incremented data back
    }
    delay_ms(1000);
  }
}
node 2:

Code: Select all

/*
* Project_name:
     Can 2st node
* Copyright:
     (c) Mikroelektronika, 2010.
* Revision History
     20101116:
    - Initial release;
    - 20101116(RR);
* Description:
     This is code that shows simple connection of two CAN modules. This is for 2st node.
         Modules can only work with MCUs that have CAN module integratet.
* Test configuration:
     MCU:                 ac:PIC18F458
                          http://ww1.microchip.com/downloads/en/DeviceDoc/41159e.pdf

     Dev.Board:           ac:EasyPIC6
                          http://www.mikroe.com/en/tools/easypic6/

     Oscillator:          External Clock 08.0000 MHz

     Ext. Modules:        ac:CAN_Board
                          http://www.mikroe.com/eng/products/view/128/can-1-board/

     SW:                  mikroC PRO for PIC v4.15
                          http://www.mikroe.com/en/compilers/mikroc/pro/pic/

* NOTES:
  - For regular functionality MCU must work on 8.0000 MHz oscillator.
  - Connect CAN Board on PortB and turn On Switches 2 and 6 on CAN Board
  - Turn On PortC LEDs (SW9.3)
*/




// End LCD module connections


unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags
unsigned char Rx_Data_Len;                                   // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const long ID_1st = 3u, ID_2nd = 12111;                       // node IDs
long Rx_ID;

void main() {
  PORTD = 0xff;
  TRISD = 0xff;
  PORTC = 0;                                                // clear PORTC
  TRISC = 0;                                                // set PORTC as output

  Can_Init_Flags = 0;                                       //
  Can_Send_Flags = 0;                                       // clear flags
  Can_Rcv_Flags  = 0;                                       //

  Can_Send_Flags = CAN_TX_PRIORITY_0 &                     // form value to be used
                   CAN_TX_XTD_FRAME &                      //     with CANWrite
                   CAN_TX_NO_RTR_FRAME;

  Can_Init_Flags = CAN_CONFIG_SAMPLE_THRICE &              // form value to be used
                   CAN_CONFIG_PHSEG2_PRG_ON &              // with CANInit
                   CAN_CONFIG_XTD_MSG &
                   CAN_CONFIG_DBL_BUFFER_ON &
                   CAN_CONFIG_VALID_XTD_MSG &
                   CAN_CONFIG_LINE_FILTER_OFF;


  CANInitialize(1,3,3,3,1,Can_Init_Flags);                  // initialize external CAN module




  CANSetOperationMode(CAN_MODE_CONFIG,0xff);               // set CONFIGURATION mode



  CANSetMask(CAN_MASK_B1,-1,CAN_CONFIG_XTD_MSG);          // set all mask1 bits to ones
  CANSetMask(CAN_MASK_B2,-1,CAN_CONFIG_XTD_MSG);          // set all mask2 bits to ones
  CANSetFilter(CAN_FILTER_B2_F3,ID_1st,CAN_CONFIG_XTD_MSG);// set id of filter B2_F3 to 1st node ID



  CANSetOperationMode(CAN_MODE_NORMAL,0);               // set NORMAL mode


  while (1)
  {                                                              // endless loop
    Msg_Rcvd = CANRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
    if ((Rx_ID == ID_1st) && Msg_Rcvd)
    {                                                                     // if message received check id

      PORTC = RxTx_Data[0];
      RxTx_Data[0]++ ;                                                     // increment received data
      CANWrite(ID_2nd, RxTx_Data, 2, Can_Send_Flags);                      // send incremented data back
    }
  }
}

PimI
Posts: 8
Joined: 30 Aug 2011 08:14

Re: CAN problem, beginner

#10 Post by PimI » 16 Sep 2011 08:40

Dear,

I'm trying to figure out how to set the baud rate, i've been playing with this and google'in but i don't really get it...
the function " CANInitialize(1,3,3,3,1,Can_Init_Flags); " initializes the baud rate doesn't it?
is there an easy way to figure out which values to put for i.e. let's say 125 kbps / 250 kbps?
using 8 Mhz crystal..

thanks!

cepera
Posts: 4
Joined: 09 Apr 2012 10:01

Re: CAN problem, beginner

#11 Post by cepera » 19 Apr 2012 14:33

Ok, I spend some time to understand how to setup CAN speed properly.
So I would like to share this info with community :)

I used this cool calculator: http://www.port.de/pages/misc/bittimings.php?lang=en

Please bear in mind that you should divide real oscillator speed by 2 (as described in calculator).

I made special wrapper for MikroC methods and made tables for speed setting:

Code: Select all

// Speed in Kbps
int baudrateList[CANBaudrateLast+1] = { 10, 20, 25, 50, 100, 125, 250, 500, 1000 };

#if OSCILLATOR_FREQ_MHZ == 8
struct CANBaudrateParams
CANBaudrateParameters[CANBaudrateLast+1] = {
    //BRP SJW PROPSEG PHSEG1 PHSEG2 Quality
    { 25, 1,  1,      8,     6,     CANQualityBest },  // 10Kbps 16Tq - best
    { 25, 1,  1,      3,     3,     CANQualityNormal },// 20Kbps 8Tq - normal
    { 10, 1,  1,      8,     6,     CANQualityBest },  // 25Kbps 16Tq - best
    { 5,  1,  1,      8,     6,     CANQualityBest },  // 50Kbps 16Tq - best
    { 5,  1,  1,      3,     3,     CANQualityNormal },// 100Kbps 8Tq - normal
    { 2,  1,  1,      8,     6,     CANQualityBest },  // 125Kbps 16Tq - best
    { 1,  1,  1,      8,     6,     CANQualityBest },  // 250Kbps 16Tq - best
    { 1,  1,  1,      3,     3,     CANQualityNormal },// 500Kbps 8Tq - normal
    { 1,  1,  1,      1,     2,     CANQualityBad },   // 1000Kbps 4Tq - bad, can be unworkable
};

#elif OSCILLATOR_FREQ_MHZ == 16

struct CANBaudrateParams
CANBaudrateParameters[CANBaudrateLast+1] = {
    //BRP SJW PROPSEG PHSEG1 PHSEG2 Quality
    { 50, 1,  1,      8,     6,     CANQualityBest },  // 10Kbps 16Tq - best
    { 25, 1,  1,      8,     6,     CANQualityBest },  // 20Kbps 16Tq - best
    { 20, 1,  1,      8,     6,     CANQualityBest },  // 25Kbps 16Tq - best
    { 10, 1,  1,      8,     6,     CANQualityBest },  // 50Kbps 16Tq - best
    { 5,  1,  1,      8,     6,     CANQualityBest },  // 100Kbps 16Tq - best
    { 4,  1,  1,      8,     6,     CANQualityBest },  // 125Kbps 16Tq - best
    { 2,  1,  1,      8,     6,     CANQualityBest },  // 250Kbps 16Tq - best
    { 1,  1,  1,      8,     6,     CANQualityBest },  // 500Kbps 16Tq - best
    { 1,  1,  1,      3,     3,     CANQualityNormal },// 1000Kbps 8Tq - normal
};

#elif OSCILLATOR_FREQ_MHZ == 32

struct CANBaudrateParams
CANBaudrateParameters[CANBaudrateLast+1] = {
    //BRP SJW PROPSEG PHSEG1 PHSEG2 Quality
    { 64, 1,  8,      8,     8,     CANQualityNormal },// 10Kbps 25Tq & 65% sample - normal
    { 50, 1,  1,      8,     6,     CANQualityBest },  // 20Kbps 16Tq - best
    { 40, 1,  1,      8,     6,     CANQualityBest },  // 25Kbps 16Tq - best
    { 20, 1,  1,      8,     6,     CANQualityBest },  // 50Kbps 16Tq - best
    { 10, 1,  1,      8,     6,     CANQualityBest },  // 100Kbps 16Tq - best
    { 8,  1,  1,      8,     6,     CANQualityBest },  // 125Kbps 16Tq - best
    { 4,  1,  1,      8,     6,     CANQualityBest },  // 250Kbps 16Tq - best
    { 2,  1,  1,      8,     6,     CANQualityBest },  // 500Kbps 16Tq - best
    { 1,  1,  1,      8,     6,     CANQualityBest },  // 1000Kbps 16Tq - best
};
#else//OSCILLATOR_FREQ_MHZ = 32
#error "Please use ocsillator from 8/16/32 Mhz set"
#endif// OSCILLATOR_FREQ_MHZcepera:router cepera$ 


But bear in mind that I use 62.5% instead of 87.5% as used in calculator.

Post Reply

Return to “mikroC General”