This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Packing 12-bit date into int

Hi,
my i have a 12-bit AD-Converter and want to avoid wasting my precious memory space.

Is it possible to put 4k of this 12-bit-word into 1.5k unsigned int (32 bit) variables?

This stuff should be adressable as a kind of ring-buffer.

Think a struct with union should do. But as i'am a relative beginner i'am not sure about the syntax.

Parents
  • Think a struct with union should do.

    That's probably not the best way to approach the problem, since there is no 12-bit data type in C.

    One way would be to divide the circular buffer into 96-bit blocks (3*32 bit and 8*12 bit). A function that reads/writes to the buffer then first finds the block that contains the requested data. It then extracts the data using a scheme similar to:

    data 0 - bits 0:11 of word0
    data 1 - bits 12:23 of word0
    data 2 - bits 24:31 of word0 OR bits 0:3 of word1
    data 3 - bits 4:15 of word1
    data 4 - bits 16:27 of word1
    data 5 - bits 28:31 of word1 OR bits 0:7 of word2
    data 6 - bits 8:19 of word2
    data 7 - bits 20:31 of word2

    This can be implement with a little bit of bit-masking and bit-shifting.

Reply
  • Think a struct with union should do.

    That's probably not the best way to approach the problem, since there is no 12-bit data type in C.

    One way would be to divide the circular buffer into 96-bit blocks (3*32 bit and 8*12 bit). A function that reads/writes to the buffer then first finds the block that contains the requested data. It then extracts the data using a scheme similar to:

    data 0 - bits 0:11 of word0
    data 1 - bits 12:23 of word0
    data 2 - bits 24:31 of word0 OR bits 0:3 of word1
    data 3 - bits 4:15 of word1
    data 4 - bits 16:27 of word1
    data 5 - bits 28:31 of word1 OR bits 0:7 of word2
    data 6 - bits 8:19 of word2
    data 7 - bits 20:31 of word2

    This can be implement with a little bit of bit-masking and bit-shifting.

Children