User manual

282
mikoC PRO for dsPIC
MikroElektronika
CANxWrite
Prototype
unsigned int CANxWrite(long id, char *data_, unsigned int DataLen, unsigned
int CAN_TX_MSG_FLAGS);
Description If at least one empty Transmit Buffer is found, the function sends message in the queue for
transmission.
Parameters - id: CAN message identier. Valid values: 11 or 29 bit values, depending on message type (standard
or extended)
- data: data to be sent
- dataLen: data length. Valid values: 0..8
- CAN_RX_MSG_FLAGS: message ags. Valid values: CAN_TX_MSG_FLAGS constants. See CAN_TX_
MSG_FLAGS constants.
Returns - 0 if all Transmit Buffers are busy
- 0xFFFF if at least one Transmit Buffer is available
Requires MCU with the CAN module.
MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN
bus.
CAN must be in Cong mode, otherwise the function will be ignored. See CANxSetOperationMode.
Example
// send message extended CAN message with appropriate ID and data
unsigned int tx_ags;
char data[8];
unsigned long msg_id;
...
CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL
mode (CAN1 must be in mode in which transmission is possible)
tx_ags = _CAN_TX_PRIORITY_0 &
_CAN_TX_XTD_FRAME &
_CAN_TX_NO_RTR_FRAME; // set message ags
CAN1Write(msg_id, data, 1, tx_ags);
Notes - CAN library routine require you to specify the module you want to use. To use the desired CAN
module, simply change the letter x in the routine prototype for a number from 1 to 2.
- Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet
before utilizing this library.