CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Constant array returns wrong value with variable index value
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

Constant array returns wrong value with variable index value
PostPosted: Mon Jan 09, 2012 12:42 pm     Reply with quote

I'm having a problem returning the correct value from an array when accessing it with a variable index. A constant index returns the correct value. They both return the correct value up to the index 1014 and then at array position 1015 only the constant index returns the correct value. See sample code below:
Code:

#include <18F26K22.h>
#device adc=10
#include <stdio.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PROTECT                  //Code protected from reads
#FUSES CPB
#FUSES EBTR
#FUSES NODEBUG

#use delay(clock=16Mhz)
#use rs232(uart1,baud=38400,bits=8,parity=N,stop=1,errors)

const signed int16 CAL_DATA[1288] = 
{
-26477,-26440,-26370,-26267,-26132,-25966,-25767,-25537,-25275,-24982,-24660,-24308,-23924,-23511,-23072,-22604,-22107,-21586,-21038,-20465,-19869,-19246,-18601,
-17936,-17248,-16538,-15812,-15068,-14304,-13523,-12725,-11916,-11092,-10255, -9407, -8550, -7684, -6810, -5927, -5038, -4144, -3246, -2347, -1445,  -541,   361,
  1261,  2162,  3057,  3949,  4833,  5712,  6584,  7446,  8299,  9141,  9970, 10788, 11592, 12378, 13153, 13909, 14650, 15373, 16078, 16761, 17424, 18068, 18686,
 19284, 19857, 20406, 20931, 21428, 21900, 22344, 22763, 23151, 23514, 23845, 24149, 24423, 24666, 24878, 25062, 25212, 25334, 25423, 25481, 25509, 25504, 25468,
 25400, 25302, 25173, 25012, 24822, 24601, 24349, 24066, 23756, 23415, 23046, 22647, 22222, 21773, 21290, 20785, 20254, 19699, 19118, 18514, 17889, 17241, 16571,
 15881, 15173, 14443, 13699, 12938, 12161, 11365, 10559,  9739,  8905,  8061,  7205,  6339,  5467,  4584,  3698,  2805,  1909,  1008,   106,  -797, -1700, -2601,
 -3501, -4398, -5290, -6176, -7058, -7931, -8794, -9649,-10495,-11327,-12147,-12954,-13747,-14524,-15281,-16022,-16743,-17445,-18127,-18786,-19424,-20040,-20631,
    51,   953,  1856,  2755,  3651,  4542,  5425,  6303,  7172,  8033,  8884,  9720, 10548, 11361, 12159, 12942, 13711, 14461, 15197, 15912, 16606, 17284, 17941,
 18572, 19182, 19771, 20333, 20869, 21381, 21869, 22330, 22756, 23162, 23538, 23885, 24202, 24491, 24746, 24975, 25171, 25337, 25471, 25574, 25646, 25686, 25694,
 25671, 25616, 25531, 25413, 25263, 25084, 24873, 24631, 24361, 24059, 23728, 23368, 22980, 22561, 22117, 21645, 21147, 20623, 20074, 19500, 18902, 18281, 17639,
 16974, 16287, 15583, 14858, 14116, 13358, 12582, 11792, 10985, 10167,  9334,  8492,  7636,  6772,  5898,  5018,  4130,  3238,  2340,  1440,   537,  -366, -1271,
 -2175, -3076, -3973, -4868, -5757, -6639, -7514, -8381, -9239,-10086,-10921,-11746,-12553,-13348,-14130,-14894,-15637,-16361,-17069,-17756,-18419,-19062,-19684,
-20279,-20850,-21399,-21920,-22412,-22880,-23323,-23733,-24114,-24468,-24792,-25085,-25349,-25581,-25783,-25949,-26088,-26192,-26266,-26307,-26317,-26293,-26236,
-26149,-26028,-25878,-25692,-25480,-25233,-24954,-24647,-24310,-23944,-23547,-23122,-22670,-22191,-21681,-21148,-20590,-20005,-19399,-18767,-18115,-17440,-16745,
-26502,-26465,-26396,-26292,-26157,-25991,-25793,-25563,-25301,-25008,-24685,-24333,-23949,-23538,-23096,-22628,-22133,-21612,-21064,-20490,-19892,-19270,-18627,
-17960,-17272,-16565,-15838,-15093,-14329,-13549,-12753,-11940,-11116,-10279, -9431, -8575, -7707, -6832, -5950, -5061, -4166, -3268, -2368, -1466,  -563,   338,
  1241,  2140,  3035,  3927,  4813,  5691,  6565,  7427,  8280,  9122,  9952, 10770, 11573, 12362, 13136, 13892, 14632, 15356, 16059, 16744, 17408, 18049, 18670,
 19267, 19840, 20390, 20913, 21413, 21885, 22329, 22748, 23137, 23499, 23831, 24134, 24410, 24651, 24864, 25047, 25198, 25320, 25409, 25467, 25493, 25492, 25454,
 25386, 25289, 25157, 24998, 24808, 24587, 24335, 24052, 23742, 23401, 23031, 22635, 22208, 21755, 21276, 20772, 20240, 19685, 19104, 18500, 17874, 17225, 16555,
 15866, 15157, 14428, 13683, 12921, 12145, 11349, 10543,  9722,  8888,  8044,  7187,  6321,  5448,  4565,  3679,  2786,  1889,   989,    86,  -816, -1719, -2620,
 -3521, -4417, -5310, -6197, -7078, -7951, -8815, -9670,-10517,-11347,-12169,-12976,-13769,-14545,-15305,-16045,-16767,-17468,-18150,-18810,-19448,-20063,-20654,
    68,   972,  1875,  2772,  3669,  4559,  5444,  6321,  7191,  8050,  8902,  9740, 10566, 11380, 12180, 12963, 13731, 14482, 15217, 15933, 16628, 17306, 17960,
 18595, 19204, 19791, 20353, 20892, 21403, 21889, 22348, 22780, 23185, 23561, 23907, 24223, 24512, 24770, 24997, 25194, 25361, 25495, 25598, 25669, 25710, 25717,
 25694, 25640, 25554, 25436, 25287, 25107, 24898, 24655, 24385, 24082, 23751, 23391, 23002, 22585, 22141, 21669, 21170, 20646, 20097, 19523, 18924, 18303, 17661,
 16995, 16310, 15604, 14880, 14139, 13379, 12603, 11813, 11008, 10188,  9355,  8512,  7658,  6791,  5917,  5037,  4149,  3256,  2359,  1457,   554,  -347, -1253,
 -2157, -3058, -3957, -4853, -5740, -6621, -7497, -8364, -9222,-10069,-10907,-11729,-12540,-13336,-14114,-14877,-15622,-16348,-17054,-17742,-18405,-19048,-19670,
-20265,-20838,-21385,-21905,-22401,-22868,-23311,-23721,-24103,-24457,-24780,-25075,-25338,-25569,-25772,-25939,-26078,-26183,-26256,-26297,-26306,-26283,-26226,
-26138,-26019,-25866,-25683,-25467,-25222,-24944,-24637,-24300,-23931,-23537,-23111,-22657,-22178,-21671,-21137,-20578,-19995,-19387,-18757,-18104,-17427,-16732,
-26514,-26477,-26406,-26305,-26171,-26004,-25805,-25575,-25313,-25020,-24698,-24345,-23963,-23551,-23110,-22642,-22146,-21625,-21076,-20502,-19906,-19283,-18640,
-17973,-17285,-16578,-15850,-15106,-14342,-13561,-12763,-11953,-11128,-10292, -9444, -8587, -7719, -6844, -5961, -5072, -4179, -3280, -2381, -1479,  -575,   328,
  1228,  2129,  3025,  3915,  4801,  5680,  6552,  7414,  8269,  9110,  9940, 10758, 11562, 12350, 13124, 13881, 14622, 15345, 16049, 16733, 17397, 18040, 18661,
 19256, 19831, 20380, 20906, 21404, 21875, 22320, 22736, 23126, 23488, 23820, 24125, 24399, 24641, 24856, 25036, 25189, 25310, 25400, 25458, 25485, 25480, 25445,
 25377, 25280, 25149, 24989, 24798, 24577, 24326, 24043, 23733, 23391, 23023, 22623, 22198, 21747, 21267, 20761, 20231, 19675, 19095, 18490, 17865, 17216, 16546,
 15856, 15147, 14418, 13675, 12911, 12134, 11339, 10533,  9712,  8877,  8035,  7178,  6312,  5441,  4557,  3668,  2778,  1880,   980,    78,  -825, -1729, -2629,
 -3530, -4427, -5320, -6207, -7088, -7961, -8825, -9680,-10526,-11359,-12178,-12986,-13778,-14556,-15314,-16054,-16776,-17479,-18160,-18821,-19458,-20073,-20665,
    86,   989,  1892,  2791,  3686,  4577,  5461,  6341,  7210,  8070,  8920,  9759, 10586, 11400, 12198, 12982, 13749, 14501, 15236, 15953, 16648, 17325, 17979,
 18613, 19225, 19810, 20372, 20911, 21423, 21911, 22371, 22800, 23205, 23581, 23928, 24245, 24533, 24792, 25019, 25217, 25381, 25516, 25618, 25691, 25730, 25739,
 25716, 25663, 25576, 25457, 25308, 25130, 24918, 24676, 24407, 24105, 23773, 23413, 23024, 22606, 22162, 21690, 21192, 20667, 20118, 19544, 18946, 18326, 17680,
 17017, 16331, 15626, 14903, 14160, 13401, 12624, 11833, 11027, 10209,  9376,  8532,  7678,  6811,  5940,  5057,  4170,  3277,  2381,  1479,   576,  -328, -1233,
 -2137, -3038, -3938, -4832, -5721, -6602, -7478, -8345, -9202,-10051,-10885,-11711,-12520,-13316,-14095,-14858,-15602,-16328,-17035,-17722,-18386,-19030,-19650,
-20246,-20818,-21366,-21887,-22383,-22849,-23291,-23702,-24085,-24439,-24761,-25054,-25319,-25549,-25752,-25920,-26058,-26163,-26237,-26277,-26287,-26265,-26207,
-26120,-26000,-25847,-25665,-25448,-25202,-24926,-24618,-24281,-23914,-23517,-23093,-22639,-22160,-21652,-21119,-20559,-19976,-19368,-18737,-18083,-17409,-16713,
-26517,-26472,-26402,-26301,-26167,-26000,-25801,-25572,-25310,-25016,-24694,-24341,-23959,-23547,-23107,-22638,-22143,-21620,-21073,-20499,-19901,-19280,-18636,
-17969,-17282,-16575,-15846,-15101,-14338,-13558,-12759,-11948,-11126,-10287, -9441, -8583, -7715, -6840, -5957, -5070, -4174, -3277, -2378, -1475,  -570,   330,
  1232,  2133,  3029,  3919,  4805,  5685,  6556,  7419,  8273,  9115,  9944, 10763, 11567, 12355, 13129, 13886, 14627, 15350, 16053, 16738, 17401, 18044, 18664,
 19261, 19835, 20385, 20909, 21407, 21879, 22325, 22742, 23132, 23494, 23825, 24130, 24403, 24646, 24860, 25043, 25194, 25315, 25404, 25463, 25491, 25486, 25450,
 25383, 25285, 25155, 24995, 24804, 24582, 24331, 24049, 23738, 23397, 23028, 22630, 22205, 21753, 21271, 20768, 20237, 19680, 19101, 18496, 17871, 17223, 16552,
 15863, 15153, 14425, 13680, 12918, 12139, 11345, 10541,  9718,  8884,  8040,  7183,  6316,  5445,  4564,  3675,  2781,  1886,   986,    82,  -820, -1722, -2625,
 -3526, -4422, -5314, -6201, -7083, -7955, -8819, -9674,-10521,-11353,-12174,-12982,-13774,-14551,-15309,-16050,-16774,-17474,-18156,-18815,-19455,-20068,-20660,
   111,  1022,  1925,  2824,  3719,  4610,  5495,  6372,  7242,  8103,  8952,  9791, 10618, 11431, 12230, 13014, 13781, 14532, 15267, 15983, 16680, 17357, 18011,
 18645, 19255, 19842, 20404, 20943, 21455, 21940, 22402, 22833, 23236, 23613, 23960, 24277, 24564, 24823, 25051, 25248, 25413, 25548, 25651, 25722, 25763, 25769,
 25747, 25693, 25607, 25489, 25341, 25160, 24951, 24708, 24438, 24136, 23804, 23445, 23057, 22639, 22195, 21723, 21224, 20701, 20151, 19577, 18980, 18359, 17715,
 17050, 16365, 15660, 14934, 14192, 13432, 12658, 11867, 11063, 10242,  9409,  8567,  7711,  6846,  5972,  5091,  4204,  3310,  2412,  1513,   609,  -294, -1198,
 -2102, -3003, -3903, -4796, -5686, -6568, -7443, -8312, -9168,-10016,-10852,-11675,-12486,-13281,-14062,-14823,-15567,-16295,-17000,-17688,-18352,-18994,-19616,
-20212,-20785,-21333,-21854,-22349,-22817,-23258,-23667,-24051,-24407,-24728,-25022,-25286,-25516,-25717,-25886,-26024,-26130,-26202,-26244,-26254,-26228,-26174,
-26086,-25966,-25814,-25629,-25416,-25168,-24892,-24585,-24248,-23881,-23484,-23059,-22607,-22128,-21620,-21086,-20529,-19943,-19336,-18704,-18051,-17377,-16682
};

