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)
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
• CAN_CONFIG_VALID_STD_MSG Accept only valid standard identifier messages
• CAN_CONFIG_ALL_VALID_MSG Accept all valid messages
These configuration values can be bitwise AND’ed to form complex configuration values.
The CANSetBaudRate function is used to set the CAN bus baud rate. The function prototype is:
void CANSetBaudRate(char SJW, char BRP, char PHSEG1, char PHSEG2,
char PROPSEG, char CAN_CONFIG_FLAGS)
The arguments of the function are as in function CANInitialize .
The CANSetMask function sets the mask for filtering messages. The function prototype is:
void CANSetMask(char CAN_MASK, long value,
char CAN_CONFIG_FLAGS)
CAN_MASK can be one of the following:
• CAN_MASK_B1 Receive buffer 1 mask value
• CAN_MASK_B2 Receive buffer 2 mask value
value is the mask register value. CAN_CONFIG_FLAGS can be either CAN_CONFIG_XTD (extended message), or CAN_CONFIG_STD (standard message).
The CANSetFilter function sets filter values. The function prototype is:
void CANSetFilter(char CAN_FILTER, long value,
char CAN_CONFIG_FLAGS)
• CAN_FILTER can be one of the following:
• CAN_FILTER_B1_F1 Filter 1 for buffer 1
• CAN_FILTER_B1_F2 Filter 2 for buffer 1
• CAN_FILTER_B2_F1 Filter 1 for buffer 2
• CAN_FILTER_B2_F2 Filter 2 for buffer 2
• CAN_FILTER_B2_F3 Filter 3 for buffer 2
• CAN_FILTER_B2_F4 Filter 4 for buffer 2
CAN_CONFIG_FLAGS can be either CAN_CONFIG_XTD (extended message) or CAN_CONFIG_STD (standard message).
The CANRead function is used to read messages from the CAN bus. If no message is available, 0 is returned. The function prototype is:
char CANRead(long *id, char *data, char *datalen,
char *CAN_RX_MSG_FLAGS)
id is the CAN message identifier. Only 11 or 29 bits may be used depending on message type (standard or extended). data is an array of bytes up to 8 where the received data is stored. datalen is the length of the received data (1 to 8).
CAN_RX_MSG_FLAGS can be one of the following:
• CAN_RX_FILTER_1 Receive buffer filter 1 accepted this message
• CAN_RX_FILTER_2 Receive buffer filter 2 accepted this message
• CAN_RX_FILTER_3 Receive buffer filter 3 accepted this message
• CAN_RX_FILTER_4 Receive buffer filter 4 accepted this message
• CAN_RX_FILTER_5 Receive buffer filter 5 accepted this message
• CAN_RX_FILTER_6 Receive buffer filter 6 accepted this message
• CAN_RX_OVERFLOW Receive buffer overflow occurred
• CAN_RX_INVALID_MSG Invalid message received
• CAN_RX_XTD_FRAME Extended identifier message received
• CAN_RX_RTR_FRAME RTR frame message received
• CAN_RX_DBL_BUFFERED This message was double buffered
These flags can be bitwise AND’ed if desired.
The CANWrite function is used to send a message to the CAN bus. A zero is returned if message can not be queued (buffer full). The function prototype is:
Читать дальше