123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // coding: utf-8
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include "can.h"
- // -----------------------------------------------------------------------------
- /** Set filters and masks.
- *
- * The filters are divided in two groups:
- *
- * Group 0: Filter 0 and 1 with corresponding mask 0.
- * Group 1: Filter 2, 3, 4 and 5 with corresponding mask 1.
- *
- * If a group mask is set to 0, the group will receive all messages.
- *
- * If you want to receive ONLY 11 bit identifiers, set your filters
- * and masks as follows:
- *
- * uint8_t can_filter[] PROGMEM = {
- * // Group 0
- * MCP2515_FILTER(0), // Filter 0
- * MCP2515_FILTER(0), // Filter 1
- *
- * // Group 1
- * MCP2515_FILTER(0), // Filter 2
- * MCP2515_FILTER(0), // Filter 3
- * MCP2515_FILTER(0), // Filter 4
- * MCP2515_FILTER(0), // Filter 5
- *
- * MCP2515_FILTER(0), // Mask 0 (for group 0)
- * MCP2515_FILTER(0), // Mask 1 (for group 1)
- * };
- *
- *
- * If you want to receive ONLY 29 bit identifiers, set your filters
- * and masks as follows:
- *
- * \code
- * uint8_t can_filter[] PROGMEM = {
- * // Group 0
- * MCP2515_FILTER_EXTENDED(0), // Filter 0
- * MCP2515_FILTER_EXTENDED(0), // Filter 1
- *
- * // Group 1
- * MCP2515_FILTER_EXTENDED(0), // Filter 2
- * MCP2515_FILTER_EXTENDED(0), // Filter 3
- * MCP2515_FILTER_EXTENDED(0), // Filter 4
- * MCP2515_FILTER_EXTENDED(0), // Filter 5
- *
- * MCP2515_FILTER_EXTENDED(0), // Mask 0 (for group 0)
- * MCP2515_FILTER_EXTENDED(0), // Mask 1 (for group 1)
- * };
- * \endcode
- *
- * If you want to receive both 11 and 29 bit identifiers, set your filters
- * and masks as follows:
- */
- const uint8_t can_filter[] PROGMEM =
- {
- // Group 0
- MCP2515_FILTER(0), // Filter 0
- MCP2515_FILTER(0), // Filter 1
-
- // Group 1
- MCP2515_FILTER_EXTENDED(0), // Filter 2
- MCP2515_FILTER_EXTENDED(0), // Filter 3
- MCP2515_FILTER_EXTENDED(0), // Filter 4
- MCP2515_FILTER_EXTENDED(0), // Filter 5
-
- MCP2515_FILTER(0), // Mask 0 (for group 0)
- MCP2515_FILTER_EXTENDED(0), // Mask 1 (for group 1)
- };
- // You can receive 11 bit identifiers with either group 0 or 1.
- // -----------------------------------------------------------------------------
- // Main loop for receiving and sending messages.
- int main(void)
- {
- // Initialize MCP2515
- can_init(BITRATE_125_KBPS);
-
- // Load filters and masks
- can_static_filter(can_filter);
-
- // Create a test messsage
- can_t msg;
-
- msg.id = 0x123456;
- msg.flags.rtr = 0;
- msg.flags.extended = 1;
-
- msg.length = 4;
- msg.data[0] = 0xde;
- msg.data[1] = 0xad;
- msg.data[2] = 0xbe;
- msg.data[3] = 0xef;
-
- // Send the message
- can_send_message(&msg);
-
- while (1)
- {
- // Check if a new messag was received
- if (can_check_message())
- {
- can_t msg;
-
- // Try to read the message
- if (can_get_message(&msg))
- {
- // If we received a message resend it with a different id
- msg.id += 10;
-
- // Send the new message
- can_send_message(&msg);
- }
- }
- }
-
- return 0;
- }
|