Выбрать главу

9.9.9 Calculating the Timing Parameters

Setting the nodes’ timing parameters is essential for the bus to operate reliably. Given the microcontroller clock frequency and the required CAN bus bit rate, we can calculate the values of the following timing parameters:

• Baud rate prescaler value 

• Prop_Seg value 

• Phase_Seg1 value 

• Phase_Seg2 value 

• SJW value

Correct timing requires that

• Prop_Seg = Phase_Seg1 ≥ Phase_Seg2 

• Phase_Seg2 ≥ SJW

The following example illustrates the calculation of these timing parameters.

Example 9.2

Assuming the microcontroller oscillator clock rate is 20MHz and the required CAN bit rate is 125KHz, calculate the timing parameters.

Solution 9.2

With a 20MHz clock rate, the clock period is 50ns. Choosing a baud rate prescaler value of 4, from Equation (9.4), TQ=2*(BRP+1)*TOSC, gives a time quantum of TQ=500ns. To obtain a nominal bit rate of 125KHz, the nominal bit time must be:

 TBIT = 1/0.125MHz = 8μs, or 16TQ

The Sync_Segment is 1TQ. Choosing 2TQ for the Prop_Seg, and 7TQ for Phase_Seg1 leaves 6TQ for Phase_Seg2 and places the sampling point at 10TQ at the end of Phase_Seg1.

By the rules described earlier, the SJW can be the maximum allowed (i.e., 4). However, a large SJW is only necessary when the clock generation of different nodes is not stable or accurate (e.g., if ceramic resonators are used). Typically, a SJW of 1 is enough. In summary, the required timing parameters are:

Baud rate prescaler (BRP) = 4

Sync_Seg                  = 1

Prop_Seg                  = 2

Phase_Seg1                = 7

Phase_Seg2                = 6

SJW                       = 1

The sampling point is at 10TQ which corresponds to 62.5% of the total bit time.

There are several tools available for free on the Internet for calculating CAN bus timing parameters. One such tool is the CAN Baud Rate Calculator, developed by Artic Consultants Ltd (http://www.articconsultants.co.uk). An example using this tool follows.

Example 9.3

Assuming the microcontroller oscillator clock rate is 20MHz and the required CAN bit rate is 125KHz, calculate the timing parameters using the CAN Baud Rate Calculator.

Solution 9.3

Figure 9.14 shows the output of the CAN Baud Rate Calculator program. The device type is selected as PIC18Fxxx8, the oscillator frequency is entered as 20MHz, and the CAN bus baud rate is entered as 125KHz.

Figure 9.14: Output of the CAN Baud Rate Calculator program

Clicking the Calculate Settings button calculates and displays the recommended timing parameters. In general, there is more than one solution, and different solutions are given in the Calculated Solutions field’s drop-down menu.

In choosing Solution 2 from the drop-down menu, the following timing parameters are recommended by the program:

Baud rate prescaler (BRP) = 4

Sync_Seg                  = 1

Prop_Seg                  = 5

Phase_Seg1                = 5

Phase_Seg2                = 5

SJW                       = 1

Sample point              = 68%

Error                     = 0%

9.10 mikroC CAN Functions

The mikroC language provides two libraries for CAN bus applications: the library for PIC microcontrollers with built-in CAN modules and the library based on using a SPI bus for PIC microcontrollers having no built-in CAN modules. In this section we will discuss only the library functions available for PIC microcontrollers with built-in CAN modules. Similar functions are available for the PIC microcontrollers with no built-in CAN modules.

The mikroC CAN functions are supported only by PIC18XXX8 microcontrollers with MCP2551 or similar CAN transceivers. Both standard (11 identifier bits) and extended format (29 identifier bits) messages are supported.

The following mikroC functions are provided:

• CANSetOperationMode

• CANGetOperationMode

• CANInitialize

• CANSetBaudRate

• CANSetMask

• CANSetFilter

• CANRead

• CANWrite

9.10.1 CANSetOperationMode

The CANSetOperationMode function sets the CAN operation mode. The function prototype is:

void CANSetOperationMode(char mode, char wait_flag)

The parameter  wait_flag is either 0 or 0xFF. If it is set to 0xFF, the function blocks and will not return until the requested mode is set. If it is set to 0, the function returns as a nonblocking call.

The mode can be one of the following:

• CAN_MODE_NORMAL Normal mode of operation

• CAN_MODE_SLEEP Sleep mode of operation

• CAN_MODE_LOOP Loop-back mode of operation

• CAN_MODE_LISTEN Listen-only mode of operation

• CAN_MODE_CONFIG Configuration mode of operation

9.10.2 CANGetOperationMode

The CANGetOperationMode function returns the current CAN operation mode. The function prototype is:

char CANGetOperationMode(void)

9.10.3 CANInitialize 

The CANInitialize function initializes the CAN module. All mask registers are cleared to 0 to allow all messages. Upon execution of this function, the normal mode is set. The function prototype is:

void CANInitialize(char SJW, char BRP, char PHSEG1, char PHSEG2,

 char PROPSEG, char CAN_CONFIG_FLAGS)

where

 SJW  is the synchronization jump width

 BRP  is the baud rate prescaler

 PHSEG1  is the Phase_Seg1 timing parameter

 PHSEG2  is the Phase_Seg2 timing parameter

 PROPSEG  is the Prop_Seg

CAN_CONFIG_FLAGS can be one of the following configuration flags:

• CAN_CONFIG_DEFAULT  Default flags

• CAN_CONFIG_PHSEG2_PRG_ON  Use supplied PHSEG2 value

• CAN_CONFIG_PHSEG2_PRG_OFF  Use maximum of PHSEG1 or information processing time (IPT), whichever is greater

• CAN_CONFIG_LINE_FILTER_ON  Use CAN bus line filter for wake-up

• CAN_CONFIG_FILTER_OFF  Do not use CAN bus line filter

• CAN_CONFIG_SAMPLE_ONCE  Sample bus once at sample point

• CAN_CONFIG_SAMPLE_THRICE  Sample bus three times prior to sample point

• CAN_CONFIG_STD_MSG  Accept only standard identifier messages

• CAN_CONFIG_XTD_MSG  Accept only extended identifier messages

• CAN_CONFIG_DBL_BUFFER_ON  Use double buffering to receive data

• CAN_CONFIG_DBL_BUFFER_OFF  Do not use double buffering

• CAN_CONFIG_ALL_MSG  Accept all messages including invalid ones

• CAN_CONFIG_VALID_XTD_MSG  Accept only valid extended identifier messages