I think about the following problem: I want to program a microcontroller (for example, type AVR mega) with a program that uses some kind of lookup tables.
The first attempt would be to find the table in a separate file and create it using any other language / script program / .... In this case, when creating the necessary source files for C.
Now I was thinking of using a preprocessor and compiler to handle things. I tried to implement this using a sine value table (as an example):
#include <avr/io.h> #include <math.h> #define S1(i,n) ((uint8_t) sin(M_PI*(i)/n*255)) #define S4(i,n) S1(i,n), S1(i+1,n), S1(i+2,n), S1(i+3,n) uint8_t lut[] = {S4(0,4)}; void main() { uint8_t val, i; for(i=0; i<4; i++) { val = lut[i]; } }
If I compile this code, I get warnings about the sin function. Further in the assembly there is nothing in the .data section. If I just delete sin in the third line, I will get the data in the assembly. Obviously, all information is available at compile time.
Can you tell me if there is a way to achieve what I intend: the compiler calculates as many values as possible in offline mode? Or is this the best way to use an external script / program / ... to calculate the entries in the table and add them to a separate file, which will simply be #include d?
source share