Shop OBEX P1 Docs P2 Docs Learn Events
12 Bit Variable — Parallax Forums

12 Bit Variable

rlmack33rlmack33 Posts: 2
edited 2005-12-23 00:12 in BASIC Stamp
I was wondering if anyone knows if there is a way to declare 12 bits (byte + nib) to a variable instead of 16 bits·(word). I don't need 65535, I just need 4095 and would hate to waste 4 bits that could be used someplace else.

Comments

  • sparksandtabssparksandtabs Posts: 3
    edited 2022-12-08 17:55
    ..
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-12-22 14:20
    rlmack33 -

    There are a number of methods you might use, but the best method, and answer will only be forthcoming with more information. We need to know more about the specific application.
    Please realize I have no knowledge of your programming expertise, so please don't be offended by any of the questions below.

    1. Will it be used at ALL within the PBASIC Stamp per se, or is this only for the benefit of some external component? By way of explanation, many external devices require a 12-bit data field be passed to them, rather then an 8-bit (byte) or 16-bit (word) field. This is something of a special case and there are certain techniques for handling these requirements. This technique can often be used for any "odd" (non-nib, non-byte, non-word) number of bits, for external transmission.

    2. If it will be used within the PBASIC Stamp per se, will you be doing any math with this "triple nibble"? If so, which mathematical operations (addition, subtraction, multiplication or division)?

    3. Will this be a short term or long term use within your program? In other words, is this only used (say) in one routine, and then never needed again, or only needed under certain special circumstances?

    3. I trust you realize that not only can variables and variable space in RAM be reassigned and re-used, but aliasing of variable names (multiple use of the same variable space) is also possible and permissable.

    4. Based on what was just said in #3, why don't you just use a WORD variable, since after you're done with this "triple nibble", the variable space can be re-used; persuming this is only a short term use.

    This should help us zero in on your specfic requirements.

    Regards,

    Bruce Bates
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-12-22 15:29
    Please see the following thread where this question has recently been asked/answered.· Take care.

    http://forums.parallax.com/showthread.php?p=560730

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • rlmack33rlmack33 Posts: 2
    edited 2005-12-22 22:32
    what is going on in the program is a user defined number (0 to 4095) will be assigned to a variable and stay with that variable until the user changes. This number is stored in an external EEPROM via I2C. 4095 is 12 bits, and having to use a word (16 bits) wastes 1 Nib (4 bits). So in a nut shell this is a user defined number where no math is involved. I'm not worried about EEPROM space just ram space. When you declare 8 Words and only using 12 Bits of each Word, that leaves 2 Words of unusable space. Note that the number will never be a negitive number.

    Hope this gives you a beter idea of what I ave trying to do.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-12-23 00:12
    Even the Word variable type is synthesized by the BASIC Stamp, as the only native variable types for the PIC or SX (core processors used in BASIC Stamps) are Bytes (8 bits) and Bits. Since you're storing the value externally, the Word variable is the most effecient way for you to go, and you can use the BYTE variable modifier in your I2COUT and I2CIN instructions:

    I2COUT SDA, slaveAddr, addrHi\addrLo, [noparse][[/noparse]value.BYTE0, value.BYTE1]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.