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 CCS Technical Support

array problem with ccs v3.212

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

array problem with ccs v3.212
PostPosted: Mon Jul 16, 2007 3:26 am     Reply with quote

Hello i am facing an array read error with my project.First of all program executes correctly on the Mplab simulator.When i put the Pic on board the error appears.Here is the problem.
I am using arrays with values to dusplay temperatutes for 2 kinds of sensors. I read the value with the A2D converter and i adapt the result to my arrays to get the correct temperature.

The program reads correct the values of the array till one point then it reads
zeros and again after a point it reads correct the values of the array after it
bypassed the previous values as zero's.

I have arrays for one type of sensor and arrays for another type of sensor.

If i dont use the array's of one sensor type in my program then the array re ad for the other type of sensor is correct.

I have tried to split the arrays but the problem still occurs.
"Temps" arrays is for the one type sensor and a1 and a2 arrays for the another.

As in the simulator the program works i can't find any answer to my problem.I am thinking of a memery allocation or overlap problem.

Here is my program:
Function GetTable takes the value from the sensor through A2d converter then peaks the value from the proper array and continues with the execution of the program display the temperatures on the lcd e.t.c.


#include "18F458.h"
//#include "18F4525.h"
//#include "18F4620.h"
#use delay(clock=4000000)
#fuses XT,noprotect,nowdt,NOBROWNOUT,NOLVP,put, nowrt ,NOSTVREN,WRT
#fuses CPD,WRTB,WRTC,EBTR,PROTECT//NOOSCSEN


#use rs232(baud=9600, parity=N,xmit=PIN_C6 ,rcv=PIN_C7,bits=8)

#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)


long a2d(void);

void init();
long GetTable(long,char);
void Takeaction(void);

unsigned char const a1[183]={0,1,1,2,3,3,4,5,5,6,6,7,8,8,9,10,10,11,12,12,13,14,14,15,16,16,17,17,18,19,19,20,21,21,22,22,23,24,25,25,26,27,27,
28,29,29,30,31,31,32,33,33,34,35,35,36,37,37,38,39,39,40,41,41,42,43,43,44,45,45,46,47,48,48,49,50,50,51,52,52,
53,54,54,55,56,56,57,58,59,59,60,61,61,62,63,63,64,65,66,66,67,68,68,69,70,71,71,72,73,73,74,75,75,76,77,78,78,
79,80,80,81,82,83,84,84,85,86,86,87,88,88,89,90,91,91,92,93,94,94,95,96,97,97,98,98,99,100,101,102,102,103,104,
105,105,106,107,108,108,109,110,111,111,112,113,114,114,115,116,117,117,118,119,120,121,121,122,123,124,124,
125,126,127,127};


signed char const Temps[96]={-40,-39,-38,-37,-36,-35,-34,-33,-32,-31,-30,-29,-28,-27,-26,-25,-24,
-23,-22,-21,-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,
5,6,7,8,9,10,11,12,13,14,14,15,16,17,18,19,20,21,22,22,23,24,25,26,27,28,29,29,30,31,32,33,34,35,
35,36,37,38,39,40,41,41,42,43,44,45,46,47,47,48,49};



char const Temps3[9]={50,51,52,52,53,54,55,56,57};



char const Temps2[109]={57,58,59,60,61,62,
62,63,64,65,66,66,67,68,69,70,71,71,72,73,74,75,75,76,77,78,79,80,80,81,82,83,84,84,85,86,87,88,
89,90,91,92,92,93,94,95,96,97,98,99,100,101,101,102,103,104,105,105,106,107,108,109,109,
110,111,112,113,113,114,115,116,117,118,118,119,120,121,122,123,123,124,125,126,127,128,129,130,
130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,150};



long const a2[199]={128,129,130,131,131,132,133,134,134,135,136,137,138,138,139,140,141,141,142,143,144,145,145,
146,147,148,149,149,150,151,152,153,153,154,155,156,157,157,158,159,160,161,161,162,163,164,165,165,166,167,
168,169,170,170,171,172,173,174,175,175,176,177,178,179,179,180,181,182,183,184,184,185,186,187,188,189,190,
190,191,192,193,194,195,195,196,197,198,199,200,201,201,202,203,204,205,206,207,207,208,209,210,211,212,213,
214,214,215,216,217,218,219,220,221,221,222,223,224,225,226,227,228,229,229,230,231,232,233,234,235,236,237,
238,238,239,240,241,242,243,244,245,246,247,247,248,249,250,251,252,253,254,255,256,257,258,259,259,260,261,262,263,264,265,266,267,
268,269,270,271,272,273,274,274,275,276,277,278,279,280,281,282,283,284,285,
286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301};



main()
{
.........


}


long GetTable(long s,char t)
{
signed long q,r;

//disable_interrupts(global);

q=s;

if(t==2) //t=1 KTY81-122 sensor t=2 PT100 sensor
goto Pt1000;



if(q>319) //if out of temperature limits from the upper
q=319; //side take highest value
else
if(q<106) //if out of temperature limits from the bottom
q=106; //side take lowest value


q-=106; //adjust A2D value to the array

if(q<97)
{

r=Temps[q]; //Choose Temps table



if(r<0)
r*=-1;

goto Exit_GetTable;

}

if(q>=97 && q<106) //Choose Temps3 table
{
q-=97;
r=Temps3[q];

goto Exit_GetTable;

}

if(q>=106) //Choose Temps2 table
{
q-=106;
r=Temps2[q];

goto Exit_GetTable;
}



Pt1000:

if(q>865) //if out of temperature limits from the upper
q=865; //side take highest value
else
if(q<484) //if out of temperature limits from the bottom
q=484; //side take lowest value



q-=484;

if(q<183) //Choose a1 table
{


r=a1[q];


r+=1;


goto Exit_GetTable;

}



if(q>=183) //Choose a2 table
{
q-=183;
r=a2[q];



goto Exit_GetTable;
}





Exit_GetTable:


return(r);


}




ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 16, 2007 3:38 am     Reply with quote

