Skip to content

Instantly share code, notes, and snippets.

@matt448
Last active August 27, 2021 21:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt448/6925334 to your computer and use it in GitHub Desktop.
Save matt448/6925334 to your computer and use it in GitHub Desktop.
Code for example CAN bus receiver.
// demo: CAN-BUS Shield, receive data
#include "mcp_can.h"
#include <SPI.h>
#include <LiquidCrystal.h>
#include <stdio.h>
#define INT8U unsigned char
INT8U Flag_Recv = 0;
INT8U len = 0;
INT8U buf[8];
INT32U canId = 0x000;
char str[20];
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
void setup()
{
lcd.begin(20, 4);
CAN.begin(CAN_100KBPS); // init can bus : baudrate = 100k
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
Serial.begin(115200);
}
void MCP2515_ISR()
{
Flag_Recv = 1;
}
void loop()
{
if(Flag_Recv) // check if data was recieved
{
Flag_Recv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
canId = CAN.getCanId();
//Print data to the serial console
//and the LCD display
Serial.println("CAN_BUS GET DATA!");
Serial.print("CAN ID: ");
Serial.println(canId);
lcd.setCursor(0, 0);
lcd.print("CAN ID: ");
lcd.print(canId);
lcd.setCursor(0, 2);
Serial.print("data len = ");Serial.println(len);
//This loops through each byte of data and prints it
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);Serial.print("\t");
lcd.print(buf[i]);
lcd.print(" ");
}
Serial.println();
delay(50);
}
}
@ronnie71
Copy link

ronnie71 commented Sep 9, 2017

but during compilation error occurred
it say that INT32U does not name a type

@mhavliczek
Copy link

6: error: 'INT32U' does not name a type

INT32U canId = 0x000;

Tengo el mismo error y no se que podra ser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment