PDA

View Full Version : strange int behaviour



tom
- 15th November 2005, 08:31
Hello dear pic friends and sorry for bad english!
Yesterday in my lab I have some strange int behaviour
So, when I wrote simple task like this:

define LOADER_USED 1
define OSC 16
TRISC.3=0
papak:
high PORTC.3
pause 5
low PORTC.3
pause 5
goto papak
end

I can see on my scope what I expected: square wave signal with period of
10ms (100Hz frequency). That is OK!

When I wrote assembler interrupt with TMR0 interrupt and do nothing in
main loop like this

define LOADER_USED 1
define OSC 16

wsave var byte $20 system
wsave1 var byte $a0 system
wsave2 var byte $120 system
wsave3 var byte $1a0 system

ssave var byte bank0 system
psave var byte bank0 system

define INTHAND myint
TRISC.3=0
goto main

asm
myint
movwf wsave
swapf STATUS,W
clrf STATUS
movwf ssave
movf PCLATH,W
movwf psave
clrf PCLATH
endasm
toggle PORTB.6
asm
movf psave,W
movwf PCLATH
swapf ssave,W
movwf STATUS
swapf wsave,F
swapf wsave,W
bcf INTCON,2
retfie
endasm

main:
OPTION_REG=%10000000
INTCON=%10100000

nothing:
goto nothing
end

In this case I also see on scope what I expected on PORTB.6:
My prescaler bits are 000, this is 1:2 TMR0 rate and my
osc is 16MHz so I have 256*2*250ns=128us low time and 128us
high time signal - this is about 3.9KHz signal on PORTB.6 pin.

But, when I combine these two simple tasks in one program like this:

define LOADER_USED 1
define OSC 16

wsave var byte $20 system
wsave1 var byte $a0 system
wsave2 var byte $120 system
wsave3 var byte $1a0 system

ssave var byte bank0 system
psave var byte bank0 system

define INTHAND myint
TRISC.3=0
goto main

asm
myint
movwf wsave
swapf STATUS,W
clrf STATUS
movwf ssave
movf PCLATH,W
movwf psave
clrf PCLATH
endasm
toggle PORTB.6
asm
movf psave,W
movwf PCLATH
swapf ssave,W
movwf STATUS
swapf wsave,F
swapf wsave,W
bcf INTCON,2
retfie
endasm

main:

OPTION_REG=%10000000
INTCON=%10100000
do:
high PORTC.3
pause 5
low PORTC.3
pause 5
goto do
end

I still have my int signal on PORTB6 pin OK(3.9KHz) but my main
routine becomes faster?! Instead of 100Hz signal on PORTC3 pin
like before now I have about 780Hz signal?!
Why?
It doesn't make any sense. The main routine should be the same
speed or slightly slower because of int latency but not faster!?

My PIC is 16F876 with 16MHz quartz and shane tolmie's bootloader.

Thanks for answers!

Darrel Taylor
- 15th November 2005, 09:06
Hi tom,

The 16F876 has 8K of program space.

PBP automatically inserts an Interrupt "Stub" for you if the program space is greater than 2K. Why?, I don't know. The "Stub" is identical to what you have at the beginning of "myint", and it's all done before getting to "myint". Then once in "myint" it tries to save them again, but now the registers have been modified. When they get restored after the interrupt, something will have the wrong value, affecting the main PBP program.

By testing for the code size first you can avoid the problem. Or, since you know the code size is larger than 2K, you can just leave that part out entirely.

IF (CODE_SIZE <= 2)
movwf wsave
swapf STATUS,W
clrf STATUS
movwf ssave
movf PCLATH,W
movwf psave
endif

tom
- 15th November 2005, 16:09
I comment out this part of code but still have the same situation...

J_Brittian
- 16th November 2005, 16:25
Tom,
Is everything faster? Do you still have a beautiful square wave with each step the same length? Or are some steps shorter than others?

tom
- 17th November 2005, 15:41
J Brittian:
Nice clean and seven times faster, but it doesn't matter.
We ddiscussed it already.