trouble with pic assembler codes

Thread Starter

mantsali

Joined Oct 20, 2009
7
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
 

Markd77

Joined Sep 7, 2009
2,806
I haven't looked at all the code but I spotted a couple of things, there is no need to ever do bcf status, c because the bit always gives the result for the previous instruction.
It doesen't affect the operation but is unnecessary.
The other thing is that the carry bit in status is clear if there is a carry and set if there is not a carry, the opposite of what happens in an add. I've no idea why that is and it got me too.
 
Last edited:

atferrari

Joined Jan 6, 2004
4,764
The other thing is that the carry bit in status is clear if there is a carry and set if there is not a carry, .
In PICs from the 16F and 18F family, the C (carry) bit in the STATUS register will be set (=1) if the last operation generated a carry.
 

BMorse

Joined Sep 26, 2009
2,675
Rich (BB code):
Example 1: SUBLW 0x02
Before Instruction
W = 1
C = ?
After Instruction
W = 1
C = 1; result is positive
Example 2: Before Instruction
W = 2
C = ?
After Instruction
W = 0
C = 1; result is zero
Example 3: Before Instruction
W = 3
C = ?
After Instruction
W = 0xFF
C = 0; result is negative
My .02
 
Top