arduino UNO R3/ESP8266连接MCP2515 CAN

CAN接收:

// demo: CAN-BUS Shield, receive data with interrupt mode, and set mask and filter
//
// when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode
// loovee, 2014-7-8#include <SPI.h>#define CAN_2515
// #define CAN_2518FD// Set SPI CS Pin according to your hardware#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN  = BCM8;
const int CAN_INT_PIN = BCM25;
#else// For Arduino MCP2515 Hat:
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;//INT表示中断的pin
#endif#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif                              unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];void setup() {SERIAL_PORT_MONITOR.begin(115200);while(!Serial){};attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt(attachInterrupt()函数是用于为Arduino开发板设置和执行ISR(中断服务程序)用的//ISR(中断服务程序)顾名思义就是中断Arduino当前正在处理的事情而优先去执行中断服务程序。当中断服务程序完成以后,再回来继续执行刚才执行的事情。中断服务程序对监测Arduino输入有很大的用处。)while (CAN_OK != CAN.begin(CAN_500KBPS)) {             // init can bus : baudrate = 500kSERIAL_PORT_MONITOR.println("CAN init fail, retry...");delay(100);}SERIAL_PORT_MONITOR.println("CAN init ok!");/*set mask, set both the mask to 0x3ff*/CAN.init_Mask(0, 0, 0x3ff);                         // there are 2 mask in mcp2515, you need to set both of themCAN.init_Mask(1, 0, 0x3ff);/*set filter, we can receive id from 0x04 ~ 0x09*/CAN.init_Filt(0, 0, 0x04);                          // there are 6 filter in mcp2515CAN.init_Filt(1, 0, 0x05);                          // there are 6 filter in mcp2515CAN.init_Filt(2, 0, 0x06);                          // there are 6 filter in mcp2515CAN.init_Filt(3, 0, 0x07);                          // there are 6 filter in mcp2515CAN.init_Filt(4, 0, 0x08);                          // there are 6 filter in mcp2515CAN.init_Filt(5, 0, 0x09);                          // there are 6 filter in mcp2515
}void MCP2515_ISR() {flagRecv = 1;SERIAL_PORT_MONITOR.println("flagRecv is trigger!!!");
}void loop() {if (flagRecv) {                // check if get dataflagRecv = 0;                // clear flagCAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data bufSERIAL_PORT_MONITOR.println("\r\n------------------------------------------------------------------");SERIAL_PORT_MONITOR.print("Get Data From id: ");SERIAL_PORT_MONITOR.println(CAN.getCanId());for (int i = 0; i < len; i++) { // print the dataSERIAL_PORT_MONITOR.print("0x");SERIAL_PORT_MONITOR.print(buf[i], HEX);SERIAL_PORT_MONITOR.print("\t");}SERIAL_PORT_MONITOR.println();}
}/*********************************************************************************************************END FILE
*********************************************************************************************************/

CAN发送:

// demo: set_mask_filter_send
// this demo will show you how to use mask and filter
#include <SPI.h>#define CAN_2515
// #define CAN_2518FD// Set SPI CS Pin according to your hardware#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN  = BCM8;
const int CAN_INT_PIN = BCM25;
#else// For Arduino MCP2515 Hat:
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
//const int SPI_CS_PIN = 9;//arduino UNO R3
//const int CAN_INT_PIN = 2;//arduino UNO R3
//
const int SPI_CS_PIN = D8;//ESP8266
const int CAN_INT_PIN = D2;//ESP8266
#endif#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif  void setup() {SERIAL_PORT_MONITOR.begin(115200);while(!Serial){};while (CAN_OK != CAN.begin(CAN_500KBPS)) {             // init can bus : baudrate = 500kSERIAL_PORT_MONITOR.println("CAN init fail, retry...");delay(100);}SERIAL_PORT_MONITOR.println("CAN init ok!");
}unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};void loop() {for (int id = 0; id < 10; id++) {// memset(stmp, id, sizeof(stmp));                 // set id to send data buffCAN.sendMsgBuf(id, 0, sizeof(stmp), stmp);delay(1000);}delay(1000);}/*********************************************************************************************************END FILE
*********************************************************************************************************/

头文件在群文件289186279

arduino UNO R3/ESP8266连接MCP2515 CAN相关推荐

  1. 基于arduino UNO R3的远程可控的红外热释传感器 (转)

    首先实物图奉上,比较简陋别见怪啦哈哈 <ignore_js_op> ●实验名称:基于机智云平台的红外热释传感器的应用 ●实验目的:1. 能够在云平台下,手机实时观测传感器返回的AD值:   ...

  2. Arduino UNO与ESP8266串口连接

    Arduino UNO与ESP8266串口连接 1. ESP8266烧录AT固件 1.1 烧录软件下载 1.2 烧录AT固件 2. ESP8266 AT指令 3. Arduino UNO 串口连接方式 ...

  3. arduino的esp32程序无法上传_原装正版arduino uno R3无法上传程序

    RT,本人在网络上买的原装意大利产正版Arduino uno R3(售价159RMB,坏了很可惜,实在找不到问题所在.)无法上传程序. 已排除问题如下:1.驱动已正确安装 端口.PNG (2.12 K ...

  4. Mixly第三方用户库开发Arduino UNO使用EMW3080连接阿里云

    文章目录 前言 一.实验准备 1.硬件准备 2.软件准备 二.实验过程 1.Mixly库说明 2.文件编写 2.1.编写xxx.xml文件 2.2.编写block/xxx.js文件 2.3.编写zh- ...

  5. RFID模块+WIFI模块+振动传感器+有源蜂鸣器+舵机+Arduino UNO R3所构成的门禁系统模块

    该系统模块主要由RFID模块+WIFI模块+振动传感器+有源蜂鸣器+舵机+Arduino UNO R3组成的门禁系统模块.这里使用舵机充当门锁,用户可以刷卡开门,也可以通过APP控制舵机状态达到开门的 ...

  6. 新手必读:Arduino UNO R3教程,原理图,引脚图,详细介绍

    刚入门的学习Arduino的朋友都会有个疑问Arduino UNO R3是什么?为什么要从Arduino UNO R3开始学起? Arduino概述: Arduino是一个开放源码电子原型平台,拥有灵 ...

  7. Arduino UNO R3 (CH340G)基础篇-引脚

    目录 Arduino Uno R3 (CH340G)引脚分配图 Arduino Uno 板载指示灯 Arduino Uno引脚分配 - 电源 Arduino Uno引脚分配 - 数字引脚 数字电平 P ...

  8. Arduino UNO R3开发板+MQ-2烟雾浓度传感器+火焰传感器+舵机+无源蜂鸣器+风扇+步进电机+WIFI模块+RGB三色LED灯+SIM900A所构成的室内安全报警模块

    该系统模块主要由Arduino UNO R3开发板+MQ-2烟雾浓度传感器+火焰传感器+舵机+无源蜂鸣器+风扇+步进电机+WIFI模块+RGB三色LED灯+SIM900A所组成,MQ-2烟雾浓度传感器 ...

  9. 单片机学习笔记(Arduino Uno r3)

    单片机学习笔记(Arduino  Uno r3) Aduino是什么? 分为硬件和软件两个部分,硬件和软件共同组成了Arduino,使用时须将Arduino硬件和软件配合使用 硬件:Arduino系列 ...

最新文章

  1. 关于 Git 提交这些规范,你都遵守了吗?
  2. 增强包_机电工程学院开展“情暖冬日,爱在机电”冬至包饺子活动
  3. Spring Cloud笔记
  4. LOL手游诺手对线技巧,上分率提高60%,战神玩家推荐玩法
  5. python里元组和列表的共同点和不同点_Python_列表,元组和字典的异同
  6. 【Python爬虫】requests与urllib库的区别
  7. 基于生命周期理论的农业科学数据中心化管理模式
  8. static在php中,php中static关键字在类中的使用
  9. @Autowired 与@Resource的区别
  10. 远程桌面连接,运维工程师-必备软件【MultiDesk】
  11. 肠道微生物组如何影响运动能力,所谓的“精英肠道微生物组”真的存在吗?
  12. 金玉良缘易配而木石前盟难得|M1 Mac os(Apple Silicon)天生一对Python3开发环境搭建(集成深度学习框架Tensorflow/Pytorch)
  13. 人民币大写的正确写法(开票据事项)
  14. 如何用电脑下载微信视频号中的视频?
  15. 高速接口----使用sfp完成以太网传输
  16. 昨天看《天使爱美丽》
  17. APP注册登录那点事
  18. 面试技巧--国企银行篇
  19. 从脚本学python(秋名山车神)
  20. 2022 年超过 27 个最流行的计算机视觉应用程序和用例

热门文章

  1. 产品需求报告文档模板
  2. 织梦滚石v3.0导航网站源码+测试版
  3. CRM软件哪个好?国内外6大顶级CRM软件盘点
  4. android桌面文件夹,打造清新手机桌面 5款安卓桌面文件夹合辑推荐
  5. java赵云主角兵器谱游戏_见过最牛的三国杀兵器谱专题论述
  6. wifi共享精灵轻松实现Dote局域网对战
  7. Python计算机二级考试题目试题 仿真试卷测试 python二级真题试卷,整卷
  8. 给丝袜哥(Swagger)升级了新版本,没想到居然有这么多坑!
  9. Hbuilder的error
  10. 苹果手机html5 播放器,酷狗全新iPhone版HTML5播放器发布