Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

My multi talented variable.

Status
Not open for further replies.

Pommie

Well-Known Member
Most Helpful Member
Often now when writing code where I need flexibility with a certain variable I use a type I call multi as it can be multiple types.

It goes like this,
Code:
typedef union{
    uint32_t i;
    int32_t s;
    struct{
        uint16_t lo;
        uint16_t hi;
    };
    struct{
        unsigned :31;
        unsigned neg:1;     
    };
  struct{
    uint8_t b0;
    uint8_t b1;
    uint8_t b2;
    uint8_t b3;
  };
}multi;

And can be used to define a variable,
Code:
multi myVar;

Now myVar can be accessed in many ways,
myVar.i is a 32 bit unsigned integer
myVar.s is a 32 bit signed integer
myVar.lo is the lower 16 bits
myVar.hi is the higher 16 bits
myVar.neg is the sign bit
myVar.b0 to myVar.b3 are the individual bytes of the variable.

A 16 bit variation of this can also be very useful. So you can do things like,
Code:
myVar.b0=loByte;
myVar.b1=hiByte;
return(myVar.i);
Saves on casting, shifting, multiplying, adding etc.

Hope someone finds this useful.

Mike.
 
Hi Mike:

Like you, I use something similar to eliminate math operations...

Regards, Mike

Code:
 /***************************************************************************
  *  program Flash ROM from hex file via serial input at 115200 baud        *
  ***************************************************************************/

  typedef union {                   // **************************************
    uint32_t val;                   //                                      *
    struct {                        // convenient byte access to uint32     *
      uint8_t lo;                   //                                      *
      uint8_t hi;                   //                                      *
      uint8_t ul;                   //                                      *
      uint8_t uh;                   //                                      *
    };                              //                                      *
  } combi32;                        // **************************************

  combi32 addr;

  char progHex()                    // **************************************
  { char bytecnt;                   // hex record length                    *
    char rectype;                   // hex record type                      *
    char data;                      //                                      *
    char cksum;                     // hex record checksum                  *
    char error = 0;                 //                                      *
    addr.val = 0;                   // init 32-bit address (lo.hi.ul.uh)    *
    do                              // process hex file                     *
    { do                            // process hex record                   *
      { putSer(data = getSer());    // echo & flush <cr> and <lf> chars     *
      } while(data != ':');         // until we see a ':' record header     *
      cksum =(bytecnt = getByte()); // get 'byte' (not character) count     *
      cksum+=(addr.hi = getByte()); // get address hi                       *
      cksum+=(addr.lo = getByte()); // get address lo                       *
      cksum+=(rectype = getByte()); // get record type (0, 1, or 4)         *
      if(rectype == 4)              // if <extended address> record         *
      { addr.uh = getByte();        // update hi part of 32-bit address     *
        cksum += addr.uh;           //  "                                   *
        addr.ul = getByte();        //  "                                   *
        cksum += addr.ul;           //  "                                   *
      }                             //                                      *
      if(rectype == 0)              // if <data> record                     *
      { while(bytecnt--)            //                                      *
        { data = getByte();         //                                      *
          cksum += data;            //                                      *
          wrByte(addr.val++, data); // program byte to Flash ROM            *
        }                           //                                      *
      }                             //                                      *
      data = getByte();             // get record checksum byte             *
      if(-data != cksum)            //                                      *
        error = 1;                  // indicate error                       *
    } while(rectype != 1);          // loop until <end-of-file> record      *
    return error;                   //                                      *
  }                                 // **************************************
 
cpt.png
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top