How to implement a look up table

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi folks,

As per title, 

 

I need to compare the 8 bit value from the potentiometer and match a given output which is not linear. For this i plan to use a look up table, but have no idea how to implement the code to read and output the values.

Could someone shed some light on this, please?

 

Regards

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's not clear to me what you are asking.  A lookup table is just an array.  y=lookup[x].  But I think you have something else in mind.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Type table lookup avr into google search and be amazed at what a search engine can do. Maybe add some words of your own just for giggles.

Last Edited: Thu. Oct 30, 2014 - 07:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
static uint16_t lookup[256] = {12, 33, 55, 65, 73, 55, 12, 11, 99, 33, 42 <256 values in this array>};

uint8_t pot;
uint16_t convertedValue;

pot =  = ReadADC();
convertedValue = lookup[pot]; 

//if your compiler complains about 8 bit indexes you'll have to cast it or disable the warning
convertedValue = lookup[(int)pot]; 

 

Keith Vasilakes

Firmware engineer

Minnesota

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or perhaps even better, since it's an avr:

const __flash uint16_t lookup[256] = {12, 33, 55, 65, 73, 55, 12, 11, 99, 33, 42 <256 values in this array>};

(That's 2 underbars before the flash. Don't confuse yourself like I do.)

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kk6gm wrote:

It's not clear to me what you are asking.  A lookup table is just an array.  y=lookup[x].  But I think you have something else in mind.

 

Yes, thats what I have on mind, this is a new subject and I am starting it from first principles, thats all ;)

 

keith v, thank you very much for your example, it has helped shed some light!

Torby, I am guessing your approach means that rather than loading the array into the ram, I am reading directly from the flash, am I correct?

Is there any advantage in any of the modes, speed wise?

 

So far i am just a bit confused with one thing: I need to load 3 values from the lookup table. Say i have LED1, LED2 and LED3 and I wish to implement and RGB output depending on the value of my pot. So I will read one column for GREEN, other for BLUE, and finally one for RED. The values are then copied into a temp variable that I use to set the PWM timers, also from a value from 0 to 255. Assume my pot value is 125, which will give me a predefined color and then I load the value for the 3 tables on position 125.

 

Is this the right way to set this?

 

table_1[125] = x;
table_2[125] = y;
table_3[125] = z;

 

Assuming it is, Would there by a way I could call just one table and somehow read the 3 values (x,y,z) from it at the same time? I need to clarify that the values are not related (for example, if I was to do a three phase wave, I could load just one table and add an offset to get the correct value, but for this application the same trick wont work)

 

Many Thanks

Last Edited: Thu. Oct 30, 2014 - 10:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Your 3 tables are one way to do it, but personally I'd use a single table of a structure holding one R, one G and one B.

 

typedef struct
{
  uint8_t r;
  uint8_t g;
  uint8_t b;
} RGB;

const __flash RGB RGB_table[125] = {{ 0,  1,  2},
                                    {10, 20, 30},
                                ...etc...

etc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To add to kk6gm's example (which is the way I'd do it too). The access to the data is then:

potR =  = ReadADC();
convertedRValue = RGB_table[potR].r; 

potG =  = ReadADC();
convertedGValue = RGB_table[potG].g; 

etc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff,

 

Why would you see fit to reread the ADC for each colour? The OP said he is using a single pot.

 

Cheers,

 

Ross

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kk6gm wrote:
Your 3 tables are one way to do it, but personally I'd use a single table of a structure holding one R, one G and one B.

That's probably how I'd do it.

 

Another alternative would be to use a 2-dimensional array...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why would you see fit to reread the ADC for each colour? The OP said he is using a single pot.

Oh I didn't read that bit blush

Another alternative would be to use a 2-dimensional array...

I guess that depends on whether you want to numerically index the R,G,B values or whether you are just happy to refer to them by name - in which case the AoS is perhaps more documentary.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Another alternative would be to use a 2-dimensional array...

Which is, in fact, the way Atmel do it in their examples for the QT1 Xplained boards:

 

const uint8_t PWM_RGB_values[64][3] = {
	{20, 0, 0},
	{20, 0, 0},
	{19, 1, 0},
	{18, 2, 0},
	{17, 3, 0},
	{16, 4, 0},
	{15, 5, 0},
	{14, 6, 0},
	{13, 7, 0},
	{12, 8, 0},
	{11, 9, 0},
 	{10, 10, 0},
    :
    :

 

clawson wrote:
I guess that depends on whether you want to numerically index the R,G,B values or whether you are just happy to refer to them by name - in which case the AoS is perhaps more documentary.

Indeed.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Although you could use an enum to "name" the array index values...

 

This, of course, is all standard software design stuff - nothing specific to microcontrollers.

 

"Algorithms + Data Structures = Programs" - Niklaus Wirth

 

http://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there any advantage in any of the modes, speed wise?

Reading from flash is slower than reading from sram.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:

Is there any advantage in any of the modes, speed wise?

Reading from flash is slower than reading from sram.

 

Yes, but how much slower? I will be performing about 10 readings per second, will the reading latency be negligible at this rate?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

cts_casemod wrote:

 

Koshchi wrote:

 

Is there any advantage in any of the modes, speed wise?

Reading from flash is slower than reading from sram.

 

 

 

Yes, but how much slower? I will be performing about 10 readings per second, will the reading latency be negligible at this rate?

Completely negligible.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I betcha a search in the datasheet fot 'cycle' might find a sentence that answers the question.

Imagecraft compiler user