This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

problem comparing signed char

hi i am trying to compare the value of two signed chars. one is a plain variable currentval while the other is a signed char inside an array of type signed char called spectrumarray.for some reason it doesnt work.

xdata signed char spectrumarray[256];
signed char currentval;//current value at this freq
int xdata channel;

for(channel=0;channel<256;channel++){

  for(sample=0;sample<70;sample++){//sample 70 times at this freq
  currentval=halSpiReadStatus(CCxxx0_RSSI); //this just reads a register from some a source external to the microcontroller

    if((signed char)spectrumarray[channel] < (signed char)currentval)

      spectrumarray[channel]=currentval; //want to put currentval  into the array if it is more than the value currently in the array

  }
printf("%Bd\n\r",(signed char)currentval);

//did this just to check to see what the currentval was
}

Used this to initalize the array

void init_array(void){
  int xdata arrayindex;
  for(arrayindex=0;arrayindex<256;arrayindex++){
        spectrumarray[arrayindex]=-128;//initialize to the lowest value possible
  }
}


I am sure the currentval values are more than what is inside the array when they are compared, yet when i print out the whole array it only holds -128 for every value except the last.I'm kind of stumped.oh yea forgot to mention im also new to the C51. microcontroller in use is the Silabs C8051F320.

  • if((signed char)spectrumarray[channel] < (signed char)currentval)
    

    Those (signed char) casts are quite totally pointless.

    I am sure the currentval values are more than what is inside the array when they are compared,

    What makes you so sure?

  • We have no way of knowing what "doesn't work" means to you. What does it mean?

    What happens if you get rid off all the superfluous (signed char) casts?

  • oh yea the (signed char) cast, i was just frustrated and was just trying random stuff didnt make any diff tho.umm i printed it out right after each comparison to see what it was i.e right after the second for loop. the values were all more than -128. By doesn't work i mean the value at that postion in the array should be made equal to the value of currentval if it is less than currentval.however it doesn't change.im sorry if i wasnt too clear

  • "yet when i print out the whole array it only holds -128 for every value except the last."

    You should probably also show us the code to print the whole array.

    You've got a mix of explicit xdata objects and objects in the default memory space. What memory model are you using?

    Sometimes it's useful to compile to assembly source or to look at the assembly listing to see if the generated code is doing what you intended and if not, that will point to what's wrong.

  • "microcontroller in use is the Silabs C8051F320."

    That has on-chip debug, doesn't it?

    Rather than mess about with printf (which could introduce bugs of its own), have you single-stepped through the code and examined what's actually happening in the chip at eah step?

    Have you tried your code in the simulator?

  • "You've got a mix of explicit xdata objects and objects in the default memory space. What memory model are you using?"

    Also, does this chip needs any specific setup to enable XDATA?
    If so, have you done it?

  • Umm it seems when i took those variables out of xdata and just placed them in normal memory it works now. the array is however still in xdata.When i print it after i intialize it it says -128 and after i put stuff in it it holds sensible values, so it works now.The memory model i used was small but i put the array in xdata as it was large.
    Im now wondering exactly why that worked so that i dont make that/those mistake(s) again. Also thanks for the help b4 ppl.

  • ok im sorry i realized my error. it was pretty stupid i cut an paste the code from another function i made and i initialized the array in the wrong place. and i kept telling myself i didnt do that arr sorry ppl

  • "i kept telling myself..."

    So, the lesson to learn here is: assume nothing!

    Always verify your assumptions; test your theories.

    As already mentioned, this chip has on-chip debug - use it!