User`s guide

APPENDIX C: Pseudo-Programming Example
/*
* Note that these examples do not include everything necessary for fully
* working CAN. It is necessary to set up the crossbar and system clock
* correctly in order for the CAN module to work properly.
* If terminal printing is desired UART0 with a timer for BAUD rate is needed.
*/
/* Example CAN Transmission Code */
/* This will send a 3-byte message (0x12 0x34 0x56) to 10-bit address 0x123 */
#include "can.h"
/* ... */
unsigned char data[3] = {0x12, 0x34, 0x56};
CAN_BUFFER canbuf;
can_init( );
/* ... */
while (1) {
/* ... */
if (want_to_transmit_something) {
canbuf = can_get_tx_buf( );
can_set_address_std(canbuf, 0x123); //CAN message ID = 123
can_set_buffer_data(canbuf, data, sizeof(data));
can_send_tx_buf(canbuf);
}
/* ... */
}
/* Example CAN Reception Code */
/* This will receive a message and print its contents to the terminal */
/* Might also be useful for testing purposes */
#include "can.h"
/* ... */
unsigned char data[8];
CAN_BUFFER canbuf;
can_init( );
/* ... */
while (1) {
/* ... */
canbuf = can_get_rx_msg( );
// check if anything was actually received
if (canbuf) {
printf("Length: %d\n", can_get_dlc(canbuf));
printf("Message ID: %04x\n", can_get_address(canbuf));
printf("Data: ");
for (i = 0; i < can_get_dlc(canbuf); ++i) {
printf("%02x ", can_get_data_byte(canbuf, i));
}
printf("\nEnd of Message\n");
}
}