im programming a pic 16f627 to recieve input from an ultrasonic reciever and to count the number of times it changes its state from high to low. depending on how high or low the counter is, i can determine how far from an obstacle it is. this is all done in a short delay of 0.5s to determine how many times a change occured every o.5s. but the problem arises when i take the final value of my third counter and subtract it from, what i believe should be, a higher value even though i have tried smaller value so that a carry occurs and the status bit c changes to 1. the change in the status bit should set my portb 3 but that never occurs could anyone tell where i went wrong, is it my thinking or my code
Rich (BB code):
;Sekoli, 3 september 2009
cnt1 equ h'20'
cnt2 equ h'21'
valx equ h'22'
valy equ h'23'
valz equ h'24'
cnt3 equ h'25'
org h'00'
goto init
org h'04'
retfie
init:
movlw h'07'
movwf cmcon
bsf status,5
;1=input 2=output
movlw b'11000000' ;set porta 6,7 as input and the rest as output
movwf trisa
movlw b'00000000' ;set portb to output
movwf trisb
bcf status,5
main:
clrf cnt1 ;clear counters
clrf cnt2
clrf cnt3
call delay2;
bcf status,c
movlw d'100' ;test value higher than cnt3
sublw cnt3 ;at a certain distance
btfsc status,c ;check for carry
goto set ;if carry true set portb,3
movlw d'10' ;test value lower than cnt3
sublw cnt3 ;at a certain distance
btfsc status,c ;check for carry
goto set ;if carry true set portb,3
goto clear ;else clear portb,3
;well all i keep geting is a low bit from portb,3
; movlw cnt3 ;tests final values from rx
; movwf portb ;and displays them in binary on portb
goto main
set:
bsf portb,3
nop
nop
nop
nop
nop
nop
nop
nop
nop
goto main
clear:
bcf portb,3
goto main
theCount:
incf cnt1,1
movlw d'255' ;sub max value from cnt1
subwf cnt1,0 ;if cnt1 reached max val
btfsc status,z
incf cnt2,1 ;inc cnt2
movlw d'255' ;do the same as for cnt1 with cnt2
subwf cnt2,0
btfsc status,z
incf cnt3,1 ;if cnt2 max val reached inc cnt3
return
;0.5s
delay2:
movlw d'3'
movwf valx
loopx:
movlw d'216'
movwf valy
loopy:
movlw d'255'
movwf valz
loopz:
btfss porta,7 ;is rx high
call theCount ;no, inc counters
decfsz valz,1
goto loopz
decfsz valy,1
goto loopy
decfsz valx,1
goto loopx
return