CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with function called by an interrupt and main()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
franckcl



Joined: 12 Sep 2003
Posts: 32
Location: France (Paris)

View user's profile Send private message

Problem with function called by an interrupt and main()
PostPosted: Wed Jan 07, 2004 9:50 am     Reply with quote

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

View user's profile Send private message

Re: Problem with function called by an interrupt and main()
PostPosted: Wed Jan 07, 2004 9:58 am     Reply with quote

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()
PostPosted: Wed Jan 07, 2004 10:26 am     Reply with quote

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)

View user's profile Send private message

PostPosted: Wed Jan 07, 2004 10:57 am     Reply with quote

Thanks a lot Ttelmah and Neutone for your respons !
Best Wishes
Franck
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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