C lookup table help

Thread Starter

blackmamba

Joined Dec 4, 2009
16
I have developed some code to implement the look-up table in C, but I'm still having some issues.
The code should create a 256 byte array, populate it with delay times in micro seconds and then stick it in program memory for me to refrence based on ADC byte outputs.
Each ADC byte output would correspond to a delay time in the lookup table.
Can someone tell me what I'm doing wrong as the code wont compile.

Thanks for your time
N.B: even the for loop part is throwing an identifier error. I'm stumped

int table [256];
int i;
for (i=0; i<256; i++)
able=((1/120000000)-(1/120000000*i/255)); //lookup table values

const int table= delay_table; //create lookup table in memory
///////////////////////////////////////////////////////////////////////////////
void main (void)
{
ADCON0=0x02; // start A/D conversion
do{} // crude polling of GO/DONE bit to see when it gets
while (ADIF!=1); //cleared which indicates ADC is complete
ADIF=0;
int dig_controlvol;

dig_controlvol=ADRESH; //pass adc result to variable

dim_delay=delay_table[dig_controlvol]; //ADC result corresponds to lookup table time in us

}
 

AlexR

Joined Jan 16, 2008
732
What compiler?
What target device?
What errors?
Plus a full listing using the "code" tags would be helpful.
 

ftsolutions

Joined Nov 21, 2009
48
+1 to all the above, plus, in the code you've shown, you have an array defined as "table". But, your for loop only shows "able" in the array initialization, not "table", so compiler has no idea what you are doing ("able" array not defined).

Your "const int table = delay_table" only (attempts to)redefine variable "table" as a const int. But, what is on the right side of the equal sign is another undefined variable name. This will not work again, as no compiler can figure out what to do with it.

Either you're missing alot of code here, or serious cut/paste problems posting it into the message.
 

rjenkins

Joined Nov 6, 2005
1,013
Other problems:

1: Your variable is defined as INT but you are trying to save fractional values. Your table needs to be using Floats to store fractional values, or you need to store integers and re-scale after reading from the table.

2: You are defining the table as a variable and writing values to it in a for( ) loop, but then trying to declare it as a constant.

Constants are fixed values defined in the source code and are generally read-only to the actual program when that is run.

Some compiler + MPU combinations can write to either or both EEPROM and Flash memory, but if you are going to do that it should only be done once, not every time the program is started.

Your best bet is to find how your compiler needs you to define 'true' program-memory constants and include the literal values in the source code. You then have some chance of getting them into the program.
eg. possibly something like (but not to be taken as written):

const float lookup_table[] = {0.012, 0.015, 0.018, (etc...) };
 

Thread Starter

blackmamba

Joined Dec 4, 2009
16
Thanks guys for all your inputs. I am obviously a newbie to C programming for MCUs. I really appreciate the help :)

I am programming the 8bit 12f683 with the HI-TECH C compiler

Now however, I still need some input on this. I am writing a program to take the 8bit result of an ADC and based on this result, determine a delay value in seconds and then trigger a TRIAC.

I decided to use the lookup table to store the delay values and then reference them with the ADC result. However, i need help to get the delay values(256 values) into the lookup table to start out with. Can I put floats in an 8bit MCU sinces the program memory will store 8bit values?

Are there any different ways to do this than the lookup table.

I appreciate all your input.
 

SgtWookie

Joined Jul 17, 2007
22,230
You do realize that if your code had compiled, the first result would've been 8.333... nanoseconds delay, right?

And the delays get shorter from there?

I'm wondering how you're going to manage such short time delays, when it takes a 20MHz uC 50 nanoseconds to execute a single instruction?

Maybe I'm looking at your code wrong?

Anyway, looks to me like you could just take the ADC result, and subtract it from zero. It's an unsigned integer function.
Then use the resulting value as the number of times to execute a fixed delay loop.

I just took this portion of code:
(1/120000000)-(1/120000000*i/255)
and plugged it into a spreadsheet as:
(1/1/255)-(1/1/255*i/255)
to keep the result in a range of 0-255; that formula is equivalent to:
(1/255)-(1/255*i/255)
where "i" was a number from 0 to 255. The result was 255 to 0.

So, if you had an ADC result of, say, 10, subtract that from 0, you'd wind up with 245.
Then execute the delay loop 245 times.
 
Last edited:

Thread Starter

blackmamba

Joined Dec 4, 2009
16
Thank you sir. That helped me so much. I dint think through the code as well as I should have. I re-implemented it with the subtraction method you suggested. the delays should be in ms not nano. My mistake.
thanks much
 
Top