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

Strange behavior of compiler when reading input pin in ISR

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







Strange behavior of compiler when reading input pin in ISR
PostPosted: Tue May 01, 2007 1:56 pm     Reply with quote

Hi!

I program a PIC16F877A and I have noticed the following strange behavior of the PICC PCM Compiler v4.032:

When I compile this code everything seems to be ok:

Code:

#INT_RB
void Encoder_isr()            // Encoder ISR
{
   if(enc_a_state==input(Pin_B3))      // Encoder Ausgang B == Zustand Ausgang A
      enc_dir=1;         // rechtsdrehung
   else
      enc_dir=-1;         // linksdrehung
   ...
   ...
   ...
}


Note: enc_a_state is short (1 bit)
And I get this LST file:

Code:

.................... #INT_RB
.................... void Encoder_isr()            // Encoder ISR
....................    {
....................    if(enc_a_state==input(Pin_B3))      // Encoder Ausgang B == Zustand Ausgang A
*
0112:  MOVLW  00
0113:  BTFSC  enc_a_state
0114:  MOVLW  01
0115:  BSF    STATUS.RP0
0116:  MOVWF  @@57
0117:  BSF    TRISB.3
0118:  MOVLW  00
0119:  BCF    STATUS.RP0
011A:  BTFSC  PORTB.3
011B:  MOVLW  01
011C:  BSF    STATUS.RP0
011D:  SUBWF  @@57,W
011E:  BTFSS  STATUS.Z
011F:  GOTO   125
....................       enc_dir=1;         // rechtsdrehung
0120:  MOVLW  01
0121:  BCF    STATUS.RP0
0122:  MOVWF  enc_dir
....................    else 
0123:  GOTO   128
0124:  BSF    STATUS.RP0
....................       enc_dir=-1;         // linksdrehung
0125:  MOVLW  FF
0126:  BCF    STATUS.RP0
0127:  MOVWF  enc_dir
....................


But when I change the code to this (I exchange "input(Pin_B3)" and "enc_a_state" in the comparison):

Code:

#INT_RB
void Encoder_isr()            // Encoder ISR
{
   if(input(Pin_B3)==enc_a_state)      // Encoder Ausgang B == Zustand Ausgang A
      enc_dir=1;         // rechtsdrehung
   else
      enc_dir=-1;         // linksdrehung
...
...
...
}


I get this LST file which obviously is NOT ok and the program does not work right:

Code:

.................... #INT_RB
.................... void Encoder_isr()            // Encoder ISR
....................    {
....................    if(input(Pin_B3)==enc_a_state)      // Encoder Ausgang B == Zustand Ausgang A
*
0112:  BTFSS  INDF.0
0113:  GOTO   117
....................       enc_dir=1;         // rechtsdrehung
0114:  MOVLW  01
0115:  MOVWF  enc_dir
....................    else 
0116:  GOTO   119
....................       enc_dir=-1;         // linksdrehung
0117:  MOVLW  FF
0118:  MOVWF  enc_dir
....................


Do I misunderstand something or is it a serious compiler problem???
Thanks for your help in advance!

Regards,
Acho
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 01, 2007 2:21 pm     Reply with quote

Can you post all variable declaration statements ?
For example, post the declaration of "enc_dir".
Acho
Guest







PostPosted: Tue May 01, 2007 3:24 pm     Reply with quote

PCM programmer wrote:
Can you post all variable declaration statements ?
For example, post the declaration of "enc_dir".


Code:

short enc_a_state;
signed int enc_dir;


and at the beginning of the program I also have:
Code:

enc_a_state=0;
   if(ENCODER_A)                  // Zustand Encoder Ausgang A speichern
      enc_a_state=1;


BTW the same WRONG code comes when I write the ISR just like this:

Code:

#INT_RB
void Encoder_isr()            // Encoder ISR
{
   if(input(Pin_B3)==enc_a_state) ;

}


A friend of mine compiled the same program some time ago with PCM v3.233 and got good code, here is a copy of his LST :

Code:

.................... #INT_RB 
.................... void Encoder_isr()            // Encoder ISR 
....................    { 
....................    if(input(Pin_B3)==enc_a_state)      // Encoder Ausgang B == Zustand Ausgang A 
*
0112:  BSF    03.5
0113:  BSF    06.3
0114:  MOVLW  00
0115:  BCF    03.5
0116:  BTFSC  06.3
0117:  MOVLW  01
0118:  BSF    03.5
0119:  MOVWF  56
011A:  MOVLW  00
011B:  BCF    03.5
011C:  BTFSC  32.0
011D:  MOVLW  01
011E:  BSF    03.5
011F:  SUBWF  56,W
0120:  BTFSS  03.2
0121:  GOTO   127
....................       enc_dir=1;         // rechtsdrehung 
0122:  MOVLW  01
0123:  BCF    03.5
0124:  MOVWF  3B
....................    else   
0125:  GOTO   12A
0126:  BSF    03.5
....................       enc_dir=-1;         // linksdrehung 
0127:  MOVLW  FF
0128:  BCF    03.5
0129:  MOVWF  3B
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 01, 2007 3:42 pm     Reply with quote

I think you can fix the problem by casting the short variable to an int8.
See the change shown in bold below:
Quote:

short enc_a_state;
signed int enc_dir;

#INT_RB
void Encoder_isr()
{
if(input(Pin_B3) == (int8)enc_a_state);

}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 01, 2007 3:52 pm     Reply with quote

Looks like a bug to me. Perhaps there are ways to work around it but that's not the way the compiler is supposed to work.
Please report this bug to [email protected]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 01, 2007 4:01 pm     Reply with quote

It looks like they fixed the bug in vs. 4.033. If you email CCS and
show them the bug, and if your download rights have run out, they
will likely extend your download rights for one day, so you can get
vs. 4.033. Ask them (politely) if they will do this.

vs. 4.032:
Code:
.................... #INT_RB 
.................... void Encoder_isr()           
.................... { 
....................    if(input(Pin_B3)==enc_a_state); 
0033:  BTFSS  00.0
0034:  GOTO   035
.................... 
.................... } 
.................... 


vs. 4.033:
Code:
.................... #INT_RB 
.................... void Encoder_isr()           
.................... { 
....................    if(input(Pin_B3)==enc_a_state); 
0033:  BSF    03.5
0034:  BSF    06.3
0035:  MOVLW  00
0036:  BCF    03.5
0037:  BTFSC  06.3
0038:  MOVLW  01
0039:  MOVWF  2A
003A:  MOVLW  00
003B:  BTFSC  28.0
003C:  MOVLW  01
003D:  SUBWF  2A,W
003E:  BTFSC  03.2
003F:  GOTO   040
.................... 
.................... } 
Acho
Guest







PostPosted: Tue May 01, 2007 4:03 pm     Reply with quote

Thanks a lot this seems to compile ok!

PCM programmer wrote:
I think you can fix the problem by casting the short variable to an int8.
See the change shown in bold below:
Quote:

short enc_a_state;
signed int enc_dir;

#INT_RB
void Encoder_isr()
{
if(input(Pin_B3) == (int8)enc_a_state);

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 01, 2007 4:11 pm     Reply with quote

Did you see my last post about getting vs. 4.033 ?
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