Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

> or < for hex value in ASM?

Status
Not open for further replies.

netmgr

Member
In a pic 16f I want to calculate if a hex value held in w is between 2 values

lets says w=21

I want to see if w >20 but < 30

its got me baffled how I would do this in asm code

at the moment in doing:
xorlw h'27'
btfsc STATUS,Z

to test if it = 27, however I dont want to have to repeat this 9 times to check for all other values between 20 and 30

any ideas folks?
 
Look here under "range check":

I have found most of the routines on PicList to work well.

John
 
Two tests are done
Code:
   movf VALUE, W
   sublw 0x20      ; is it above h'20'  
   btfsc STATUS,C
   goto no    ;false
   movf VALUE, W
   sublw 0x30    ; is it over h'30'
   btfss STATUS,C
   goto no     ; false
   goto yes    ; true
 
Last edited:
thank you! ian, it seems it was the way i was declaring the values, ie 0x20, I was using h'20'
things are so much easier n VB!!
 
Use this

When working with the 16F family, I used the code below to skip next line IF the file's value was between hi and lo limits.

I cannot say where I copied it from. PicList?


Code:
SKIP8_F_IN_RANGE_L_H	MACRO FILE,LO_LIM,HI_LIM
				MOVF FILE,W					;W =FILE
				ADDLW 255 - HI_LIM			;W =FILE - HI_LIM
				ADDLW HI_LIM - LO_LIM + 1	;W =FILE - HI_LIM + HI_LIM - LO_LIM + 1
				BTFSS STATUS,C
				ENDM

Sorry but formatting anything here is not easy.
 
Last edited:
Two tests are done
Code:
        movf    VALUE, W
        sublw   0x20      ; is it above h'20'  
        btfsc   STATUS,C
        goto    no              ;false
        movf    VALUE, W
        sublw   0x30            ; is it over h'30'
        btfss   STATUS,C
        goto    no              ; false
        goto    yes             ; true

The method described in PICLIST, and by Agustín, is probably worth taking a closer look (if you're interested in assembly language).


Code:
;
;  test for WREG value in the range of 21..30
;
        addlw   255-30          ; upper limit 30 and
        addlw   30-21+1         ; lower limit 21 (range 21..30)
        skpc                    ; C=1 (in range)? yes, skip, else
        goto    xxxx            ; branch (out of range)
;
Code:
;
;  test for a pulse width of ~600-usecs ± 200-usecs
;  (upper limit 800-usecs, lower limit 400-usecs)
;
        movf    TMR0,W          ; pulse hi time (64-us ticks)     |B0
        addlw   255-(800/64)    ; test for ~600 ± 200 usecs       |B0
        addlw   (800-400)/64+1  ; C=1 (400..800 usec range)?      |B0
        skpc                    ; yes, skip (in range), else      |B0
        goto    irloop          ; branch (abort & start over)     |B0
;
 
Last edited:
In detail, you're essentially doing a subtraction, then testing the Carry/Borrow flag <STATUS,C> to see if the number that was subtracted is greater than or less than the number it was subtracted from.

Take for instance the equation 8-7. Since 7 is less than 8, you can subtract 7 from 8 without a borrow taking place. After this equation takes place, the Carry/Borrow flag will not be set.

Now if you reverse the equation so that it's 7-8, a borrow must take place because 8 is greater than 7. In this instance, the Carry/Borrow flag will be set after subtracting 8 from 7.

The routine would go as such -

Code:
	movlw		<VALUE1>		;load w with value 1
	sublw		<VALUE2>,W		;subtract value 2 from value 1, place result in w
	btfss		STATUS,C		;is value 2 greater than value 1?
	goto		SomeLineofCode		;no, jump to SomeLineofCode
	
	;continue with main code		;yes, continue with main code

Or you can reverse it by using "btfsc" instead so that it jumps to "SomeLineofCode" if value2 is greater and continues with main code if value2 is less.

Make sense?
 
Last edited:
Jon be wary of this

sublw <VALUE2>,W ;subtract value 2 from value 1, place result in w

The command is backwards... sublw...... it means subtract W from literal not the other way round..... It has fooled me a couple of times..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top