PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

97 lines
2.2KB

  1. /*
  2. * Object Oriented CAN example for Teensy 3.6 with Dual CAN buses
  3. * By Collin Kidder. Based upon the work of Pawelsky and Teachop
  4. *
  5. * Both buses are set to 500k to show things with a faster bus.
  6. * The reception of frames in this example is done via callbacks
  7. * to an object rather than polling. Frames are delivered as they come in.
  8. */
  9. #include <FlexCAN.h>
  10. #ifndef __MK66FX1M0__
  11. #error "Teensy 3.6 with dual CAN bus is required to run this example"
  12. #endif
  13. static CAN_message_t msg;
  14. static uint8_t hex[17] = "0123456789abcdef";
  15. class ExampleClass : public CANListener
  16. {
  17. public:
  18. void printFrame(CAN_message_t &frame, int mailbox);
  19. bool frameHandler(CAN_message_t &frame, int mailbox, uint8_t controller); //overrides the parent version so we can actually do something
  20. };
  21. void ExampleClass::printFrame(CAN_message_t &frame, int mailbox)
  22. {
  23. Serial.print("ID: ");
  24. Serial.print(frame.id, HEX);
  25. Serial.print(" Data: ");
  26. for (int c = 0; c < frame.len; c++)
  27. {
  28. Serial.print(frame.buf[c], HEX);
  29. Serial.write(' ');
  30. }
  31. Serial.write('\r');
  32. Serial.write('\n');
  33. }
  34. bool ExampleClass::frameHandler(CAN_message_t &frame, int mailbox, uint8_t controller)
  35. {
  36. printFrame(frame, mailbox);
  37. return true;
  38. }
  39. ExampleClass exampleClass;
  40. // -------------------------------------------------------------
  41. void setup(void)
  42. {
  43. delay(1000);
  44. Serial.println(F("Hello Teensy 3.6 dual CAN Test With Objects."));
  45. Can0.begin(500000);
  46. Can1.begin(500000);
  47. //if using enable pins on a transceiver they need to be set on
  48. pinMode(2, OUTPUT);
  49. pinMode(35, OUTPUT);
  50. digitalWrite(2, HIGH);
  51. digitalWrite(35, HIGH);
  52. Can0.attachObj(&exampleClass);
  53. exampleClass.attachGeneralHandler();
  54. msg.ext = 0;
  55. msg.id = 0x100;
  56. msg.len = 8;
  57. msg.buf[0] = 10;
  58. msg.buf[1] = 20;
  59. msg.buf[2] = 0;
  60. msg.buf[3] = 100;
  61. msg.buf[4] = 128;
  62. msg.buf[5] = 64;
  63. msg.buf[6] = 32;
  64. msg.buf[7] = 16;
  65. }
  66. // -------------------------------------------------------------
  67. void loop(void)
  68. {
  69. msg.buf[0]++;
  70. Can1.write(msg);
  71. msg.buf[0]++;
  72. Can1.write(msg);
  73. msg.buf[0]++;
  74. Can1.write(msg);
  75. msg.buf[0]++;
  76. Can1.write(msg);
  77. msg.buf[0]++;
  78. Can1.write(msg);
  79. delay(20);
  80. }