Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Memory corruption ? Pic just gives wrong values to variables ..

Status
Not open for further replies.

burrow

Full Member level 2
Full Member level 2
Joined
Dec 15, 2014
Messages
147
Helped
3
Reputation
6
Reaction score
3
Trophy points
18
Visit site
Activity points
1,148
Hai,
i had posted a similar question some time back, My program is really lengthy, thats why iam not pasting it here.
Iam using mikroc for coding, and my mcu is pic18f4520.

problem is that pic assigns wrong values to certain variables or in other words the value of variable changes from its intended value,

Suppose say during run time i change value in variable A to 10. and it stores 10 and displays it, but after going through some other part of code which has no relation with this variable A, the value of A changes.

When i alter the code and modify it to fix the issue ( by assigning some extra variables and all ) then the issue shifts to different part of a code.

i am using interrupts, but iam not using these variables directly in the interrupts.
but in main function i am reading the values of variables used inside interrupts.
 

Declaring variables use inside and outside an ISR as volatile is correct but in this case burrow says those variables are not used in the ISR. It would be useful to see the code and the language it is written in but from the description it sounds like a variable type is incorrectly declared and it is overwriting adjacent storage addresses.

Brian.
 

I've seen this sort of thing occur when a pointer goes 'bad' (points to a wrong address and is then dereferenced etc.) and also when array bounds are exceeded.
Without any other details being provided, it is also possible that the stack might be over flowing. I've not used the mikroc compiler but the XC8/16 compilers typically have the stack grow from low addresses to higher ones - still it might be something to consider.
Susan
 

Yes..its something i lile inside isr value changes but in main i just checks the variable.(no writing)

Also i have declared variables inside isr as volatile

- - - Updated - - -

It would be useful to see the code and the language it is written in n

.


I would cutoff unwanted portion and post the sample code.
And one more thing i noticed is that its effecting only thosebvariables whose value i change during run time. I have no ptoblrm withthe variables whose value is declared during compile time and duting runtime i just reaf them only
 

Make sure that all the variables are declared as global for better usage, don't write much more statements inside the ISR function.


Yea all variables are global.
Interrupt routines doesnt pefrorm any complicated things..it increments certain variables.
 

Assuming from post #5 the code is written in C, declaring all values as global is not good programming advice, it negates one the languages strengths and probably uses more memory.

It still sounds like you are writing a larger variable in a smaller storage location and 'overhanging' the edge of the variable in the next address. Or, as pointed out you may have a rogue pointer that is targetted at the offending variable. Either way, we need to see code to diagnose the problem and if you are cutting part of it out before posting, make sure we have at least the routine causing the problem AND the declarations of the variables you are using in that routine.

Brian.
 

I'm not sure if burrow completely understands the problems related to variables used both on interrupt and main level. The report is yet too vague to decide about a likely cause for data corruption. All reasons that have been mentioned in previous posts might be involved.

What you should do is:
1. Set up a reduced code example
2. Verify that the error still occurs

If step 2 fails, you are probably nearer to finding the problem by yourself.

- - - Updated - - -

Sorry, there's apparently a problem with forum time zones. This was posted after kam1787.
 

Assuming from post #5 the code is written in C, declaring all values as global is not good programming advice, it negates one the languages strengths and probably uses more memory.

and using variable names like A, or X, or Y1 is deplorable

- - - Updated - - -

I've seen this sort of thing occur when a pointer goes 'bad' (points to a wrong address and is then dereferenced etc.) and also when array bounds are exceeded.
Without any other details being provided, it is also possible that the stack might be over flowing.

my experience too
or
the OP is inadvertently using the same variable elsewhere for another purpose
 

Assuming from post #5 the code is written in C, declaring all values as global is not good programming advice, it negates one the languages strengths and probably uses more memory.

It still sounds like you are writing a larger variable in a smaller storage location and 'overhanging' the edge of the variable in the next address. Or, as pointed out you may have a rogue pointer that is targetted at the offending variable. Either way, we need to see code to diagnose the problem and if you are cutting part of it out before posting, make sure we have at least the routine causing the problem AND the declarations of the variables you are using in that routine.

Brian.

Yes its written in c using mikroc.

Anyway let me try to find what causes the problem

- - - Updated - - -

Yea iam trying to set up a reduced code example.
In the process let us hope i will be able to point out the cause myself.:) Will get back to you guys soon
 

Hai,
Sorry i was away for a while.
Just like FvM told in his reply ,In the process of reducing my code to post it here, i think i solved the problem. I didn't change any variables that had problem. But i did change other parts . So i guess now the problem has disappeared.. ( Maybe because now the address of variables changed, so might be problem has transferred to some other variables. As of now i dont see problems with any variables..
Anyway thanks everybody, if problem pops up with some other variable i will let you know
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top