View previous topic :: View next topic |
Author |
Message |
KTrenholm
Joined: 19 Dec 2012 Posts: 43 Location: Connecticut, USA
|
Variable getting clobbered by calculations? |
Posted: Wed Feb 12, 2014 10:51 am |
|
|
Hi all,
I've got an issue with variables getting seemingly clobbered when I perform some math operations and I was hoping someone could maybe shed some light on what exactly the compiler/MCU is doing and how I can prevent it.
PCWHD 4.132
PIC24FV32KA302
I noticed that somehow one of my variables that I initialize early on in execution wasn't holding it's value when I perform some math on other unrelated values.
The variable in question is part of a structure. This structure has 4 members.
Code: |
typedef enum{
LedOff = 0,
LedOn = 1,
LedBlink = 2
}KeyLedModes;
typedef struct{
KeyLedModes OnBehavior;
KeyLedModes SuspendBehavior;
KeyLedModes BootupBehavior;
KeyLedModes OffBehavior;
}KpdLedProfile;
KpdLedProfile G_Led_Profile;
|
I set these values manually in a configuration function early on in the application:
Code: |
G_Led_Profile.OnBehavior = LedOn;
G_Led_Profile.SuspendBehavior = LedOff;
G_Led_Profile.BootupBehavior = LedOff;
G_Led_Profile.OffBehavior = LedOff;
|
I noticed a few functions later my value for OnBehavior has been reset to 0. I traced the change to a loop inside another function:
Code: |
#define ABS_MAX_BRT_STEPS 128
unsigned int16 PWM_Brightness_Table[ABS_MAX_BRT_STEPS];
float bl_step;
float bl_step_denominator;
float pwm_value;
unsigned int8 index;
pwm_value = 0;
bl_step_denominator = ABS_MAX_BRT_STEPS+1;
bl_step = ((float)PWM_BL_PR2Setting/bl_step_denominator);
Brt_Step = ABS_MAX_BRT_STEPS/(BrightnessTablePoints-1);
//AS OF HERE G_Led_Profile.OnBehavior is = 1
for (index=0;index <= ABS_MAX_BRT_STEPS;index++) {
pwm_value = (pwm_value+bl_step);
vbr_value += vbr_step;
PWM_Brightness_Table[index] = (unsigned int16)pwm_value;
}
//AT THIS POINT G_Led_Profile.OnBehavior is = 0
|
So somewhere in that loop calculating PWM DC steps, my value for OnBehavior is getting wiped.
I took a look at the symbol table and found that the array I'm filling out in that loop as well as the structure getting wiped are right next to each other:
8E0-9DF PWM_Brightness_Table
9E0-9E3 G_Led_Profile
So I have to imagine maybe I'm somehow overrunning the table and wiping the next location in memory. Could anyone maybe point out what kind of correction I could make to prevent anything getting wiped? Is there some sort of best practice that I'm missing that's causing this or is the compiler possibly managing the memory incorrectly?
Thanks in advance for any help.
Last edited by KTrenholm on Wed Feb 12, 2014 11:04 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 12, 2014 10:59 am |
|
|
You don't show us the array declarations, so we can see the datatype
and the size of the arrays.
Also, generally, don't post code that's commented out and not being used.
We just want to see clean code so we can concentrate on the problem. |
|
![](templates/subSilver/images/spacer.gif) |
KTrenholm
Joined: 19 Dec 2012 Posts: 43 Location: Connecticut, USA
|
|
Posted: Wed Feb 12, 2014 11:05 am |
|
|
PCM programmer wrote: | You don't show us the array declarations, so we can see the datatype
and the size of the arrays.
Also, generally, don't post code that's commented out and not being used.
We just want to see clean code so we can concentrate on the problem. |
Quite right.
I cleaned up the code to only include what's actually occurring and added the declarations. |
|
![](templates/subSilver/images/spacer.gif) |
jeremiah
Joined: 20 Jul 2010 Posts: 1329
|
|
Posted: Wed Feb 12, 2014 11:32 am |
|
|
Your array is declared with a size ABS_MAX_BRT_STEPS, which means 0 to (ABS_MAX_BRT_STEPS-1). Your loop goes from 0 to ABS_MAX_BRT_STEPS, which is one more than your array allows.
That might be it. It depends if those two variables are next to each other in RAM. |
|
![](templates/subSilver/images/spacer.gif) |
KTrenholm
Joined: 19 Dec 2012 Posts: 43 Location: Connecticut, USA
|
|
Posted: Wed Feb 12, 2014 11:39 am |
|
|
jeremiah wrote: | Your array is declared with a size ABS_MAX_BRT_STEPS, which means 0 to (ABS_MAX_BRT_STEPS-1). Your loop goes from 0 to ABS_MAX_BRT_STEPS, which is one more than your array allows.
That might be it. It depends if those two variables are next to each other in RAM. |
Oh wow you're totally right.
they are right next to each other. If I just change it from a '<=' to '<' should fix it.
Can't believe I didn't catch that. |
|
![](templates/subSilver/images/spacer.gif) |
|