I didn't look at your program as you have to know that all v4.xxx versions are still to be considered beta releases. It is called a beta release because there are very likely errors in these new releases. History has proved this to be an understatement... Sad
Version 4 of the compiler has become more or less useable from v4.030 and up. You might say there have been some bugs fixed in the 18 releases following your ancient v4.012. For current status of the v4 release I recommend reading the v4 sticky topic on top of this thread.

The last stable release is v3.249, please use that one for production quality software unless your chip is not supported by that release.
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

PostPosted: Mon Jul 16, 2007 3:49 am     Reply with quote

The version of ccsc.exe is 3.6.0.96 and the version of pcw.exe is 3.212.0.10

Are those unstable versions?

You think that version 3.249 will fix my problem?
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

PostPosted: Mon Jul 16, 2007 4:48 am     Reply with quote

Any help people.Can someone see my code?Cause if it isn't my code wrong then for sure i have to change my compiler version
Ttelmah
Guest







PostPosted: Mon Jul 16, 2007 5:57 am     Reply with quote

One reason for the few replies, is that your code layout is almost impossible to read/understand, both because of it's core 'design', and then because of not using the code buttons to post. A a general comment, using 'goto', is bad practice, and increases the likelyhood of something going wrong.
Now, I have retyped the block, without using 'got', and with notes of the numeric ranges involved added. Still not exactly tidy, but more readable. One of the tables, never has it's last value used, and this is noted as well:
Code:

int16 GetTable(int16 s,char t) {
   signed int16 q,r;

   q=s;

   if(t==2) //t=1 KTY81-122 sensor t=2 PT100 sensor {
      //Don't use 'goto' bad programming, and increases risk of bugs...
      if(q>865) //if out of temperature limits from the upper
         q=865; //side take highest value
      else
         if(q<484) //if out of temperature limits from the bottom
            q=484; //side take lowest value
      //value limited here to 484 to 865
      q-=484;
      //now 0 to 381

      if(q<183>319) //if out of temperature limits from the upper
         q=319; //side take highest value
      else
         if(q<106) //if out of temperature limits from the bottom
            q=106; //side take lowest value
      //Now for other sensor. q limited to 106 to 319
      q-=106; //adjust A2D value to the array
      //Now 0 to 213

      if(q<97) {
         //Here 0 to 96
         r=Temps[q]; //Choose Temps table
         //return only the positive value
         r = abs(r);
      }
      else {
        if(q<106) { //Choose Temps3 table
           //97 to 105
           q-=97;
           //0 to 8
           r=Temps3[q];

        }
        else { //Choose Temps2 table
           //106 to 213
           q-=106;
           //0 to 107 - note table here is one too long?...
           r=Temps2[q];
         
        }
      }
   }
   return(r);
}

As a minor 'comment', I'd avoid using 'long'. The probem is that using the named designators like this, can cause problems if you move to other compilers, where a 'long', or a 'short', can have different meanings. This is why it is safer to use the 'explicit' length designators (int1, int8, int16, int32), which make it easier to keep track of just how large a value 'is'.
3.212, is pretty old, and a lot of things were improved/ammended after this. However it was considered a fairly useable compiler, and I can't directly see anything that isa 'known' bug for the older compiler.
Multiplyng by -1, is a very slow way to generate the positive version of a -ve value. Quicker to use:

r=-r;

or just use the abs function which does the test, and sign reversal for you.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 16, 2007 7:00 am     Reply with quote

Sorry for misreading your version number. For v3.212 applies the same comment as for the early v4.0xx versions, this was a beta release as well. v3.19x were stable releases and than v3.2xx introduced many optimizations for the PIC18 processor but also many new bugs, only at about v3.225 things were stable again.

a small bug
Code:
if(q<97)
{
r=Temps[q]; //Choose Temps table

Temps[96] is outside of the defined array and will return an undefined value. This explains the other error Ttelmah found where the Temps3 table range is one too long.

In addition to Ttelmah's comments I want to add:
- Try using more meaningful variable names than a,q,r and s.
- Split the GetTable function in two separate functions for easier reading and maintenance.
- A nice feature is to introduce an enumerated type for the two sensor types instead of the magic numbers '1' and '2'.

Code:
enum TSensorType {KTY81, PT100};

//-----------------------------------------------------------------------------
int16 GetTable_KTY81(signed int16 q)

  int16 Result;

  // ...
 
  return Result;
}

//-----------------------------------------------------------------------------
int16 GetTable_PT100(signed int16 q)

  int16 Result;

  // ...

  return Result;
}

//-----------------------------------------------------------------------------
int16 GetTable(signed int16 s, TSensorType Sensor)
{
  int16 result;

  if (Sensor==KTY81) // if KTY81-122
    Result = GetTable_KTY81(s);
  else              // PT100 sensor
    Result = GetTable_PT100(s);

  return Result;
}

From main() this function is called with
Code:
    Result = GetTable(SomeValue, KTY81);
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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