My program is going off into the weeds, and in an odd fashion. I hope someone can spot my error. I have a table of sine values stored as a const in flash. The table is 2048 16-bit entries. Here is the routine that uses the table:
unsigned int ConvertToSin(unsigned long thisPhase) { unsigned int quarterPhase; unsigned int thisQuarter; unsigned int thisSample; unsigned int dummySample; quarterPhase = (thisPhase>>19) & 0x7FF; //bottom 11 bits please thisQuarter = thisPhase>>30; //top two bits is the quarter thisQuarter = thisQuarter & 0x03; //limit to 2 bits switch(thisQuarter) { case 0: thisSample=sine_table[quarterPhase]; break; case 1: thisSample=sine_table[(0x7FF-quarterPhase)]; break; case 2: thisSample=0xFFF-sine_table[quarterPhase]; break; case 3: thisSample=0xFFF-sine_table[(0x7FF-quarterPhase)]; break; default: thisSample = 0; } return thisSample; //return 0; }
Here is the sine table declaration:
const __flash uint16_t sine_table[] = { 0x0800, 0x0801, 0x0803, 0x0804, .. .. 0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF }; // 2048 entries
When this routine gets executed my target stops running, and when I pause the debugger the program counter is in the sine table!
If I remove the references to quarterPhase in the sine_table reads and use constants it works. I don't see how running off the end of an array would cause what I am seeing.
I have tried using PROGMEM instead of __flash with the same results. Also I have another table that is the same size but is 1K of long words and it works just fine.
Any help would be appreciated.
--tr