Hi,
I'm using DEMO9S08QG8 for a project, however, this is the first time I use freescale. When I use the codewarrior to debug the firmware, I have a weird problem.
Below it is the code, the declare of varaibles are:
================================
int DL = 10, L = 10; // L and DL are constant
int DL2;
int dataX[10];
================================
Then, in the firmware, I need calculate some variables:
================================
DL2 = DL / 2;
D_X = 0, D_Y = 0, D_Z = 0;
for (i = L-1; i>=L - DL2; i--){
D_X = D_X + (dataX[i]-dataX[(int)(L-DL2)]) * (dataX[i]-dataX[(int)(L-DL2)]); //*****
}
================================
i loops from 9 to 5, when i=9, everything seems ok. but when i=8, suddenly, DL2 changed value to 0. DL2 is a constant although it is declared as 'int' only. It is not used in any other places in the firmware. Because of this, the calculation is wrong. dataX's value is small, 0 or 1, so, it is not overflow problem
So, I changed the code to :
================================
DL2 = DL / 2;
D_X = 0, D_Y = 0, D_Z = L - DL2;
for (i = L-1; i>=D_Z; i--){
D_Y = dataX[i]-dataX[(int)(L-DL2)];
D_Y = D_Y * D_Y;
D_X = D_X + D_Y;
}
================================
Then seems ok, DL2's value doesn't change. But I'm worrying about something else changed but I didn't notice. So, what can cause this problem? How to fix this? Anyone has any ideas? Really need some help.
thanks
urlreader