Thread: How to index an array of nibbles

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    2

    Question How to index an array of nibbles

    Hello,

    I'm trying to index the digits to set a clock. I have a variable called Parm, which increments with each press of a button (parm++), and is intended to set the corresponding digit on an LCD.

    My clock structure looks like this...

    Code:
    /* --- RTCC Union --- */ typedef union _uRTCC
    {
       struct
       {
          uint8 Sec0: 4;   // Unit value in decimal
          uint8 Sec1: 4;   // Tens value in decimal
          
          uint8 Min0: 4;
          uint8 Min1: 4;
    
    
          uint8 Hrs0: 4;
          uint8 Hrs1: 4;
    
    
          uint8 Wdy0: 4;
          uint8 Wdy1: 4;
    
    
          uint8 Day0: 4;
          uint8 Day1: 4;
    
    
          uint8 Mth0: 4;
          uint8 Mth1: 4;
    
    
          uint8 Yrs0: 4;
          uint8 Yrs1: 4;
       };
       uint8  Reg[8];       // 8 byte access
       uint16 RTC[4];       // 4 word access
    } rtcc;
    
    enum Parm {Sec0, Sec1, Min0, Min1, Hrs0, Hrs1, None};
    rtcc *pTD;
    uint8 Parm = 0;
    
    
    //I want to index the nibbles in turn as parm increments - bit like this...
    
    pTD->Reg[Parm] = xx;
    
    
    I would like to index each nibble in turn, but realize the above indexes the byte, not the nibble. Can someone provide an elegant way to go about this?
    
    Thank you.
    Last edited by Toe007; 11-25-2015 at 11:13 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There is no elegant way (unless you count don't use bit fields as the elegant way - which it is).

    Given
    uint8 Sec0: 4; // Unit value in decimal
    uint8 Sec1: 4; // Tens value in decimal
    There is NO way for you to be sure, given your bit fields
    xxxx yyyy
    whether xxxx is Sec0 or Sec1

    If you don't know this (well you can find out by reading your compiler manual, but no two compilers are guaranteed to do the same thing), you can't go accessing Reg[0] and be sure you're changing the right bits.

    Bit fields are NOT portable. They're fine to read/write in themselves, but useless for overlaying anything else on top of them with a union.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    2
    OK thanks Salem,

    I did not realize bit fields were compiler specific. I also read that one can not create arrays of nybbles, so guess I need to take a different approach to all this. Pity, it looked like it was coming together the way I intended to adjust each digit of the time/date!

    Appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calloc for an array of array with negative index in C
    By george_engineer in forum C Programming
    Replies: 6
    Last Post: 10-15-2014, 03:26 AM
  2. Replies: 1
    Last Post: 10-13-2013, 12:01 AM
  3. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  4. Replies: 1
    Last Post: 08-12-2011, 03:07 AM
  5. About Array Index
    By Kokila in forum C Programming
    Replies: 3
    Last Post: 11-08-2001, 12:29 PM

Tags for this Thread