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

3 роки тому
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // -------------------------------------------------------------
  2. // CANtest for Teensy 3.6 dual CAN bus
  3. // by Collin Kidder, Based on CANTest by Pawelsky (based on CANtest by teachop)
  4. //
  5. // Both buses are left at default 250k speed and the second bus sends frames to the first
  6. // to do this properly you should have the two buses linked together. This sketch
  7. // also assumes that you need to set enable pins active. Comment out if not using
  8. // enable pins or set them to your correct pins.
  9. //
  10. // This sketch tests both buses as well as interrupt driven Rx and Tx. There are only
  11. // two Tx buffers by default so sending 5 at a time forces the interrupt driven system
  12. // to buffer the final three and send them via interrupts. All the while all Rx frames
  13. // are internally saved to a software buffer by the interrupt handler.
  14. //
  15. #include <FlexCAN.h>
  16. #ifndef __MK66FX1M0__
  17. #error "Teensy 3.6 with dual CAN bus is required to run this example"
  18. #endif
  19. static CAN_message_t msg;
  20. static uint8_t hex[17] = "0123456789abcdef";
  21. // -------------------------------------------------------------
  22. static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
  23. {
  24. uint8_t working;
  25. while( dumpLen-- ) {
  26. working = *bytePtr++;
  27. Serial.write( hex[ working>>4 ] );
  28. Serial.write( hex[ working&15 ] );
  29. }
  30. Serial.write('\r');
  31. Serial.write('\n');
  32. }
  33. // -------------------------------------------------------------
  34. void setup(void)
  35. {
  36. delay(1000);
  37. Serial.println(F("Hello Teensy 3.6 dual CAN Test."));
  38. Can0.begin();
  39. Can1.begin();
  40. //if using enable pins on a transceiver they need to be set on
  41. pinMode(2, OUTPUT);
  42. pinMode(35, OUTPUT);
  43. digitalWrite(2, HIGH);
  44. digitalWrite(35, HIGH);
  45. msg.ext = 0;
  46. msg.id = 0x100;
  47. msg.len = 8;
  48. msg.buf[0] = 10;
  49. msg.buf[1] = 20;
  50. msg.buf[2] = 0;
  51. msg.buf[3] = 100;
  52. msg.buf[4] = 128;
  53. msg.buf[5] = 64;
  54. msg.buf[6] = 32;
  55. msg.buf[7] = 16;
  56. }
  57. // -------------------------------------------------------------
  58. void loop(void)
  59. {
  60. CAN_message_t inMsg;
  61. while (Can0.available())
  62. {
  63. Can0.read(inMsg);
  64. Serial.print("CAN bus 0: "); hexDump(8, inMsg.buf);
  65. }
  66. msg.buf[0]++;
  67. Can1.write(msg);
  68. msg.buf[0]++;
  69. Can1.write(msg);
  70. msg.buf[0]++;
  71. Can1.write(msg);
  72. msg.buf[0]++;
  73. Can1.write(msg);
  74. msg.buf[0]++;
  75. Can1.write(msg);
  76. delay(20);
  77. }