12 bit variable

Hi,

I'm trying to interface the arduino trough the classical latch, clock and data pins.

I already did this and I know how to do it. The problem is that the data that the chip needs is contained in blocks of 12 bits. This represents a problem as there are no suitable data types, what results in memory wasting and in complex conversions, i.e. shifting bits with the classical (1 << i) bit mask is not anymore so easy.

is it possible to create a 12 bit variable???

I found the following for C++:

http://www.cplusplus.com/reference/stl/bitset/operators/

but I see that it doesn't work in arduino... any ideas???

You could try this:

struct bitset12 { unsigned val:12; };
bitset12 x = 0xfff;

The real answer is (to best of my knowledge) ...

No there is not not. The best you can do is a 16-bit signed, or unsigned, short. Which as you allude to will waste 4-bits per, or packing which will require shifting.

struct bitset2x12 { 
  unsigned a:12; 
  unsigned b:12; 
};
bitset2x12 test;

void setup(){
  Serial.begin(9600);
  Serial.println(sizeof(test));
}

void loop(){}

This prints '3' and (12+12)/8 = 3 so I think it works :slight_smile:

Try making an array, or array pointer, of 12-bit variables. I realize the request didn't state a use case but that will likely not provide the solution the requester had in mind for anything other than very limited run of 12-bit values.

thanks for your help!!!

the solution worked, but: it affected other regions of the memory... :o, it is very strange...

I created the structure "color" and then I created a class "pixel" which contains three of this structures. Another class "row" contains 8 "pixels"
when I assing the values to the "colors" contained in "pixels" of the "row" the arduino does not assing correctly the values of the "colors" and the values of completely unrelated variables are changed. :cry:

do you think that using pointers will solve the trouble???

many many thanks for your help. (code follows.)

pi_classes.h

#include <WProgram.h>

struct pi_row_led_col {
unsigned data:12;
};

struct pi_row_led_col_rgb {
pi_row_led_col r;
pi_row_led_col g;
pi_row_led_col b;
};

class pi_row_led {

public:

pi_row_led(pi_row_led_col led_in_r, pi_row_led_col led_in_g, pi_row_led_col led_in_b);
void change_r(pi_row_led_col led_in_r);
void change_g(pi_row_led_col led_in_g);
void change_b(pi_row_led_col led_in_b);

pi_row_led_col get_r();
pi_row_led_col get_g();
pi_row_led_col get_b();

private:

pi_row_led_col led_r;
pi_row_led_col led_g;
pi_row_led_col led_b;

};

// . . . . . . . . . . . . . . . . . . . . . .

class pi_row {

public:

pi_row(byte pi_row_len_in);
byte pi_row_len;
void row_prtdat();

private:
pi_row_led row_pix[];

};

pi_classes.cpp

#include "pi_classes.h"

pi_row_led::pi_row_led(pi_row_led_col led_in_r, pi_row_led_col led_in_g, pi_row_led_col led_in_b){

led_r.data = led_in_r.data;
led_g.data = led_in_g.data;
led_b.data = led_in_b.data;

}

void pi_row_led::change_r(pi_row_led_col led_in_r) { led_r.data = led_in_r.data; }
void pi_row_led::change_g(pi_row_led_col led_in_g) { led_g.data = led_in_g.data; }
void pi_row_led::change_b(pi_row_led_col led_in_b) { led_b.data = led_in_b.data; }

pi_row_led_col pi_row_led::get_r() { return led_r; }
pi_row_led_col pi_row_led::get_g() { return led_g; }
pi_row_led_col pi_row_led::get_b() { return led_b; }

// . . . . . . . . . . . . . . . . . . . . . .

pi_row::pi_row (byte pi_row_len_in) {

pi_row_len = pi_row_len_in;

pi_row_led_col col_tmp;
col_tmp.data = 2047;

row_pix[pi_row_len];
for (byte i = 0; i < pi_row_len; i++) {

row_pix*.change_r(col_tmp);*
row_pix*.change_g(col_tmp);
row_pix.change_b(col_tmp); *

* }*

}
void pi_row::row_prtdat() {

* for (byte i = 0; i < pi_row_len; i++) {*

* //pi_row_led_col col_tmp;
//col_tmp.data = row_pix.get_r().data; [/color]*

Serial.print(row_pix*.get_r().data, HEX);
_ Serial.print(". .");_
Serial.print(row_pix.get_g().data, HEX);
_ Serial.print(". .");_
Serial.print(row_pix.get_b().data, HEX);*

_ Serial.println(". .");_

* }*

}
// . . . . . . . . . . . . . . . . . . . . . .
[/quote]
and this is what I get when Iuse the row_prtdat():
0. .0. .7FF. .
7FF. .7FF. .7FF. .
7FF. .7FF. .BFF. .
700. .8FF. .7FF. .
AFF. .7FF. .9FF. .
7FF. .7FF. .7FF. .
7FF. .7FF. .7FF. .
7FF. .7FF. .7FF. .
Transmitir........0.0.0.0.1.1.1.1.1.255.8.
there are incorrect values in the array and the numbers in red are normally 0 and 1. (!!!)

