PDA

View Full Version : count problem



servo260
- 21st December 2004, 03:31
Hi, i'm using the COUNT command along with an asm interrupt wich shows in a display de variable used by count.
without the interrupt the commad works fine, but now I'm getting all kind numbers.
Is there any special variable used by count that I need to save before the interrupt for it to work ok?

mister_e
- 21st December 2004, 07:28
IMO, disable interrupt before counting will solve the problem, but there's certainely other way ...See section 9.3 of the PBP manual. It must point you in the right direction.

BTW wich PIC are you using ? you can look in the according PBPPICXXX.LIB to see what value are use in COUNT. Try R1 and W.

regards

Ingvar
- 21st December 2004, 09:54
It's always a good idea to post your code. It's difficult to help otherwise ........

Count uses FSR and RM1. FSR needs to be saved and restored in your ASM blocks. RM1 is a PBP variable and needs to be saved and restored the same way as R0,1,2 and 3.



fsave var byte bank0 system
rm1 var word bank0

asm
display
movwf wsave ; display rutine
swapf STATUS, W
clrf STATUS
movwf ssave
movf PCLATH, W
movwf psave
movf FSR,w
movwf fsave
endasm
r0save = R0
r1save = R1
r2save = R2
r3save = R3
rm1save = RM1

'blah blah ......

R0 = r0save
R1 = r1save
R2 = r2save
R3 = r3save
RM1 = rm1save
asm
bcf INTCON, 1
movf fsave,w
movwf FSR
movf psave, W
movwf PCLATH
swapf ssave, W
movwf STATUS
swapf wsave, F
swapf wsave, W
retfie
endasm


It's always a good idea to tell wich PIC and compiler you're using.

/Ingvar

servo260
- 21st December 2004, 17:33
Hi, here is the code I i'm using to test count and asm interruprs with a 16f84a, some var are in sapanish, I hope it's no a problem to understand it, I can't find what is wrong with it.

define osc 20


clear

pulsos var byte

x var word ; display value

num var byte


velocidad var byte

wsave var byte $20 system
ssave var byte bank0 system
psave var byte bank0 system
r0save VAR WORD bank0
r1save VAR WORD bank0
r2save VAR WORD bank0
r3save VAR WORD bank0
fsave var byte bank0 system
rm1save var word bank0



TRISA = %11111111

TRISB = %00000000


goto start


define INTHAND display



start:

option_reg = %00000011


intcon = %10100000

main:



count porta.2,1000,pulsos



x = pulsos


goto main






asm
display
ENDASM

r0save = R0
r1save = R1
r2save = R2
r3save = R3
fsave = fsr
rm1save = RM1

ASM
movwf wsave
swapf STATUS, W
clrf STATUS
movwf ssave
movf PCLATH, W
movwf psave

endasm



num = x dig 0

portb = num

high portb.4

pause 7

low portb.4

num = x dig 1

portb = num

high portb.5

pause 7

low portb.5

num = x dig 2

portb = num

high portb.6

pause 7

low portb.6

TMR0 = 60

INTCON.2 = 0

asm

movf psave, W

movf psave, W
movwf PCLATH
swapf ssave, W
movwf STATUS
swapf wsave, F
swapf wsave, W
endasm

fsr = fsave
R0 = r0save
R1 = r1save
R2 = r2save
R3 = r3save
RM1 = rm1save

asm
retfie
endasm

Ingvar
- 22nd December 2004, 10:29
Hi,

First of all ...... you didn't follow my instructions, the saving and restoring of PBPvariables and registers are shuffled. Not that it matters much, it will not work anyway. Now when i see your code, i understand what you're trying to do. It will not work for several reasons .... the most obvious one beeing that your interruptroutine will take much longer time than the timer needs to rollover. This means that your pic will be busy handling the display almost all the time. That leaves virtually no time to count pulses. It can be done if you write a true assemblerinterrupt and slow the display. I would have helped you but i'm going away for the hollidays. Perhaps someone else could help, there must be somone more than me that have done this in the past.

/Ingvar

servo260
- 22nd December 2004, 15:05
Hi, I did follow your instructions and everithing is working fine now, thanks a lot.
Some how I post and older code, I didn't notice, sorry.
And thanks again because I'm new with pics and this help me a lot.