• AVR Freaks

Hot!how to use individual bytes of a long integer ( C code)

Page: 123 > Showing page 1 of 3
Author
davea
Super Member
  • Total Posts : 278
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: Tampa Bay FL USA
  • Status: offline
2020/07/12 21:00:55 (permalink)
0

how to use individual bytes of a long integer ( C code)

Hi , guys
I have
uint24_t S_cnt;
but I need to get to each individual byte as HI, MID, LO for read/write
and still use S_cnt to read/write as a 24 bit number
 
so I changed it to
uint8_t S_cnt[3];
but it got messy converting back and forth to 24 bits
is there a better way to do this..
I have looked into union and struct but they don't seem to apply
thanks David
post edited by davea - 2020/07/14 09:22:51
#1

50 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: what is the best way ( C code) 2020/07/12 21:47:22 (permalink)
    +4 (6)
    Yes, a union would be the simplest way to do it.

    union
    {
        uint24_t s24;
        uint8_t s8[3];
    } S_cnt;


    S_cnt.s24 = 0x123456;
    S_cnt.s8[0] = 0x56;
    S_cnt.s8[1] = 0x34;
    S_cnt.s8[2] = 0x12;

     
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Hen
    Senior Member
    • Total Posts : 86
    • Reward points : 0
    • Joined: 2018/10/24 04:01:44
    • Location: 0
    • Status: offline
    Re: what is the best way ( C code) 2020/07/12 22:42:06 (permalink)
    +1 (3)
    Precissimo, but I would probably not call these s24/s8 grin: grin
    #3
    davea
    Super Member
    • Total Posts : 278
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: what is the best way ( C code) 2020/07/12 22:42:46 (permalink)
    0 (2)
    thanks ric
     
    so if I write
    S_cnt.s8[0] = 0xAA;
    temp = S_cnt.s24;
    then temp will be 0x1234AA
     
     
     
    #4
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: what is the best way ( C code) 2020/07/12 22:47:42 (permalink)
    +2 (4)
    Hen
    Precissimo, but I would probably not call these s24/s8 grin: grin

    Totally what you want. I just wanted the example to be clear what size each was.

    so if I write
    S_cnt.s8[0] = 0xAA;
    temp = S_cnt.s24;
    then temp will be 0x1234AA

    Yes, assuming this follows the lines I showed.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    davea
    Super Member
    • Total Posts : 278
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: what is the best way ( C code) 2020/07/12 22:59:56 (permalink)
    0 (2)
    is this correct
    union
    {
    bank2    uint24_t s24;
    bank2    uint8_t s8[3];
    } S_cnt;
     
    most likely the 2nd bank2 is not needed
    post edited by davea - 2020/07/12 23:05:48
    #6
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: what is the best way ( C code) 2020/07/12 23:09:21 (permalink)
    +2 (4)
    No, you can't specify individual banks for each member (even if they're the same).
    You might be able to declare the whole union in bank2. I've never tried. It might be easier to typedef the union, then create an instance of that type with a "bank2" qualifier.
    Maybe a C expert can clarify.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: what is the best way ( C code) 2020/07/12 23:18:42 (permalink)
    +2 (4)
    Try:
    bank2 union 
    {
        uint24_t s24;
        uint8_t s8[3];
    } S_cnt;

    Of course, you must have set the compiler to observe memory qualifiers.
    I don't think the "bank2" qualifier is supported in C99 mode.
    "__bank(2)" might work.
     
    post edited by ric - 2020/07/12 23:20:23

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    davea
    Super Member
    • Total Posts : 278
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 09:23:56 (permalink)
    0 (2)
    here is what i used
    bank2 union
    {
       uint24_t All;
       uint8_t Byte[3];
    } S24_cnt;

    works as advertised 
    thanks again
    if only I could change the title to something more useful.. :(
    #9
    NKurzman
    A Guy on the Net
    • Total Posts : 18858
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: what is the best way ( C code) 2020/07/13 10:59:52 (permalink)
    +2 (4)
    I think the bank is ignored by default, and has been since OCG.  There should be a compiler switch to enable it.
    #10
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: what is the best way ( C code) 2020/07/13 13:30:14 (permalink)
    +4 (6)
    davea
    if only I could change the title to something more useful.. :(

    You can, just edit the first post.
     
    NKurzman
    I think the bank is ignored by default, and has been since OCG.  There should be a compiler switch to enable it.

    That is what I said back in post#8...
    ric
    Of course, you must have set the compiler to observe memory qualifiers.

    That's in the project properties.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11936
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 14:47:04 (permalink)
    +1 (3)
    Is there some reason you're trying to place the object in a particular bank?  The reason those qualifiers are not enabled by default is that it's unlikely you're going to improve anything by using them, but there's a good chance you will make things worse.
    #12
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 17:34:11 (permalink)
    0 (4)
    Different methods, I prefer to cast.
     
    unsigned long color = 0x12345678;
    unsigned char *x    = (unsigned char *) &color;
     
    Using a pointer:
    x[0] = 0x9A;
    x[1] = 0xBC;
     
    *x++ = 0xDE;
     
    I prefer selectors:
     
    typedef struct {
        unsigned char Red;     // Select Red Color      0 .. 255
        unsigned char Green; // Sectect Green Color 0 .. 255
        unsigned char Blue;   //  Select Blue Color       0 .. 255
        unsigned char White;
    }RGBW_t;

     
    unsigned long color = 0x12345678;
    RGBW_t  *x  = (RGBW_t *) &color, *y = &x;
     
    x.Red = 0x42;
    y -> Red = 0x43;
     
    RGBW_t
    The comments appear in the "man" field when you use the item selector
    in MPLab X if you wonder why that pops up empty.
     
    If an include file has a comment after a function then that comment appears as "man" explaining
    what the function does if ctrl' and click over a funnction.
    post edited by Gort2015 - 2020/07/13 17:35:57

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #13
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 17:54:44 (permalink)
    -3 (7)
    An intelligent Human would not have downvoted.
     
    You may have learnt something about casting and "Man"
    that pops up empty.
     
    You make yourself look stupid.
     
    My Brain is only a Receiver,
    In the Universe there is a Core from which we obtain Knowledge.
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #14
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11936
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 17:58:35 (permalink)
    +1 (5)
    Lots of downvoting going on  today.  Maybe it's a COVID side-effect.
    #15
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 18:24:55 (permalink)
    -3 (5)
    It's what dumb People do, they know no better.
    They have no interest in coding like we do.
     
    I'd like to see their code though..

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #16
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 18:27:16 (permalink)
    -3 (5)
    I make an effort when I make a post like that.
    I didn't test it so maybe there was a typo.
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #17
    LdB_ECM
    Super Member
    • Total Posts : 406
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 19:36:14 (permalink)
    +1 (3)
    There are a couple of typos :-)
     
    Interesting assignment :-)
    *y = &x
     
    Good luck with this x is a pointer
    x.Red = 0x42;
     
    In your color structure it isn't white it's alpha and it probably should be packed for portability (it will work on XC8).
    #18
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11936
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: what is the best way ( C code) 2020/07/13 20:48:27 (permalink)
    +1 (3)
    it probably should be packed for portability

     
    While the code above will almost surely work on an 8-bit platform, none of the code in this thread is portable, and packing won't change that (even the concept packing isn't portable).
    #19
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: what is the best way ( C code) 2020/07/13 20:52:01 (permalink)
    +1 (3)
    It all comes back to your definition of "best" :)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5