Maybe you could post your sketch (correctly, using the # icon)

Hi,

I realized that the memory corruption occurs when I construct the variables. (this section is commented and red now).

I don't have a clue to be honest. Have anybody an idea of what is happening?

if you need the rest of the program, tell me... it is pretty long but I can post it as well.

thx a lot

pi_classes.h

#include <WProgram.h>

struct pi_row_led_col {
  unsigned cdata:12;
};

struct pi_row_led_col_rgb {
  pi_row_led_col r;
  pi_row_led_col g;
  pi_row_led_col b;
};




class pi_row_led {

  public:

  pi_row_led(pi_row_led_col led_in_r, pi_row_led_col led_in_g, pi_row_led_col led_in_b);
  void change_r(pi_row_led_col led_in_r);
  void change_g(pi_row_led_col led_in_g);
  void change_b(pi_row_led_col led_in_b);

  pi_row_led_col get_r();
  pi_row_led_col get_g();
  pi_row_led_col get_b();
  
  private:

  pi_row_led_col led_r;
  pi_row_led_col led_g;
  pi_row_led_col led_b;

}; 


// *. .* *. .* *. .* *. .* *. .* *. .* *. .* *. .* *. .* *. .* *. .*


class pi_row {
  
  public:
  
  pi_row(byte pi_row_len_in);
  byte pi_row_len;
  void row_prtdat();

  private:
  pi_row_led row_pix[];
  pi_row_led *row_pix_pnt[];
  
};

pi_classes.cpp

#include "pi_classes.h"


pi_row_led::pi_row_led(pi_row_led_col led_in_r, pi_row_led_col led_in_g, pi_row_led_col led_in_b){
  
  led_r.cdata = led_in_r.cdata;
  led_g.cdata = led_in_g.cdata;
  led_b.cdata = led_in_b.cdata;
  
}

void pi_row_led::change_r(pi_row_led_col led_in_r) { led_r.cdata = led_in_r.cdata; }
void pi_row_led::change_g(pi_row_led_col led_in_g) { led_g.cdata = led_in_g.cdata; }
void pi_row_led::change_b(pi_row_led_col led_in_b) { led_b.cdata = led_in_b.cdata; }

pi_row_led_col pi_row_led::get_r() { return led_r; }
pi_row_led_col pi_row_led::get_g() { return led_g; }
pi_row_led_col pi_row_led::get_b() { return led_b; }


// *. .* *. .* *. .* *. .* *. .* *. .* *. .* *. .* *. .* *. .* *. .*


pi_row::pi_row (byte pi_row_len_in) {
  
  pi_row_len = pi_row_len_in;
  
  //pi_row_led_col col_tmp;  
  //col_tmp.cdata = 2047;
  
  //row_pix[pi_row_len];  // >>>> here occurs data corruption!!!
  //row_pix_pnt[pi_row_len];  // >>>> here occurs data corruption!!!

  for (byte i = 0; i < pi_row_len; i++) { 
    
    //row_pix_pnt[i] = &row_pix[i];
    //row_pix[i].change_r(col_tmp);
    //row_pix[i].change_g(col_tmp);
    //row_pix[i].change_b(col_tmp);    
    
  }
  
}

void pi_row::row_prtdat() { 
  
    for (byte i = 0; i < pi_row_len; i++) { 
    
    //pi_row_led_col col_tmp;
    //col_tmp.cdata = row_pix[i].get_r().cdata;  
      
    Serial.print(row_pix[i].get_r().cdata, HEX);
    Serial.print(".  .");
    Serial.print(row_pix[i].get_g().cdata, HEX);
    Serial.print(".  .");
    Serial.print(row_pix[i].get_b().cdata, HEX);
    Serial.println(".  .");
    
  }
  
}