Hello everybody,
I am very new to C language. In my project I get data serially from a port pin. I want to save the first 8 bits to one variable, another to second & so on. In all I want to save 32 bits in 4 bytes. Can you suggest C code.
I know it in assembly by using RRC or RLC, but how to achieve it in C?
Thanks
Thank you christian crosa, Per Westermark.
I will try this. While searching for this I came across a solution which uses union
stackoverflow.com/.../how-do-i-split-up-a-long-value-32-bits-into-four-char-variables-8bits-using
union { unsigned long position; unsigned char bytes[4]; } CurrentPosition; CurrentPosition.position = 7654321;
The bytes can now be accessed as: CurrentPosition.bytes[0], ..., CurrentPosition.bytes[3]
Though I will be using your approach which seems very simple to me. Just asking for my knowledge, can union be used for this conversion?
yep, the use of the union should work too.
I came across a solution which uses union
Be careful with these things! Read about them here:
c-faq.com/.../index.html
In particular, see here:
c-faq.com/.../taggedunion.html
Be careful with these things! Read about them here: 'simple' unions like the long/char combo discussed here is nothing to be afraid of HOWEVER, I have seen unions made by the "I'll show them how smart I am" types that were sheer horror.
Erik
Thank you all of you. You people helped me a lot.