user3862194
user3862194

Reputation: 3

creating and using lookup table based on AD value

I am having a little bit of trouble creating and using a look up table.
My code is for a microcontroller that polls an 11 bit AD converter, and based on the value updates a 7 segment display.

The way i want to write the code is to create a table with 2047 values, each value has a binary pattern attached to it representing which pins and on and off, and then having the table return that binary value as int.

I am however not sure on the syntax to create the table or calling to it.

So far i got:

int table[2048][2];
j=0;
for(i=0; i<2048; i++)
{
    if(i%204==8)
    {
        j++;
    }

    if(j==0)
    {
        value=0x3F;
    }

    if(j==1)
    {
        value=0x06;
    }
    table[i][2]={i},{value}; 
}

the if statements keep going down to j==9, the value is the binary pattern I'm interested in returning.

Is this a smart way of approaching the problem? if the AD value returned is for example 300, how would I look it up in the table to be able to get 0x06 as the number i want?

The bits for each of the second segments are

 111
6   2
6   2
 777
5   3
5   3
 444

So the digit 2 has bits 1, 2, 4, 5, and 7 set,and so is represented by 0x5B

Upvotes: 0

Views: 193

Answers (1)

Mooing Duck
Mooing Duck

Reputation: 66922

That is a lot of duplicated bits, with a lot of runtime overhead, and I can't even think of an efficient way to use it. Why not a lookup table of each of the 10 decimal digits, and simply have a function assemble the result 1 decimal digit at a time?

static const unsigned char segment7digits = {
    0x3F, //0 -> 00111111 
    0x3F, //1 -> 00000110 
    0x5B, //2 -> 01011011
    0x4F, //3 -> 01001111
    0x66, //4 -> 01100110
    0x6D, //5 -> 01101101
    0x7D, //6 -> 01111101
    0x07, //7 -> 00000111
    0x7F, //8 -> 01111111
    0x6F, //9 -> 01101111
    }; //these values are untested and probably have errors

int lookup_7segment_bits(int value) {
   assert(value>=0 && value<=9999);
   int result;
   result  = segment7digits[value/1%10]; //least significant decimal digit
   result |= segment7digits[value/10%10]<<7;
   result |= segment7digits[value/100%10]<<14;
   result |= segment7digits[value/1000%10]<<21; //most significant decimal digit
   return result;
};

Upvotes: 1

Related Questions