|
|
View previous topic :: View next topic |
Author |
Message |
franckcl
Joined: 12 Sep 2003 Posts: 32 Location: France (Paris)
|
Problem with function called by an interrupt and main() |
Posted: Wed Jan 07, 2004 9:50 am |
|
|
Hi,
I have already posted a message about this problem but I have found a solution to solve it, and then I think that can be useful for other users and maybe to CCS to solve the bug (is it a bug ??).
(Using last CCS 3.182 and PIC18F6520)
I have a function which is called by the interrupt of timer #3 and also by the main function.
When I compile it the compiler display this warning :
">>> Warning 216 "C:\Soft\UTEX\Aff\testAff\test.c" Line 331(0,1): Interrupts disabled during call to prevent re-entrancy: UpdateReg"
My Program reset If I use the instruction "switch" with 4 cases but runs with 3 cases (or if I use the "if" instruction), see the source here :
Here is the function which does'nt works:
void UpdateReg(BYTE Latch,BYTE n)
{
BYTE i;
for (i=0;i<8;i++)
{
if (n & 0x80) Output_High(OUT_REG_DATA);
else Output_Low(OUT_REG_DATA);
Output_High(OUT_REG_CLK);
Output_Low(OUT_REG_CLK);
n<<=1;
}
switch (Latch)
{
case 1 : OutPut_High(OUT_REG_LATCH1);
OutPut_Low(OUT_REG_LATCH1);
break;
case 2 : OutPut_High(OUT_REG_LATCH2);
OutPut_Low(OUT_REG_LATCH2);
break;
case 3 : OutPut_High(OUT_REG_LATCH3);
OutPut_Low(OUT_REG_LATCH3);
break;
case 4 : OutPut_High(OUT_REG_LATCH4);
OutPut_Low(OUT_REG_LATCH4);
break;
}
If some one have an explication ?
Thank you
Franck |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Problem with function called by an interrupt and main() |
Posted: Wed Jan 07, 2004 9:58 am |
|
|
franckcl wrote: | Hi,
I have already posted a message about this problem but I have found a solution to solve it, and then I think that can be useful for other users and maybe to CCS to solve the bug (is it a bug ??).
(Using last CCS 3.182 and PIC18F6520)
I have a function which is called by the interrupt of timer #3 and also by the main function.
When I compile it the compiler display this warning :
">>> Warning 216 "C:\Soft\UTEX\Aff\testAff\test.c" Line 331(0,1): Interrupts disabled during call to prevent re-entrancy: UpdateReg"
My Program reset If I use the instruction "switch" with 4 cases but runs with 3 cases (or if I use the "if" instruction), see the source here :
Here is the function which does'nt works:
void UpdateReg(BYTE Latch,BYTE n)
{
BYTE i;
for (i=0;i<8;i++)
{
if (n & 0x80) Output_High(OUT_REG_DATA);
else Output_Low(OUT_REG_DATA);
Output_High(OUT_REG_CLK);
Output_Low(OUT_REG_CLK);
n<<=1;
}
switch (Latch)
{
case 1 : OutPut_High(OUT_REG_LATCH1);
OutPut_Low(OUT_REG_LATCH1);
break;
case 2 : OutPut_High(OUT_REG_LATCH2);
OutPut_Low(OUT_REG_LATCH2);
break;
case 3 : OutPut_High(OUT_REG_LATCH3);
OutPut_Low(OUT_REG_LATCH3);
break;
case 4 : OutPut_High(OUT_REG_LATCH4);
OutPut_Low(OUT_REG_LATCH4);
break;
}
If some one have an explication ?
Thank you
Franck |
It's a bad programming method to place a lot of code within an interupt. Using a switch in the way you have causes a jump table to be created. That in turn causes a lot of code to be generated. Using a jump table can be really good for some algorithm's but not if it's within an interupt because it causes behavior that is dificult to predict. Using if else statements is a much better aproach to your problem of 4 cases and does not require special handeling for interupts. |
|
|
Ttelmah Guest
|
Re: Problem with function called by an interrupt and main() |
Posted: Wed Jan 07, 2004 10:26 am |
|
|
Neutone wrote: | franckcl wrote: | Hi,
I have already posted a message about this problem but I have found a solution to solve it, and then I think that can be useful for other users and maybe to CCS to solve the bug (is it a bug ??).
(Using last CCS 3.182 and PIC18F6520)
I have a function which is called by the interrupt of timer #3 and also by the main function.
When I compile it the compiler display this warning :
">>> Warning 216 "C:\Soft\UTEX\Aff\testAff\test.c" Line 331(0,1): Interrupts disabled during call to prevent re-entrancy: UpdateReg"
My Program reset If I use the instruction "switch" with 4 cases but runs with 3 cases (or if I use the "if" instruction), see the source here :
Here is the function which does'nt works:
void UpdateReg(BYTE Latch,BYTE n)
{
BYTE i;
for (i=0;i<8;i++)
{
if (n & 0x80) Output_High(OUT_REG_DATA);
else Output_Low(OUT_REG_DATA);
Output_High(OUT_REG_CLK);
Output_Low(OUT_REG_CLK);
n<<=1;
}
switch (Latch)
{
case 1 : OutPut_High(OUT_REG_LATCH1);
OutPut_Low(OUT_REG_LATCH1);
break;
case 2 : OutPut_High(OUT_REG_LATCH2);
OutPut_Low(OUT_REG_LATCH2);
break;
case 3 : OutPut_High(OUT_REG_LATCH3);
OutPut_Low(OUT_REG_LATCH3);
break;
case 4 : OutPut_High(OUT_REG_LATCH4);
OutPut_Low(OUT_REG_LATCH4);
break;
}
If some one have an explication ?
Thank you
Franck |
It's a bad programming method to place a lot of code within an interupt. Using a switch in the way you have causes a jump table to be created. That in turn causes a lot of code to be generated. Using a jump table can be really good for some algorithm's but not if it's within an interupt because it causes behavior that is dificult to predict. Using if else statements is a much better aproach to your problem of 4 cases and does not require special handeling for interupts. |
Yes.
Worth also remembering, that if he adds a 'default' statement, to the switch, the code generated will go back to using (effectively) 'if' constructs internally.
Basically the change to using table jumps, occurs at a specific number of cases, and only if there is no 'default'.
The interrupt duration is going to get quite significant with the eight loops, and then the switch beyond, and as you say, in general it'd be better to avoid this (set a flag, and do the output in the main function).
The big problem with the jump table, is not the amount of code generated (this will be large with the if tests as well, once you get to this many cases), but that it also requires extra registers to be saved/restored on the interrupts as well...
It is worth saying that you can reduce the worst case duration involved in testing (though not the code size), by using a 'binary' search tree, rather than just testing for each case.
Best Wishes |
|
|
franckcl
Joined: 12 Sep 2003 Posts: 32 Location: France (Paris)
|
|
Posted: Wed Jan 07, 2004 10:57 am |
|
|
Thanks a lot Ttelmah and Neutone for your respons !
Best Wishes
Franck |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|