Hi,
I've had a hard time the last few days on this issue.
I am building a CAN BUS gauge for the car. Battery value is 16 bit from CAN BUS. My code is filtering 8 bytes of data from each address and filling a buffer of [8] in my code. In these parts of the code I am trying to be tricky adding +255 to buf[2] if buf[3]==1, but I need a better way to make this a full 16 bit int.
example of CAN address and data going into buf in DEC:
Address ID 0x04
DLC 8
data0 (buf[0])
0
data1 (buf[1])
0
data2 (buf[2])
203
data3 (buf[3])
1
data4 (buf[4])
0
data5 (buf[5])
0
data6 (buf[6])
0
data7 (buf[7])
0
Data 2 and 3 are 16 bit for Battery. 0-255 on buf[2] (0xFF or 255) is 6.88 volts. How do I combine buf[2] and buf[3] to make my 16 bit value? Voltage 0-20 is the scale so that would mean I need to create one buffer or int to make 0-740 decimal. This is basic example with only part of the code, but should be understandable:
unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[8];
void setup()
{
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
CAN.init_Mask(1, 0, 0x3ff);
pinMode(29, INPUT);
}
void MCP2515_ISR()
{
flagRecv = 1;
//displaystatus = 0;
}
void loop()
{
int battvalue1 = (buf[2]);
int battvalue2 = (buf[3]);
int battscale1 = (buf[2] *0.027);
int battscale = (battvalue1 + 255);
int battscale2 = (battscale *0.027);
if (buf[3]==0) {
Serial.println (battscale1);
}
if (buf[3]==1) {
Serial.println (battscale2);
}
Delta_G:
If you just want to put two bytes together to form an int do this.
byte lowByte = 234;
byte highByte = 1;
int combined = ((int) highByte << 8) | lowByte;
Cast the highByte to int so you can shift it over by 8 bits and then add the lowByte.
I can't thank you enough! it works. The last thing I was looking at before posting was bit shifting. Thank you...
The only other major issue I've had is figuring how I can make separate buffers for each address for the different gauges.
The address IDs are 0x00 through 0x06. For example, right now I am telling it to fill buf[8] with address 0x00 when I select a gauge, then another gauge may be 0x04 so I call that address to fill buf[8], but I get bleed over for a second from the buffer not getting filled with the right info.
Using the same example, you can see I call 0x03 when I select the gauge. When the filters were in "setup" the printed values kept changing erratically.
unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[8];
void setup()
{
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
CAN.init_Mask(1, 0, 0x3ff);
pinMode(29, INPUT);
}
void MCP2515_ISR()
{
flagRecv = 1;
//displaystatus = 0;
}
void loop()
{
flagRecv = 0; // clear flag
if (canidstatus == 0) {
CAN.init_Filt(0, 0, 0x00);
CAN.readMsgBuf(&len, buf);
{
//Serial.print("0x");
//Serial.print(buf[i], HEX);
//Serial.print("\t");
}
} else {
if (canidstatus == 1) {
CAN.init_Filt(0, 0, 0x01);
CAN.readMsgBuf(&len, buf);
{
//Serial.print("0x");
//Serial.print(buf[i], HEX);
//Serial.print("\t");
}
} else {
if (canidstatus == 2) {
CAN.init_Filt(0, 0, 0x02);
CAN.readMsgBuf(&len, buf);
{
//Serial.print("0x");
//Serial.print(buf[i], HEX);
//Serial.print("\t");
}
} else {
if (canidstatus == 3) {
CAN.init_Filt(0, 0, 0x03);
CAN.readMsgBuf(&len, buf);
{
//Serial.print("0x");
//Serial.print(buf[i], HEX);
//Serial.print("\t");
}
} else {
if (canidstatus == 4) {
CAN.init_Filt(0, 0, 0x04);
CAN.readMsgBuf(&len, buf);
{
//Serial.print("0x");
//Serial.print(buf[i], HEX);
//Serial.print("\t");
}
}
}
}
}
}
}
}
{
canidstatus = 3; //calling ID address to fill buf[8] with new info
int battvalue1 = (buf[2]);
int battvalue2 = (buf[3]);
int battscale1 = (buf[2] *0.027);
int battscale = (battvalue1 + 255);
int battscale2 = (battscale *0.027);
if (buf[3]==0) {
Serial.println (battscale1);
}
if (buf[3]==1) {
Serial.println (battscale2);
}