#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
#include <LiquidCrystal.h> //Library for using LCD display
const int rs = 3, en = 4, d4 = 5, d5 = 6, d6 = 7, d7 = 8;
struct can_frame canMsg1;
struct can_frame canMsg2;
#define outputA a0 //rotARY
#define outputB a1 //ROTARY
#define rotbutton 9 /rotary switch
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //Define LCD display pins RS,E,D4,D5,D6,D7
byte customChar[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000,
0b00000
};
struct can_frame canMsg;
MCP2515 mcp2515(10); // SPI CS Pin 10
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
pinMode (rotbutton,INPUT);
lcd.begin(16,2); //Sets LCD as 16x2 type
lcd.setCursor(0,0); //Display Welcome Message
lcd.print("adjust");
lcd.setCursor(0,1);
lcd.print(" CAN");
delay(3000);
lcd.clear();
SPI.begin(); //Begins SPI communication
Serial.begin(9600); //Begins Serial Communication at 9600 baudrate
mcp2515.reset();
mcp2515.setBitrate(CAN_250KBPS,MCP_16MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode(); //Sets CAN at normal mode
canMsg1.can_id = 0x601; //reads status timer
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x40;
canMsg1.data[1] = 0x00;
canMsg1.data[2] = 0x21;
canMsg1.data[3] = 0x00;
canMsg1.data[4] = 0x00; //read this xx = 00=0 01=01 0A =10 etc max 20
canMsg1.data[5] = 0x00;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x601; //writes status timer
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x20;
canMsg2.data[1] = 0x00;
canMsg2.data[2] = 0x21;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x01; //write this xx = 00=0 01=01 0A =10 etc
canMsg2.data[5] = 0x00;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
}
void loop()
{
mcp2515.sendMessage(&canMsg1);
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) // To receive data (Poll Read)
{
lcd.print(canMsg.can_id, HEX); // print ID
lcd.print(" ");
lcd.print(canMsg.can_dlc, HEX); // print DLC
lcd.print(" ");
for (int i = 0; i<canMsg.can_dlc; i++) { // print the data
lcd.print(canMsg.data[i],HEX);
lcd.print(" ");
}
lcd.println();
if ((canMsg.can_id == 0x701)&& (canMsg.data[0] == 0x5)){
lcd.createChar(0, customChar); // create a new custom character
lcd.setCursor(15, 0); // move cursor to (2, 0)
lcd.write((byte)0); // print the custom char at (2, 0)
delay(1000);
lcd.setCursor(15, 0);
lcd.print(" ");
delay(500);
lcd.println();
{
if ((canMsg.can_id == 0x581) and
(canMsg.data[0] == 0x43)and
(canMsg.data[1] == 0x00)
{
lcd.setCursor(2, 0); // move cursor to (2, 0)
lcd.print("timer");
}}
}
I am trying to read a value from a CAN-device by sending request
601 8 40 00 21 00 00 00 00 00
I will get an response like this:
581 8 43 00 21 00 xx 00 00 00 (xx = 00=0 01=01 0A =10 etc)
and than update value by rotaty swith (0 - 20 ) with command:
`601 8 20 00 21 00 xx 00 00 00 (xx = 00=0 01=01 0A =10 etc)`
hints welcome.