Instructions

240Libraries
© 2013 Conrad Electronic
5.5.1 CAN Examples
In this chapter some initialization examples are given to clarify the operation of the CAN Library.
Initialization
In any event, the CAN library must be initialized before use. This example is for the CAN bus at a
speed of 1 mega bps, and for a FIFO RAM with 10 entries.
byte fifo_buf[140];
CAN_Init(CAN_1MBPS, 10, fifo_buf);
Reception
1. On MOb 2 messages of type CAN 2.0A are received, that have exactly an identifier of 0x123.
CAN_SetMOb(2, 0x123, 0x7ff, CAN_RECV);
2. On MOb 3 messages of type CAN 2.0B are received, that have exactly an identifier of 0x12345.
CAN_SetMOb(3, 0x12345, 0x1fffffff, CAN_RECV|CAN_EXTID);
3. On MOb 3 messages of type CAN2.0A and CAN 2.0B are received, because the
CAN_IGN_EXTID flag is set. Because the maskID is null messages with all identifiers are received.
Since CAN_IGN_RTR is set, normal and trigger packets are accepted.
CAN_SetMOb(3, 0x12345, 0, CAN_RECV|CAN_IGN_EXTID|CAN_IGN_RTR);
4. On MOb 2 messages of type CAN 2.0A are received, that have an identifier of 0x120, 0x121,
0x122 or 0x123.
CAN_SetMOb(2, 0x120, 0x7fc, CAN_RECV);
Send
1. On MOb 0 is sent a CAN 2.0A message with ID 0x432 and 6 data byte.
byte data[8], i;
for(i=0;i<8;i++) data[i]=i;
CAN_SetMOb(0, 0x432, 0, CAN_SEND);
CAN_MObSend(0, 6, data);
2. On MOb 1 a CAN 2.0B message will be sent with ID 0x12345678 and 8 data.