PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 satır
1.6KB

  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. class ExampleClass : public CANListener
  11. {
  12. public:
  13. void printFrame(CAN_message_t &frame, int mailbox);
  14. bool frameHandler(CAN_message_t &frame, int mailbox, uint8_t controller); //overrides the parent version so we can actually do something
  15. };
  16. void ExampleClass::printFrame(CAN_message_t &frame, int mailbox)
  17. {
  18. Serial.print("ID: ");
  19. Serial.print(frame.id, HEX);
  20. Serial.print(" Data: ");
  21. for (int c = 0; c < frame.len; c++)
  22. {
  23. Serial.print(frame.buf[c], HEX);
  24. Serial.write(' ');
  25. }
  26. Serial.write('\r');
  27. Serial.write('\n');
  28. }
  29. bool ExampleClass::frameHandler(CAN_message_t &frame, int mailbox, uint8_t controller)
  30. {
  31. printFrame(frame, mailbox);
  32. return true;
  33. }
  34. ExampleClass exampleClass;
  35. // -------------------------------------------------------------
  36. void setup(void)
  37. {
  38. delay(1000);
  39. Serial.println(F("Hello Teensy Single CAN Receiving Example With Objects."));
  40. Can0.begin(500000);
  41. //if using enable pins on a transceiver they need to be set on
  42. pinMode(2, OUTPUT);
  43. digitalWrite(2, HIGH);
  44. Can0.attachObj(&exampleClass);
  45. exampleClass.attachGeneralHandler();
  46. }
  47. // -------------------------------------------------------------
  48. void loop(void)
  49. {
  50. delay(1000);
  51. Serial.write('.');
  52. }