Re: Look-Up Tables Tutorial
Posted: Fri Jan 27, 2012 5:25 am
martin, which file name are you refering to on the c1
Old forums - please visit https://www.flowcode.co.uk/forums
https://www.flowcode.co.uk/mmforums/
another Q, suppose i have 2 look up tables and i want to make the numbers as one since they are both linked to the same count varible, in one table i have a value of 3 and in the other table for the same count value i have a value of 45, how can i put these numbers together to make 345 so i can use this in a microsecond delay directly by using another variable = lut1 and lut2 combinedYou need the us after since if you have 183
ms = 18
us = 300
You can use ms = LUT/10 to retrieve the 18 for ms
You can use the mod function to retrieve the 3 from 183 then * 100
E.g us = (LUT MOD 10 )* 100
So you only need 1 decision branch
Martin
Hi Brandon.brandonb wrote: i gotta say the snippit below is way cool
Yes that should be fairly straight forwardbrandonb wrote:im gonna play with this some and post some files when i get off work..... but there is no way to make a 6 and a 8 become 68?
Don't worry bud, your secret is safe with me!brandonb wrote:dont tell anyone but i though about how to solve that way too long last night![]()
i was playing around with this last night from martins comments on bens post of the excell lut, but how do i do anything with this like use count+1 scheme and such.... by the way martin your lower solution works with same result as top solution..........below is a working lut i did last night messing around, works great on hardware, the odd numbers are subtracting the values like 47 actually gives me 50*10=500u/s(real quick to do)To combine into a 16-bit int variable
INTvar = LUTlow[0] + ( LUThigh[0] << 8 )
This would allow you to store any number between 0 and 65535.
Nice one! Just tried it and yes it works very well.brandonb wrote:check it out martin, got my pwm working...
medelec35 wrote:I thought I would put together a basic tutorial on Look-up tables.
I have Benj to thanks as I first read about LUT here:
http://www.matrixmultimedia.com/mmforum ... 543&#p5509
I thought I would try and write a tutorial form a different perspective.
Thanks werner for spotting my typical spelling errors before posting tutorial on here.
I have added a bit more since so hopefully not too many more have crept in![]()
LOOK–UP TABLES (LUT) TUTORIAL
Equipment:
EB006 programmer with 16F88 microcontroller.
EB003 Sensor board (using pot at pin2) connected to port A
EB005 LCD Board connected to port B
Problem: I have a variable called ADC_Val which can have a range of 0 - 30
For each value I need a precise delay in milliseconds (or it could be a value of thermistor for a corresponding temperature) e.g.As you can see there is no direct formula you can use to derive correct delay time from ADC_ValCode: Select all
ADC_Val Delay_Val 0 7 1 10 2 12 3 16 4 20 5 23 6 25 7 52 8 55 9 61 10 63 11 66 12 70 13 72 14 80 15 85 16 91 17 98 18 100 19 115 20 130 21 146 22 151 23 155 24 160 25 168 26 171 27 180 28 185 29 191 30 203
You can create a flowchart using decision branches but I’m sure you can imagine how large flowchart would be after all 31 values are used. What about if there were 256 different values!
Here is the Flowchart with just 3 out of 31 values:
The most efficient way to solve this is by use of a look-up table.
Basically all the values of delay required are placed in the supplementary code window separated by a comer after the name of LUT variable that is declared. Don’t worry I will showing a completed LUT.
In Flowcode V4 supplementary code window is found by ‘View’ menu, ‘Project Options’
In V5 it’s Build menu, ‘Project Options’ Don’t forget to tick the “Use Supplementary code’ which enables Supplementary code button.
This is how the code in the supplementary code window will look:Look – up tables in Flowcode will always be in the format of:Code: Select all
rom char* Delay_Val = { 7,10,12,16,20,23,25,52,55,61,63,66,70,72,80, 85,91,98,100,115,130,146,151,155,160,168,171,180,191,203 };Notice first number has to be preceded with an open brace {Code: Select all
rom char* Name_Of_Variable_Storing_Data { number0,number1,number2,……………last_number } ;
There are comers after every number except the last number which has a close brace and a semicolon.
NOTE: Don't forget the semicolon!.
If you do you will get a general error that gives the wrong line number . Try it just so you will recognise the error if it does occur.
It Does not matter how many lines you have so long as you don't exceed the maximum quantity of 256 numbers and don't exceed the value of 255 since LUT are 8 Bit.
E.g. for a total 31 you could have 2 rows of 10 numbers + 1 row of 11
Or
1 row of 15, and 1 row of 16 etc.
If you want to use numbers greater than 255 then you will have to use 2 sets of 8 bit LUTs.
Other important things are don't allow the number which will be used to retrieve the LUT value to exceed the quantity of LUT data values.
E.g since I have 31 data items, then ADC_Val can't be higher than 31-1 = 30
If it is, a variable not related to LUT data will be retrieved instead!
Also if the minimum number which will be used to retrieve the LUT value is greater than 0, say 35.
Then you either do 0,0,0,0,0,0 …34 times then 1st data value or an easier way could be ADC_Val = ADC_Val – 35 which is my preferred method
The Name_Of_Variable used e.g. Delay_Val is not created within in normal flowchart variables since it's declared by rom char*
Now the LUT has been created in the supplementary code window we need to create a flowchart in the main work area.
You will need to create a 8bit variable E.g called Delay_Milliseconds and 8bit variable called ADC_Val. These can be created using Variables Manager. One way is 'Edit' menu and select 'Variables' Basically here is how the look-up table system will work.
Think of each value of Delay as an array* so:
Delay_Val(0)=7,
Delay_Val (1)=10………Delay(30)=203 etc.
So you could say Delay_Val=(ADC_Val)
The way to retrieve correct LUT value on flowchart is by using a C Code block.
In this example code block will have:Notice how all variable names after FCV_ must be in upper case since these are how Normal Flowcode variables assigned in CCode: Select all
FCV_DELAY_MILLISECONDS=Delay_Val[FCV_ADC_VAL];
Variable that’s declared in supplementary code window in this example * Delay_Val
Is case sensitive so you must have the capitals D and V
You don't need the underscore so DelayVal is also valid. But same as variable rules … no spaces etc..
Attached is a Flowchart that has the example LUT incorporated so you can see how the example looks in Flowchart form.
You will need to test it out on your hardware to see the LUT Value display correctly as LUT uses C, FLOWCODE SIMULATION WON'T display LUT Value!
Only 0 will be displayed in simulator.
What I usually do is use EB006 programming board and enable ICD ('Edit', ''Project Options' in the flowchart. Or use LCD to display both number that is use for retrieval & value of LUT that's retrieved.
*For anyone who don't know about arrays:
An array:
Starting with variables: is just a storage area which can sore numbers and text, but a variable will hold only one value at a time.
E.g a byte (8 bits) type variable called MY_Variable1 = 25
If you have a list of numbers e.g:And you want to use a specific one then looping though a set of 4 numbers will not be too bad. what about finding a value from 200 numbers. e'g what is the value of MY_Variable180?Code: Select all
MY_Variable1= 7 MY_Variable2= 10 MY_Variable3= 12 MY_Variable4= 16 etc.
To have 200 variables with 200 different names will be very tedious to do and difficult to find. So in programming like basic. C or even in flowcharts that's where arrays are very useful.
An array are just separate storage areas set aside for a variable. The number of separate storage areas are set by square brackets [ ]
So to set aside MY_Variable which could have 200 separate values at the same time, is created by MY_Variable[200] this is created in the variable manager and not C box.
Then using a calulation box, a variable can be retrieved by either
Required_Value=MY_Variable[180]
or
Var=180 < assigned anywhere so long as its assigned before you would like to retrieve contents of MY_Variable
Required_Value=MY_Variable[Var]
Reason why LUT's are used instead of assigning MY_Variable with 200 numbers at the start is because it will take much longer to do:Than in C:Code: Select all
MY_Variable[0]= 7 MY_Variable[1]= 10 MY_Variable[2]= 12 MY_Variable[3]= 16 etc.
rom char* MY_Variable =
{
7,10,12,16 etc
};
Any questions or comments are most welcome.
Thank you
Martin
poweronhand wrote:medelec35 wrote:I thought I would put together a basic tutorial on Look-up tables.
I have Benj to thanks as I first read about LUT here:
http://www.matrixmultimedia.com/mmforum ... 543&#p5509
I thought I would try and write a tutorial form a different perspective.
Thanks werner for spotting my typical spelling errors before posting tutorial on here.
I have added a bit more since so hopefully not too many more have crept in![]()
LOOK–UP TABLES (LUT) TUTORIAL
Equipment:
EB006 programmer with 16F88 microcontroller.
EB003 Sensor board (using pot at pin2) connected to port A
EB005 LCD Board connected to port B
Problem: I have a variable called ADC_Val which can have a range of 0 - 30
For each value I need a precise delay in milliseconds (or it could be a value of thermistor for a corresponding temperature) e.g.As you can see there is no direct formula you can use to derive correct delay time from ADC_ValCode: Select all
ADC_Val Delay_Val 0 7 1 10 2 12 3 16 4 20 5 23 6 25 7 52 8 55 9 61 10 63 11 66 12 70 13 72 14 80 15 85 16 91 17 98 18 100 19 115 20 130 21 146 22 151 23 155 24 160 25 168 26 171 27 180 28 185 29 191 30 203
You can create a flowchart using decision branches but I’m sure you can imagine how large flowchart would be after all 31 values are used. What about if there were 256 different values!
Here is the Flowchart with just 3 out of 31 values:
The most efficient way to solve this is by use of a look-up table.
Basically all the values of delay required are placed in the supplementary code window separated by a comer after the name of LUT variable that is declared. Don’t worry I will showing a completed LUT.
In Flowcode V4 supplementary code window is found by ‘View’ menu, ‘Project Options’
In V5 it’s Build menu, ‘Project Options’ Don’t forget to tick the “Use Supplementary code’ which enables Supplementary code button.
This is how the code in the supplementary code window will look:Look – up tables in Flowcode will always be in the format of:Code: Select all
rom char* Delay_Val = { 7,10,12,16,20,23,25,52,55,61,63,66,70,72,80, 85,91,98,100,115,130,146,151,155,160,168,171,180,191,203 };Notice first number has to be preceded with an open brace {Code: Select all
rom char* Name_Of_Variable_Storing_Data { number0,number1,number2,……………last_number } ;
There are comers after every number except the last number which has a close brace and a semicolon.
NOTE: Don't forget the semicolon!.
If you do you will get a general error that gives the wrong line number . Try it just so you will recognise the error if it does occur.
It Does not matter how many lines you have so long as you don't exceed the maximum quantity of 256 numbers and don't exceed the value of 255 since LUT are 8 Bit.
E.g. for a total 31 you could have 2 rows of 10 numbers + 1 row of 11
Or
1 row of 15, and 1 row of 16 etc.
If you want to use numbers greater than 255 then you will have to use 2 sets of 8 bit LUTs.
Other important things are don't allow the number which will be used to retrieve the LUT value to exceed the quantity of LUT data values.
E.g since I have 31 data items, then ADC_Val can't be higher than 31-1 = 30
If it is, a variable not related to LUT data will be retrieved instead!
Also if the minimum number which will be used to retrieve the LUT value is greater than 0, say 35.
Then you either do 0,0,0,0,0,0 …34 times then 1st data value or an easier way could be ADC_Val = ADC_Val – 35 which is my preferred method
The Name_Of_Variable used e.g. Delay_Val is not created within in normal flowchart variables since it's declared by rom char*
Now the LUT has been created in the supplementary code window we need to create a flowchart in the main work area.
You will need to create a 8bit variable E.g called Delay_Milliseconds and 8bit variable called ADC_Val. These can be created using Variables Manager. One way is 'Edit' menu and select 'Variables' Basically here is how the look-up table system will work.
Think of each value of Delay as an array* so:
Delay_Val(0)=7,
Delay_Val (1)=10………Delay(30)=203 etc.
So you could say Delay_Val=(ADC_Val)
The way to retrieve correct LUT value on flowchart is by using a C Code block.
In this example code block will have:Notice how all variable names after FCV_ must be in upper case since these are how Normal Flowcode variables assigned in CCode: Select all
FCV_DELAY_MILLISECONDS=Delay_Val[FCV_ADC_VAL];
Variable that’s declared in supplementary code window in this example * Delay_Val
Is case sensitive so you must have the capitals D and V
You don't need the underscore so DelayVal is also valid. But same as variable rules … no spaces etc..
Attached is a Flowchart that has the example LUT incorporated so you can see how the example looks in Flowchart form.
You will need to test it out on your hardware to see the LUT Value display correctly as LUT uses C, FLOWCODE SIMULATION WON'T display LUT Value!
Only 0 will be displayed in simulator.
What I usually do is use EB006 programming board and enable ICD ('Edit', ''Project Options' in the flowchart. Or use LCD to display both number that is use for retrieval & value of LUT that's retrieved.
*For anyone who don't know about arrays:
An array:
Starting with variables: is just a storage area which can sore numbers and text, but a variable will hold only one value at a time.
E.g a byte (8 bits) type variable called MY_Variable1 = 25
If you have a list of numbers e.g:And you want to use a specific one then looping though a set of 4 numbers will not be too bad. what about finding a value from 200 numbers. e'g what is the value of MY_Variable180?Code: Select all
MY_Variable1= 7 MY_Variable2= 10 MY_Variable3= 12 MY_Variable4= 16 etc.
To have 200 variables with 200 different names will be very tedious to do and difficult to find. So in programming like basic. C or even in flowcharts that's where arrays are very useful.
An array are just separate storage areas set aside for a variable. The number of separate storage areas are set by square brackets [ ]
So to set aside MY_Variable which could have 200 separate values at the same time, is created by MY_Variable[200] this is created in the variable manager and not C box.
Then using a calulation box, a variable can be retrieved by either
Required_Value=MY_Variable[180]
or
Var=180 < assigned anywhere so long as its assigned before you would like to retrieve contents of MY_Variable
Required_Value=MY_Variable[Var]
Reason why LUT's are used instead of assigning MY_Variable with 200 numbers at the start is because it will take much longer to do:Than in C:Code: Select all
MY_Variable[0]= 7 MY_Variable[1]= 10 MY_Variable[2]= 12 MY_Variable[3]= 16 etc.
rom char* MY_Variable =
{
7,10,12,16 etc
};
Any questions or comments are most welcome.
Thank you
Martin
Hi martin, sorry for my late reply have been tied up at work. I will post my flowchart later today so u can have a look at what I have done so far. Thanks.medelec35 wrote:Hi vonskyes,
If you post what you have done so far, then I will have a look at you Flowchart, and let you know where your going wrong.
Maritn
I have put in the c code in the supplementary code window. its two tables of values. the first compares undervoltage (UV) against decimal equivalent and the second one is overvoltage (OV) against decimal equivalent. when i set the switches it reads it in as binary and then coverts it to decimal. the first three switches sets undervoltage level while the last three sets for over voltage level. so that when i set the switch it coverts it to a decimal value and that decimal value is matched to a corresponding value in the table. I hope you can understand what the code is all about with the explanation i have given. Pls let me know it there is any question. Thank you so much for your help. Earnestly waiting for you response.vonskyes wrote:Hi martin, sorry for my late reply have been tied up at work. I will post my flowchart later today so u can have a look at what I have done so far. Thanks.medelec35 wrote:Hi vonskyes,
If you post what you have done so far, then I will have a look at you Flowchart, and let you know where your going wrong.
Maritn