void Initialise_uC()
{
   setup_oscillator(OSC_16MHZ|OSC_INTRC|OSC_PLL_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(T0_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
}

void main()
{

   Initialise_uC();
   while(1)
   {
      int16 A=1015;
      printf("%ld\r",CAL_DATA[A]);
      printf("%ld\r",CAL_DATA[1015]);
      delay_ms(30);
   }
}


Can anyone explain why this is, and offer a solution or a work around.

Compiler version is PCWH 4.119
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 09, 2012 1:11 pm     Reply with quote

I tested your code with vs. 4.119 in MPLAB simulator and I got this:
Quote:

3919
3919
3919
3919
3919
3919



Quote:
void main()
{

Initialise_uC();
while(1)
{
int16 A=1015;
printf("%ld\r",CAL_DATA[A]);
printf("%ld\r",CAL_DATA[1015]);
delay_ms(30);
}
}

Putting a variable declaration in the middle of code is not supported.
It's possible that this is causing the problem on your machine. Move
the declaration of 'A' to the start of main().
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Mon Jan 09, 2012 1:37 pm     Reply with quote

Thanks for your reply. I moved the variable declaration, this has made the two values consistently the same, but still wrong when the array index goes over 1014.

Code:
void main()
{
   int16 A=1014;
   Initialise_uC();
   while(1)
   {
      printf("%ld\r",CAL_DATA[A]);
      printf("%ld\r",CAL_DATA[1014]);
      delay_ms(30);
   }
}



prints as expected :
Code:
3029
3029


but
Code:
void main()
{
   int16 A=1015;
   Initialise_uC();
   while(1)
   {
      printf("%ld\r",CAL_DATA[A]);
      printf("%ld\r",CAL_DATA[1015]);
      delay_ms(30);
   }
}

prints
Code:
3840
3840

instead of the correct value for this array position which is 3919. From my testing all values returned above array index 1014 are incorrect. Have you any idea about the reason for this?
Ttelmah



Joined: 11 Mar 2010
Posts: 19457

View user's profile Send private message

PostPosted: Mon Jan 09, 2012 3:18 pm     Reply with quote

Wrote a 'bodge round' for the read:
Code:

signed int16 get_val(int16 index){
   signed int16 intretval;
   index*=2;
   index+=label_address(CAL_DATA);
   read_program_memory(index,&intretval,2);
   return(intretval);
}

and it works.
It seems to be a couple of locations after the 0x800 page boundary (where you might expect a problem).

Best Wishes
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Mon Jan 09, 2012 5:40 pm     Reply with quote

Thanks for the fix. I had just been working on a similar fix myself, but haven't had time to test either yet.

The thing I don't understand is that for this function to work, the bytes of data must be stored consecutively in program memory. If they are stored consecutively then why is there an error retrieving the values directly from the array using the array index. Surely the compiled lookup code for this is similar to your function anyway, adding the array index*2 to the array memory start location.

In my real program, the array of calibration data is 3 dimensional, and much larger than this test program with about 5000 data points. As such I can't proceed with confidence at the moment that the correct data will be returned, which means the device might not give the correct result.
Ttelmah



Joined: 11 Mar 2010
Posts: 19457

View user's profile Send private message

PostPosted: Tue Jan 10, 2012 3:35 am     Reply with quote

Since it comes right at the page boundary, I'd guess that the code is just getting the 'carry' wrong when it reaches the boundary. The array access code is direct assembler, and probably optimised to be a little faster. Unfortunately, somewhere in this a bug has been introduced when dealing with large arrays that cross a page boundary.

Best Wishes
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Tue Jan 10, 2012 4:35 pm     Reply with quote

Is there anyway of forcing the array into the start of a page, so it doesn't have to cross the boundary?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 10, 2012 4:48 pm     Reply with quote

You can use #locate to set the starting RAM address of an array. Example:
http://www.ccsinfo.com/forum/viewtopic.php?t=19945&start=5
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Tue Jan 10, 2012 4:55 pm     Reply with quote

Can you use this for constant arrays stored in program memory? This array is too big for the RAm available on the PIC18f26k22.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jan 10, 2012 5:44 pm     Reply with quote

specifying the array as CONST should force it into program memory.
i recall my days with 8086 code where it was best practice to force an array
to start on an even byte. - simply rearranging WHERE in your program linear order - the array is encountered by the compiler might be of help.
Or so i thought until i tried THIS:
( warning do not TRY this code as i wrecked your table to make its location more visible in HEX !!!)

in the HEX file you can see a long line of hex 5555555555
where the printf "U" statement is located )

the changes in the table , 174876 repeated produce a long line of 44444444 so you can SEE what address the table is dumped to
and no matter WHERE the table is encountered in linear fashion -
the compiler blindly stuffs it at the head of the program memory

Code:

#include <18F26K22.h>
#device adc=10
#include <stdio.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PROTECT                  //Code protected from reads
#FUSES CPB
#FUSES EBTR
#FUSES NODEBUG

#use delay(clock=16Mhz)
#use rs232(uart1,baud=38400,bits=8,parity=N,stop=1,errors)


void Initialise_uC()
{
   setup_oscillator(OSC_16MHZ|OSC_INTRC|OSC_PLL_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(T0_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   printf("UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU");
}

const signed int16 CAL_DATA[1288] =
{
 17476, 17476, 17476, 17476, 17476, 17476, 25767,-25537,-25275,-24982,-24660,-24308,-23924,-23511,-23072,-22604,-22107,-21586,-21038,-20465,-19869,-19246,-18601,
-17936,-17248,-16538,-15812,-15068,-14304,-13523,-12725,-11916,-11092,-10255, -9407, -8550, -7684, -6810, -5927, -5038, -4144, -3246, -2347, -1445,  -541,   361,
  1261,  2162,  3057,  3949,  4833,  5712,  6584,  7446,  8299,  9141,  9970, 10788, 11592, 12378, 13153, 13909, 14650, 15373, 16078, 16761, 17424, 18068, 18686,
 19284, 19857, 20406, 20931, 21428, 21900, 22344, 22763, 23151, 23514, 23845, 24149, 24423, 24666, 24878, 25062, 25212, 25334, 25423, 25481, 25509, 25504, 25468,
 25400, 25302, 25173, 25012, 24822, 24601, 24349, 24066, 23756, 23415, 23046, 22647, 22222, 21773, 21290, 20785, 20254, 19699, 19118, 18514, 17889, 17241, 16571,
 15881, 15173, 14443, 13699, 12938, 12161, 11365, 10559,  9739,  8905,  8061,  7205,  6339,  5467,  4584,  3698,  2805,  1909,  1008,   106,  -797, -1700, -2601,
 -3501, -4398, -5290, -6176, -7058, -7931, -8794, -9649,-10495,-11327,-12147,-12954,-13747,-14524,-15281,-16022,-16743,-17445,-18127,-18786,-19424,-20040,-20631,
    51,   953,  1856,  2755,  3651,  4542,  5425,  6303,  7172,  8033,  8884,  9720, 10548, 11361, 12159, 12942, 13711, 14461, 15197, 15912, 16606, 17284, 17941,
 18572, 19182, 19771, 20333, 20869, 21381, 21869, 22330, 22756, 23162, 23538, 23885, 24202, 24491, 24746, 24975, 25171, 25337, 25471, 25574, 25646, 25686, 25694,
 25671, 25616, 25531, 25413, 25263, 25084, 24873, 24631, 24361, 24059, 23728, 23368, 22980, 22561, 22117, 21645, 21147, 20623, 20074, 19500, 18902, 18281, 17639,
 16974, 16287, 15583, 14858, 14116, 13358, 12582, 11792, 10985, 10167,  9334,  8492,  7636,  6772,  5898,  5018,  4130,  3238,  2340,  1440,   537,  -366, -1271,
 -2175, -3076, -3973, -4868, -5757, -6639, -7514, -8381, -9239,-10086,-10921,-11746,-12553,-13348,-14130,-14894,-15637,-16361,-17069,-17756,-18419,-19062,-19684,
-20279,-20850,-21399,-21920,-22412,-22880,-23323,-23733,-24114,-24468,-24792,-25085,-25349,-25581,-25783,-25949,-26088,-26192,-26266,-26307,-26317,-26293,-26236,
-26149,-26028,-25878,-25692,-25480,-25233,-24954,-24647,-24310,-23944,-23547,-23122,-22670,-22191,-21681,-21148,-20590,-20005,-19399,-18767,-18115,-17440,-16745,
-26502,-26465,-26396,-26292,-26157,-25991,-25793,-25563,-25301,-25008,-24685,-24333,-23949,-23538,-23096,-22628,-22133,-21612,-21064,-20490,-19892,-19270,-18627,
-17960,-17272,-16565,-15838,-15093,-14329,-13549,-12753,-11940,-11116,-10279, -9431, -8575, -7707, -6832, -5950, -5061, -4166, -3268, -2368, -1466,  -563,   338,
  1241,  2140,  3035,  3927,  4813,  5691,  6565,  7427,  8280,  9122,  9952, 10770, 11573, 12362, 13136, 13892, 14632, 15356, 16059, 16744, 17408, 18049, 18670,
 19267, 19840, 20390, 20913, 21413, 21885, 22329, 22748, 23137, 23499, 23831, 24134, 24410, 24651, 24864, 25047, 25198, 25320, 25409, 25467, 25493, 25492, 25454,
 25386, 25289, 25157, 24998, 24808, 24587, 24335, 24052, 23742, 23401, 23031, 22635, 22208, 21755, 21276, 20772, 20240, 19685, 19104, 18500, 17874, 17225, 16555,
 15866, 15157, 14428, 13683, 12921, 12145, 11349, 10543,  9722,  8888,  8044,  7187,  6321,  5448,  4565,  3679,  2786,  1889,   989,    86,  -816, -1719, -2620,
 -3521, -4417, -5310, -6197, -7078, -7951, -8815, -9670,-10517,-11347,-12169,-12976,-13769,-14545,-15305,-16045,-16767,-17468,-18150,-18810,-19448,-20063,-20654,
    68,   972,  1875,  2772,  3669,  4559,  5444,  6321,  7191,  8050,  8902,  9740, 10566, 11380, 12180, 12963, 13731, 14482, 15217, 15933, 16628, 17306, 17960,
 18595, 19204, 19791, 20353, 20892, 21403, 21889, 22348, 22780, 23185, 23561, 23907, 24223, 24512, 24770, 24997, 25194, 25361, 25495, 25598, 25669, 25710, 25717,
 25694, 25640, 25554, 25436, 25287, 25107, 24898, 24655, 24385, 24082, 23751, 23391, 23002, 22585, 22141, 21669, 21170, 20646, 20097, 19523, 18924, 18303, 17661,
 16995, 16310, 15604, 14880, 14139, 13379, 12603, 11813, 11008, 10188,  9355,  8512,  7658,  6791,  5917,  5037,  4149,  3256,  2359,  1457,   554,  -347, -1253,
 -2157, -3058, -3957, -4853, -5740, -6621, -7497, -8364, -9222,-10069,-10907,-11729,-12540,-13336,-14114,-14877,-15622,-16348,-17054,-17742,-18405,-19048,-19670,
-20265,-20838,-21385,-21905,-22401,-22868,-23311,-23721,-24103,-24457,-24780,-25075,-25338,-25569,-25772,-25939,-26078,-26183,-26256,-26297,-26306,-26283,-26226,
-26138,-26019,-25866,-25683,-25467,-25222,-24944,-24637,-24300,-23931,-23537,-23111,-22657,-22178,-21671,-21137,-20578,-19995,-19387,-18757,-18104,-17427,-16732,
-26514,-26477,-26406,-26305,-26171,-26004,-25805,-25575,-25313,-25020,-24698,-24345,-23963,-23551,-23110,-22642,-22146,-21625,-21076,-20502,-19906,-19283,-18640,
-17973,-17285,-16578,-15850,-15106,-14342,-13561,-12763,-11953,-11128,-10292, -9444, -8587, -7719, -6844, -5961, -5072, -4179, -3280, -2381, -1479,  -575,   328,
  1228,  2129,  3025,  3915,  4801,  5680,  6552,  7414,  8269,  9110,  9940, 10758, 11562, 12350, 13124, 13881, 14622, 15345, 16049, 16733, 17397, 18040, 18661,
 19256, 19831, 20380, 20906, 21404, 21875, 22320, 22736, 23126, 23488, 23820, 24125, 24399, 24641, 24856, 25036, 25189, 25310, 25400, 25458, 25485, 25480, 25445,
 25377, 25280, 25149, 24989, 24798, 24577, 24326, 24043, 23733, 23391, 23023, 22623, 22198, 21747, 21267, 20761, 20231, 19675, 19095, 18490, 17865, 17216, 16546,
 15856, 15147, 14418, 13675, 12911, 12134, 11339, 10533,  9712,  8877,  8035,  7178,  6312,  5441,  4557,  3668,  2778,  1880,   980,    78,  -825, -1729, -2629,
 -3530, -4427, -5320, -6207, -7088, -7961, -8825, -9680,-10526,-11359,-12178,-12986,-13778,-14556,-15314,-16054,-16776,-17479,-18160,-18821,-19458,-20073,-20665,
    86,   989,  1892,  2791,  3686,  4577,  5461,  6341,  7210,  8070,  8920,  9759, 10586, 11400, 12198, 12982, 13749, 14501, 15236, 15953, 16648, 17325, 17979,
 18613, 19225, 19810, 20372, 20911, 21423, 21911, 22371, 22800, 23205, 23581, 23928, 24245, 24533, 24792, 25019, 25217, 25381, 25516, 25618, 25691, 25730, 25739,
 25716, 25663, 25576, 25457, 25308, 25130, 24918, 24676, 24407, 24105, 23773, 23413, 23024, 22606, 22162, 21690, 21192, 20667, 20118, 19544, 18946, 18326, 17680,
 17017, 16331, 15626, 14903, 14160, 13401, 12624, 11833, 11027, 10209,  9376,  8532,  7678,  6811,  5940,  5057,  4170,  3277,  2381,  1479,   576,  -328, -1233,
 -2137, -3038, -3938, -4832, -5721, -6602, -7478, -8345, -9202,-10051,-10885,-11711,-12520,-13316,-14095,-14858,-15602,-16328,-17035,-17722,-18386,-19030,-19650,
-20246,-20818,-21366,-21887,-22383,-22849,-23291,-23702,-24085,-24439,-24761,-25054,-25319,-25549,-25752,-25920,-26058,-26163,-26237,-26277,-26287,-26265,-26207,
-26120,-26000,-25847,-25665,-25448,-25202,-24926,-24618,-24281,-23914,-23517,-23093,-22639,-22160,-21652,-21119,-20559,-19976,-19368,-18737,-18083,-17409,-16713,
-26517,-26472,-26402,-26301,-26167,-26000,-25801,-25572,-25310,-25016,-24694,-24341,-23959,-23547,-23107,-22638,-22143,-21620,-21073,-20499,-19901,-19280,-18636,
-17969,-17282,-16575,-15846,-15101,-14338,-13558,-12759,-11948,-11126,-10287, -9441, -8583, -7715, -6840, -5957, -5070, -4174, -3277, -2378, -1475,  -570,   330,
  1232,  2133,  3029,  3919,  4805,  5685,  6556,  7419,  8273,  9115,  9944, 10763, 11567, 12355, 13129, 13886, 14627, 15350, 16053, 16738, 17401, 18044, 18664,
 19261, 19835, 20385, 20909, 21407, 21879, 22325, 22742, 23132, 23494, 23825, 24130, 24403, 24646, 24860, 25043, 25194, 25315, 25404, 25463, 25491, 25486, 25450,
 25383, 25285, 25155, 24995, 24804, 24582, 24331, 24049, 23738, 23397, 23028, 22630, 22205, 21753, 21271, 20768, 20237, 19680, 19101, 18496, 17871, 17223, 16552,
 15863, 15153, 14425, 13680, 12918, 12139, 11345, 10541,  9718,  8884,  8040,  7183,  6316,  5445,  4564,  3675,  2781,  1886,   986,    82,  -820, -1722, -2625,
 -3526, -4422, -5314, -6201, -7083, -7955, -8819, -9674,-10521,-11353,-12174,-12982,-13774,-14551,-15309,-16050,-16774,-17474,-18156,-18815,-19455,-20068,-20660,
   111,  1022,  1925,  2824,  3719,  4610,  5495,  6372,  7242,  8103,  8952,  9791, 10618, 11431, 12230, 13014, 13781, 14532, 15267, 15983, 16680, 17357, 18011,
 18645, 19255, 19842, 20404, 20943, 21455, 21940, 22402, 22833, 23236, 23613, 23960, 24277, 24564, 24823, 25051, 25248, 25413, 25548, 25651, 25722, 25763, 25769,
 25747, 25693, 25607, 25489, 25341, 25160, 24951, 24708, 24438, 24136, 23804, 23445, 23057, 22639, 22195, 21723, 21224, 20701, 20151, 19577, 18980, 18359, 17715,
 17050, 16365, 15660, 14934, 14192, 13432, 12658, 11867, 11063, 10242,  9409,  8567,  7711,  6846,  5972,  5091,  4204,  3310,  2412,  1513,   609,  -294, -1198,
 -2102, -3003, -3903, -4796, -5686, -6568, -7443, -8312, -9168,-10016,-10852,-11675,-12486,-13281,-14062,-14823,-15567,-16295,-17000,-17688,-18352,-18994,-19616,
-20212,-20785,-21333,-21854,-22349,-22817,-23258,-23667,-24051,-24407,-24728,-25022,-25286,-25516,-25717,-25886,-26024,-26130,-26202,-26244,-26254,-26228,-26174,
-26086,-25966,-25814,-25629,-25416,-25168,-24892,-24585,-24248,-23881,-23484,-23059,-22607,-22128,-21620,-21086, 17476, 17476, 17476, 17476, 17476, 17476, 17476,
};

void main()
{

   Initialise_uC();
   while(1)
   {
      int16 A=1015;
      printf("%ld\r",CAL_DATA[A]);
      printf("%ld\r",CAL_DATA[1015]);
      delay_ms(30);
   }
}


the next thing i would try is experimenting with the
#ORG directive, placed just ahead of your table


Last edited by asmboy on Tue Jan 10, 2012 6:01 pm; edited 2 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 10, 2012 5:50 pm     Reply with quote

You can use #org to place an array in ROM. Example:
Quote:

#org 0x1000, 0x1FFF
const signed int16 CAL_DATA[1288] =
{

I made the range larger than it really needs to be. That's just to make
it easy for me, so I don't have to calculate the exact amount needed.

Note that #org won't put the array data at the exact address that you
specify (for example, at 0x1000 above). The data will be a few bytes
after that. That's because the compiler inserts a small routine to access
the array at the start of the block. You can see the code and the data
if you go to the View / Program Memory menu in MPLAB and scroll down
to address 0x1000.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jan 10, 2012 6:07 pm     Reply with quote

i just tried this and it compiled A-OK

Code:

#org 0x2000 ,0x3A10
const signed int16 CAL_DATA[1288]


BTW: the preamble takes EXACTLY 0x10 bytes of space

so if you want the first element DEAD on the boundary
try #ORG 0x1FF0, 0x3A00

i DONT have that chip to try it out in hardware though ;-(((
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Wed Jan 11, 2012 12:10 pm     Reply with quote

After some more testing it seems that any constant array placed after memory location 0x0800 will not return the correct value. It's not so much the transition past 7FF, but just anything beyond this location. The data is put into memory correctly at compile time, but when the address is higher than 7FF It returns the LSB correctly but the MSB is always zero. This is even true with a small array as per the code below:

Code:
#include <18F26K22.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PROTECT                  //Code protected from reads
#FUSES CPB
#FUSES EBTR
#FUSES NODEBUG

#use delay(clock=16Mhz)
#use rs232(uart1,baud=38400,bits=8,parity=N,stop=1,errors)

#ORG 0x0800, 0x08FF
const signed int16 CAL_DATA[4] = {1000,1001,1002,1003};

#ORG default

void Initialise_uC()
{
   setup_oscillator(OSC_16MHZ|OSC_INTRC|OSC_PLL_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(T0_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
}

void main()
{
   int16 A;
   Initialise_uC();
   while(1)
   {
      for (a=0;a<4;a++)
      {
         printf("A=%lu\tCD=%ld\r",A,CAL_DATA[A]);
         delay_ms(30);
      }
   }
}


If the array index is a constant it works ok, if it is a variable then it doesn't work. Needless to say, most code will require that arrays can be accessed with variable indexes.

I'm not sure what the code is before the array data that enables the lookup but it's not working properly. I will use Ttelmah's function instead which from initial testing seems to work.

It never fails to amaze me how this compiler gets some very basic things wrong. Of all the complexity involved in my project, the PIC and the compiler, and the thing it should fall over on is a simple lookup table!

So for now, I think it would be reasonable to say thay all constant arrays should be located at the beginning of program memory (after the reset and interrupt vectors) and they should not extend past memory location 0x7FF. That gives you about 2Kb of ROM for your constant arrays.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Jan 11, 2012 12:22 pm     Reply with quote

i have a currently deliverable application using an 18F4620 that has 512 elements, to ten decimal places - saved as a const FLOAT table - a GRAND old memory hog - that has never done anything but perform as expected.

it was assembled under 4.085

this sort of problem is one more of the main reasons i hesitate to blindly
use a new version of CCS - on a complicated program.
at least not until i have done a lot of testing with old things that work already.
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Wed Jan 11, 2012 12:23 pm     Reply with quote

Actually scratch some of that, Ttelmah the function also doesn't work on data stored after memory location 0x7FF.

Code:
#include <18F26K22.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PROTECT                  //Code protected from reads
#FUSES CPB
#FUSES EBTR
#FUSES NODEBUG

#use delay(clock=16Mhz)
#use rs232(uart1,baud=38400,bits=8,parity=N,stop=1,errors)

#ORG 0x0800, 0x08FF
const signed int16 CAL_DATA[4] = {1000,1001,1002,1003};

#ORG default

void Initialise_uC()
{
   setup_oscillator(OSC_16MHZ|OSC_INTRC|OSC_PLL_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(T0_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
}

signed int16 get_val(int16 index)
{
   signed int16 intretval;
   index*=2;
   index+=label_address(CAL_DATA);
   read_program_memory(index,&intretval,2);
   return(intretval);
}

void main()
{
   int16 A;
   signed int16 B;
   Initialise_uC();
   while(1)
   {
      for (A=0;A<4;A++)
      {
         B=get_val(A);
         printf("A=%lu\tCD=%ld\r",A,B);
         delay_ms(30);
      }
   }
}

returns all zeros. If I move the #ORG so the data is stored lower than 0x800 then it works fine.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group