I personally think you need to step out of your comfort zone here, what degree are you studying for anyway?Thank you Irving for advice! This is my last faculty course and when I pass this, I have degree. But, I'm not into programming so I find this hard to solve because I'm not so interested in embedded programming to be honest.
I'm completely bad in embedded programming and I was lazy to study when we had this type of programming on college. I tried something with the help of internet, but I'm not really sure if this is the right solution for this exercises. Here is 1st exercise:
View attachment 270166
Don't know how to write errors and how to write "The function must return 0 if not sent, otherwise 1".
I will try to at least solve the last 2 exercises and maybe get the right solution. Thanks in advance. Cheers, Luka
Mechatronics engineer. But I’m more into IT, web development…I personally think you need to step out of your comfort zone here, what degree are you studying for anyway?
Hi LukaThank you Irving for advice! This is my last faculty course and when I pass this, I have degree. But, I'm not into programming so I find this hard to solve because I'm not so interested in embedded programming to be honest.
I'm completely bad in embedded programming and I was lazy to study when we had this type of programming on college. I tried something with the help of internet, but I'm not really sure if this is the right solution for this exercises. Here is 1st exercise:
View attachment 270166
Don't know how to write errors and how to write "The function must return 0 if not sent, otherwise 1".
I will try to at least solve the last 2 exercises and maybe get the right solution. Thanks in advance. Cheers, Luka
Hi Luka, Since your attempt was a little off the mark, there's clearly a lot of background you need to catch up on. So in this tutorial I've probably given more direction and left less to the reader than normal or that some on AAC would accept. But there's still work for you to do to make it a complete answer.Thank you Irving for advice! This is my last faculty course and when I pass this, I have degree. But, I'm not into programming so I find this hard to solve because I'm not so interested in embedded programming to be honest.
I'm completely bad in embedded programming and I was lazy to study when we had this type of programming on college. I tried something with the help of internet, but I'm not really sure if this is the right solution for this exercises. Here is 1st exercise:
View attachment 270166
Don't know how to write errors and how to write "The function must return 0 if not sent, otherwise 1".
I will try to at least solve the last 2 exercises and maybe get the right solution. Thanks in advance. Cheers, Luka
unsigned char enable;
unsigned char error;
.
.
short inCur;
short inPwr;
int send_to_can(void);
byte data_to_send[6];
looking like:// initialise some global values
inPwr = 0x1234;
inCur = 0xF99;
error = 0x1;
enable = 0x1;
long long result; // the 64bit variable to build the data packet in...
result = inPwr;
//After result = inPwr: 00000000 00000000 00000000 00000000 00010010 00110100
result <<= 12; //shift up 12 to make room for next field (inCur)
//After result <<= 12: 00000000 00000000 00000001 00100011 01000000 00000000
result |= (inCur & 0FFF); //mask off lower 12 bit and bit-wise OR into result
//After result |= inCur: 00000000 00000000 00000001 00100011 01001111 10011001
result <<= 12; //shift up 12 to make room for next field (inVolt)
//After result <<= 12: 00000000 00010010 00110100 11111001 10010000 00000000
.
.
//After setting inVolt, status, inOC, inOV, inUV (all to 0 and shifting appropriately, exercise for reader to complete):
//After further shifts: 00001001 00011010 01111100 11001000 00000000 00000000
result |= (error & 0x01); //mask off lsb and bit-wise OR into result
//After result |= error: 00001001 00011010 01111100 11001000 00000000 00000001
result <<= 1;
//After result <<= 1: 00010010 00110100 11111001 10010000 00000000 00000010
result |= (enable & 0x01); //mask off lsb and bit-wise OR into result
//After result |= enable: 00010010 00110100 11111001 10010000 00000000 00000011
//Done, phew!
struct data_to_send {
long long _enable :1; //1 bit, bit 0
long long _error:1; // 1 bit, bit 1
.
.
long long_inCur:12; // bits 20 - 31 - assuming previous bits properly declared = 2^12 = 0 - 4095
long long_inPwr:16; // bits 32 - 47 -> 2^16 = 0 - 65535
};
data_to_send._enable = enable; // simple copy LSb of global to data packet bit 0
//After data_to_send._enable = enable: 00000000 00000000 00000000 00000000 00000000 00000001
.
data_to_send._inPwr = inPwr; // maps global short bits 0-15 to 16 bit element
//After data_to_send._inPwr = inPwr: 00010010 00110100 00000000 00000000 00000000 00000001
data_to_send._error = error;
//After data_to_send._error = error: 00010010 00110100 00000000 00000000 00000000 00000011
union map_it{
short val;
byte data[4];
};
union data_to_send{
byte data[6];
long long result;
struct { //anonymous structure
long long _enable :1; //1 bit, bit 0
long long _error:1; // 1 bit, bit 1
.
.
long long _inCur:12; // bits 20 - 31 - assuming previous bits properly declared
long long _inPwr:16; // bits 32 - 47
};//end structure
};//end union
for(int i=5; i>=0; i--){
//do something with ...
data_to_send.data[i];
}
int send_to_can(void){
//declare structure - partially. To do - complete declaration
union data_to_send{
byte data[6];
//long long result; //not needed for structure version.
struct { //anonymous structure
long long _enable:1; //1 bit, bit 0
long long _error:1; // 1 bit, bit 1
.
.
long long _inCur:12; // bits 20 - 31 - assuming previous bits properly declared
long long _inPwr:16; // bits 32 - 47
};//end structure
};//end union
//copy values from globals - partially. To do, complete copy
data_to_send._enable = enable; // simple copy LSB of global to data packet bit 0
data_to_send._inPwr = inPwr; // maps global short bits 0-15 to 16 bit element
data_to_send._error = error;
//send to CANBus - this is my platform's send routine which takes an ID, a byte array, and a length
// and returns 0 if the send was acknowledged, ie someone accepted it and -1 if it timed out.
int res = sendCANMessage(ID, data_to_send.data, 6);
// return result to caller
if (res == -1) //failed? return 0
return 0;
return 1; //if res was 0, will end up here...
};// end send_to_can