main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // coding: utf-8
  2. #include <avr/io.h>
  3. #include <avr/pgmspace.h>
  4. #include "can.h"
  5. // -----------------------------------------------------------------------------
  6. /** Set filters and masks.
  7. *
  8. * The filters are divided in two groups:
  9. *
  10. * Group 0: Filter 0 and 1 with corresponding mask 0.
  11. * Group 1: Filter 2, 3, 4 and 5 with corresponding mask 1.
  12. *
  13. * If a group mask is set to 0, the group will receive all messages.
  14. *
  15. * If you want to receive ONLY 11 bit identifiers, set your filters
  16. * and masks as follows:
  17. *
  18. * uint8_t can_filter[] PROGMEM = {
  19. * // Group 0
  20. * MCP2515_FILTER(0), // Filter 0
  21. * MCP2515_FILTER(0), // Filter 1
  22. *
  23. * // Group 1
  24. * MCP2515_FILTER(0), // Filter 2
  25. * MCP2515_FILTER(0), // Filter 3
  26. * MCP2515_FILTER(0), // Filter 4
  27. * MCP2515_FILTER(0), // Filter 5
  28. *
  29. * MCP2515_FILTER(0), // Mask 0 (for group 0)
  30. * MCP2515_FILTER(0), // Mask 1 (for group 1)
  31. * };
  32. *
  33. *
  34. * If you want to receive ONLY 29 bit identifiers, set your filters
  35. * and masks as follows:
  36. *
  37. * \code
  38. * uint8_t can_filter[] PROGMEM = {
  39. * // Group 0
  40. * MCP2515_FILTER_EXTENDED(0), // Filter 0
  41. * MCP2515_FILTER_EXTENDED(0), // Filter 1
  42. *
  43. * // Group 1
  44. * MCP2515_FILTER_EXTENDED(0), // Filter 2
  45. * MCP2515_FILTER_EXTENDED(0), // Filter 3
  46. * MCP2515_FILTER_EXTENDED(0), // Filter 4
  47. * MCP2515_FILTER_EXTENDED(0), // Filter 5
  48. *
  49. * MCP2515_FILTER_EXTENDED(0), // Mask 0 (for group 0)
  50. * MCP2515_FILTER_EXTENDED(0), // Mask 1 (for group 1)
  51. * };
  52. * \endcode
  53. *
  54. * If you want to receive both 11 and 29 bit identifiers, set your filters
  55. * and masks as follows:
  56. */
  57. const uint8_t can_filter[] PROGMEM =
  58. {
  59. // Group 0
  60. MCP2515_FILTER(0), // Filter 0
  61. MCP2515_FILTER(0), // Filter 1
  62. // Group 1
  63. MCP2515_FILTER_EXTENDED(0), // Filter 2
  64. MCP2515_FILTER_EXTENDED(0), // Filter 3
  65. MCP2515_FILTER_EXTENDED(0), // Filter 4
  66. MCP2515_FILTER_EXTENDED(0), // Filter 5
  67. MCP2515_FILTER(0), // Mask 0 (for group 0)
  68. MCP2515_FILTER_EXTENDED(0), // Mask 1 (for group 1)
  69. };
  70. // You can receive 11 bit identifiers with either group 0 or 1.
  71. // -----------------------------------------------------------------------------
  72. // Main loop for receiving and sending messages.
  73. int main(void)
  74. {
  75. // Initialize MCP2515
  76. can_init(BITRATE_125_KBPS);
  77. // Load filters and masks
  78. can_static_filter(can_filter);
  79. // Create a test messsage
  80. can_t msg;
  81. msg.id = 0x123456;
  82. msg.flags.rtr = 0;
  83. msg.flags.extended = 1;
  84. msg.length = 4;
  85. msg.data[0] = 0xde;
  86. msg.data[1] = 0xad;
  87. msg.data[2] = 0xbe;
  88. msg.data[3] = 0xef;
  89. // Send the message
  90. can_send_message(&msg);
  91. while (1)
  92. {
  93. // Check if a new messag was received
  94. if (can_check_message())
  95. {
  96. can_t msg;
  97. // Try to read the message
  98. if (can_get_message(&msg))
  99. {
  100. // If we received a message resend it with a different id
  101. msg.id += 10;
  102. // Send the new message
  103. can_send_message(&msg);
  104. }
  105. }
  106. }
  107. return 0;
  108. }