Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem with Global Variables with PIC18F

Status
Not open for further replies.

demestav

Member
Hey guys,

I am working on a program and I noticed a strange behavior I cannot understand yet.

You see I have 2 global variables. I set a value to each of those variables in main()
Code:
unsigned short var1=0;
unsigned short var2=0;
...

void main(void)
{
 var1=1;
 var2=2;
}

I also have an interrupt that uses those variables. Everything was working great until I added those variables. I also tested it out in the MPLABX simulator, again giving strange behavior.

Then I took the 2 lines of code from the main (shown above) and put it in the interrupt. And it worked like a charm. I don't access those variables ANYwhere else. Just within the interrupt.

Could you please enlighten me?

Thanks
 
I Have had this before..... Section boundary crossing.... If you use udata section , it should work OK... in your example you are using the idata section.

Its a bug, I think, in the linker....

If you declare "unsigned short var1; it is placed in udata section (un-initialised data)
if you declare "unsigned short var1 = 0; it is placed in the idata section (initialised data)

Try this work round.
 
Last edited:
Are the global variables above all code?

It would be helpful to have cut down code that still exhibits this behaviour.

Edit, Your not doing if(var1=0) in your ISR instead of if(var1==0)? A very common mistake.

Mike.
 
Last edited:
Thanks guys...

Mike, the ISR is correctly set as var=1;

Well the problem is kinda solved but I don't quite understand why (and that is not good of course :) )

I made some changes. It should be something close to what Ian suggested in post #2. First of all, I played a bit with the linker file 18f4520.lkr and replaced the c018i.o with c018iz.o. I am not sure if this is correct but it was a lot to learn about the linker file in 5 minutes :). Then I went back to the program. Instead of having the variables var=0; for initialization, I used var=2 (something else than zero). The simulator and the device are now working as expected.

I don't know if this gives enough info to clear up what is going on. I am also sending you the .c file. Take a look if you have spare time.

Thanks again.
 
I have just simulated the code..... Initialising in the main DOES work however...... the main is interrupted BEFORE the initialisation on the first run... the GIE needs to be after all the interrupt setup.. and after the global variable initialisation ..

On the second interrupt it seems fine...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top