Stuck Code

It appears that If an object is really close to the SRF05 sensor the code will get stuck. I'm thinking this is because an interrupt occurs while i'm waiting for a pulse from the SRF05. since the object is close the pulse is short and ends before the program returns from the interrupt. I've tried to disable interrupts for just that short period of time but that isnt working.

my code looks like this (if you'd like to see more just ask)

bcf INTCON,GIE ;temporarily disable interrupts

btfss LATA,1 ;check if pin A1 has goine high

goto SRF_wait ;when pin A1 goes high the loop will exit

bsf INTCON,GIE ; re-enable interrupts

The problem i'm having is with the two lines of code seen in bold. without them it only has the minor problem with close objects. with thenm the code seems to stall all the time.

**btfss LATA,1 **

btfss LATA,1 ;wait for the srf05 to give us a signal

Does this code wait for a signal and then continue on in the code or does it skip past it if no signal is detected? Since the next line is a goto make SURE it will skip the goto if the pin is high otherwise you got yourself a nice infinite loop.

the code only skips the goto
the code only skips the goto when there is the pin is high (or there is a 1 in bit 1 of LATA). I think what is happening is while its waitng for the pin to go high, an interrupt occurs and in the time it takes to run the interrupt serviec routine the pin has gone high and low (since the object is close to the sensor) this causes it to get stuck waiting for the pin to go high when it alredy has. My solution was to disable the interrupts while its waiting (since it really isnt that long). There is a problem with the two lines of code I’ve added to disable the interrupt and then re enable it. I cant really figure out whats going on since i dont have a debugger but it appears that disableing the interrupt causes the code to stall all the time and not just when an object is really close.

Does btfss LATA,1

Does btfss LATA,1 specifically say when the pin is high skip the next line of code? Seems like you need

While pin isnt high
btfss LATA,1

That way when the pin goes high it kicks out and reads the value. I dont see why the pin going high skips the goto line. I’m no expert on this chip so maybe your right I just don’t see why unless it is in the definiton of how btfss works.

btfss does not wait for

I think my comment on that line was misleading (i’ve changed it so its more clear). btfss does not wait for anything. it just checks the pin. my loop is what does the waiting.

here is how my loop works. first I check the pin. if its low then I loop back and check it again. if it is high then i continue going on with my code. My problem currently is with the two lines of code that i’ve added to this.


here is some information from the pic 18F1220 datasheet

BTFSS Bit Test File, Skip if Set

Syntax: [ label ] BTFSS f,b[,a]

0 ≤ f ≤ 255



a ∈ [0,1]

Operation: skip if (f<b>) = 1

Status Affected: None

Description: If bit ‘b’ in register ‘f’ is ‘1’, then the

next instruction is skipped.

If bit ‘b’ is ‘1’, then the next

instruction fetched during the current

instruction execution is discarded

and a NOP is executed instead,

making this a two-cycle instruction. If

‘a’ is ‘0’, the Access Bank will be

selected, overriding the BSR value. If

‘a’ = 1, then the bank will be selected

as per the BSR value (default).

Words: 1

Cycles: 1(2)

Note: 3 cycles if skip and followed

by a 2-word instruction.





Before Instruction

PC = address (HERE)

After Instruction

If FLAG<1> = 0;

PC = address (FALSE)

If FLAG<1> = 1;

PC = address (TRUE)