当前位置:   article > 正文

arduino UNO R3/ESP8266连接MCP2515 CAN_esp8266 mcp2515 can

esp8266 mcp2515 can

arduino UNO R3/ESP8266连接MCP2515 CAN 

 

 CAN接收:

  1. // demo: CAN-BUS Shield, receive data with interrupt mode, and set mask and filter
  2. //
  3. // when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode
  4. // loovee, 2014-7-8
  5. #include <SPI.h>
  6. #define CAN_2515
  7. // #define CAN_2518FD
  8. // Set SPI CS Pin according to your hardware
  9. #if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
  10. // For Wio Terminal w/ MCP2518FD RPi Hat:
  11. // Channel 0 SPI_CS Pin: BCM 8
  12. // Channel 1 SPI_CS Pin: BCM 7
  13. // Interupt Pin: BCM25
  14. const int SPI_CS_PIN = BCM8;
  15. const int CAN_INT_PIN = BCM25;
  16. #else
  17. // For Arduino MCP2515 Hat:
  18. // the cs pin of the version after v1.1 is default to D9
  19. // v0.9b and v1.0 is default D10
  20. const int SPI_CS_PIN = 9;
  21. const int CAN_INT_PIN = 2;//INT表示中断的pin
  22. #endif
  23. #ifdef CAN_2518FD
  24. #include "mcp2518fd_can.h"
  25. mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
  26. #endif
  27. #ifdef CAN_2515
  28. #include "mcp2515_can.h"
  29. mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
  30. #endif
  31. unsigned char flagRecv = 0;
  32. unsigned char len = 0;
  33. unsigned char buf[8];
  34. char str[20];
  35. void setup() {
  36. SERIAL_PORT_MONITOR.begin(115200);
  37. while(!Serial){};
  38. attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt(attachInterrupt()函数是用于为Arduino开发板设置和执行ISR(中断服务程序)用的
  39. //ISR(中断服务程序)顾名思义就是中断Arduino当前正在处理的事情而优先去执行中断服务程序。当中断服务程序完成以后,再回来继续执行刚才执行的事情。中断服务程序对监测Arduino输入有很大的用处。)
  40. while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
  41. SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
  42. delay(100);
  43. }
  44. SERIAL_PORT_MONITOR.println("CAN init ok!");
  45. /*
  46. set mask, set both the mask to 0x3ff
  47. */
  48. CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
  49. CAN.init_Mask(1, 0, 0x3ff);
  50. /*
  51. set filter, we can receive id from 0x04 ~ 0x09
  52. */
  53. CAN.init_Filt(0, 0, 0x04); // there are 6 filter in mcp2515
  54. CAN.init_Filt(1, 0, 0x05); // there are 6 filter in mcp2515
  55. CAN.init_Filt(2, 0, 0x06); // there are 6 filter in mcp2515
  56. CAN.init_Filt(3, 0, 0x07); // there are 6 filter in mcp2515
  57. CAN.init_Filt(4, 0, 0x08); // there are 6 filter in mcp2515
  58. CAN.init_Filt(5, 0, 0x09); // there are 6 filter in mcp2515
  59. }
  60. void MCP2515_ISR() {
  61. flagRecv = 1;
  62. SERIAL_PORT_MONITOR.println("flagRecv is trigger!!!");
  63. }
  64. void loop() {
  65. if (flagRecv) { // check if get data
  66. flagRecv = 0; // clear flag
  67. CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
  68. SERIAL_PORT_MONITOR.println("\r\n------------------------------------------------------------------");
  69. SERIAL_PORT_MONITOR.print("Get Data From id: ");
  70. SERIAL_PORT_MONITOR.println(CAN.getCanId());
  71. for (int i = 0; i < len; i++) { // print the data
  72. SERIAL_PORT_MONITOR.print("0x");
  73. SERIAL_PORT_MONITOR.print(buf[i], HEX);
  74. SERIAL_PORT_MONITOR.print("\t");
  75. }
  76. SERIAL_PORT_MONITOR.println();
  77. }
  78. }
  79. /*********************************************************************************************************
  80. END FILE
  81. *********************************************************************************************************/

CAN发送:

  1. // demo: set_mask_filter_send
  2. // this demo will show you how to use mask and filter
  3. #include <SPI.h>
  4. #define CAN_2515
  5. // #define CAN_2518FD
  6. // Set SPI CS Pin according to your hardware
  7. #if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
  8. // For Wio Terminal w/ MCP2518FD RPi Hat:
  9. // Channel 0 SPI_CS Pin: BCM 8
  10. // Channel 1 SPI_CS Pin: BCM 7
  11. // Interupt Pin: BCM25
  12. const int SPI_CS_PIN = BCM8;
  13. const int CAN_INT_PIN = BCM25;
  14. #else
  15. // For Arduino MCP2515 Hat:
  16. // the cs pin of the version after v1.1 is default to D9
  17. // v0.9b and v1.0 is default D10
  18. //const int SPI_CS_PIN = 9;//arduino UNO R3
  19. //const int CAN_INT_PIN = 2;//arduino UNO R3
  20. //
  21. const int SPI_CS_PIN = D8;//ESP8266
  22. const int CAN_INT_PIN = D2;//ESP8266
  23. #endif
  24. #ifdef CAN_2518FD
  25. #include "mcp2518fd_can.h"
  26. mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
  27. #endif
  28. #ifdef CAN_2515
  29. #include "mcp2515_can.h"
  30. mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
  31. #endif
  32. void setup() {
  33. SERIAL_PORT_MONITOR.begin(115200);
  34. while(!Serial){};
  35. while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
  36. SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
  37. delay(100);
  38. }
  39. SERIAL_PORT_MONITOR.println("CAN init ok!");
  40. }
  41. unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  42. void loop() {
  43. for (int id = 0; id < 10; id++)
  44. {
  45. // memset(stmp, id, sizeof(stmp)); // set id to send data buff
  46. CAN.sendMsgBuf(id, 0, sizeof(stmp), stmp);
  47. delay(1000);
  48. }
  49. delay(1000);
  50. }
  51. /*********************************************************************************************************
  52. END FILE
  53. *********************************************************************************************************/

头文件下载:

https://download.csdn.net/download/txwtech/87954870icon-default.png?t=N5K3https://download.csdn.net/download/txwtech/87954870

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/610319
推荐阅读
相关标签
  

闽ICP备14008679号