View previous topic :: View next topic |
Author |
Message |
Acho Guest
|
Strange behavior of compiler when reading input pin in ISR |
Posted: Tue May 01, 2007 1:56 pm |
|
|
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
|
|
Posted: Tue May 01, 2007 2:21 pm |
|
|
Can you post all variable declaration statements ?
For example, post the declaration of "enc_dir". |
|
|
Acho Guest
|
|
Posted: Tue May 01, 2007 3:24 pm |
|
|
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
|
|
Posted: Tue May 01, 2007 3:42 pm |
|
|
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
|
|
Posted: Tue May 01, 2007 3:52 pm |
|
|
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
|
|
Posted: Tue May 01, 2007 4:01 pm |
|
|
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
|
|
Posted: Tue May 01, 2007 4:03 pm |
|
|
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
|
|
Posted: Tue May 01, 2007 4:11 pm |
|
|
Did you see my last post about getting vs. 4.033 ? |
|
|
|