The code below is something I am investigating, its being built on a mega and put together on visual micro and the latest Arduino IDE.
The problem is that I cant seem to get the integer that I have split to 4 bytes (intVal) and written to the DS1302 to come back out and merge into one 32 bit integer (merge_var)
the issue I have is byte0 byte1 add up but byte2 and byte3 do things I dont understand.
This has beat me. I think the error is in the bit shifting line or it's the wrong type maybe.
Anyway for those interested the idea is to put a form timestamp quantified in minutes on the rtc SRAM so if the power is lost the arduino wont allow the machine to come on till a specified time has elasped.
it does this by comparing when it was last on. but as I'm measuring time in minutes I need to split this to 4 bytes.
Thanks for the reply, could you elaborate for me. I'm relatively new. Are you referring to the union that is splitting the integer into 4 for writing to the SRAM or making a new union to merge the 4 bytes once read?
Also maybe you help my understanding but I thought if you shifted a variable 24<< then it would take the place of bits 24-32 if put into a 32 bit unsigned integer. I thought they shift into the positions: 1-8 byte0, 9-16 byte1, 17-24 byte2, 24-32 byte3.
The problem is that I cant seem to get the integer that I have split to 4 bytes (intVal) and written to the DS1302 to come back out and merge into one 32 bit integer (merge_var)
If you want to bitshift and not use the union to recombine, you need to recast the bytes which are currently being bit shifted to oblivion
Thanks for all the responses, I understand now and have recast the bytes. I didnt realise the shifting is done in the byte3-1 which are only 8 bits. so of course there's no where to go in each byte.
This has really interested me in unions and reading into it I'm going to attempt to see if I can build an array to understand them a bit better tomorrow. I am assuming a union is more efficient and or elegant solution?
I realise there are many ways of doing this.
I thank you again for all the replies.
I did some swatting up on all the relevant topics. I had got it working the easy way but I wanted to master arrays and unions so I stuck at it, A couple of late nights and I worked it out in the end.
Just wanted to post my workings for you and say thanks. A great introduction to a great community.
Suggestion to the code always welcome. Amazing how long a tiny code can take to condense.
Just to confirm, by doing this am I creating another instance of that union and using it as a template to create a new union of data types. or is it re using the first union again data types and data?
dgog71:
Just to confirm, by doing this am I creating another instance of that union and using it as a template to create a new union of data types. or is it re using the first union again data types and data?
Defining the union doesn't create any instances so it doesn't use any space. Each function declares an instance of the union as a local variable so those take up four bytes for as long as the function is running.