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.

Help with determing if a hex value is less than a variable

Status
Not open for further replies.

Steve311

Member
Hi all, i am using an enhanced midrange pic16f1824 and im trying to determine in a register value (0x01) is less than VARIABLE( 0x0f). Shouldnt the code below work ok?

Movlw 0x01
subwf VARIABLE, w
Bnc LessThan
goto GreaterThan

?

Thanks!
 
Movlw 0x01 --> Will Move the value 0x01 to the Work register.
subwf VARIABLE, w --> Will Subtract the value of Variable(0x0f) with value in w. And value of w is greater than 0x0f then carry flag should be generated.
Bnc LessThan --> If carry flag has been generated then go to label LessThan...
goto GreaterThan --> If not then go to label GreaterThan...

So, yes It seem above code should work to fulfill your requirement...
 
Last edited:
I'm a little out of practice with my assembler code so take this with a grain of salt.

You say you want to compare VARIABLE with a register, but then you movlw. Do you mean you want to compare to a constant? or should that be a MOVF?

What is BNC? I've had a flick through the 16f1824 datasheet and I don't see it. I presume it's a macro to do a BTFSS on C and then goto but we would need to see the final code after preprocessing to know what is going on.
 
should that be a MOVF?

MOVF f,d : will do the Move f to d (f -> d)
MOVLW k : will move constant to W(k -> w)

What is BNC?

BNC is not instructions as such, but macros supported by MPLAB. Microchip calls them "Special Instructions" since all of them are in fact obtained by combining already existing instructions. BNC is combination of BTFSS, GOTO instruction...

You say you want to compare VARIABLE with a register, but then you movlw.

your above statement can be made more clear by steve311 only, because he would be having better idea of the requirement...
 
Movlw 0x01 --> Will Move the value 0x01 to the Work register.
subwf VARIABLE, w --> Will Subtract the value of Variable(0x0f) with value in w. And value of w is greater than 0x0f then carry flag should be generated.
Bnc LessThan --> If carry flag has been generated then go to label LessThan...
goto GreaterThan --> If not then go to label GreaterThan...

So, yes It seem above code should work to fulfill your requirement...

Be careful with subwf.... It works the wrong way round.....

Subtract W from literal

It has caught me several times..... If subtracts the value in W from the variable So if W contains 0x1 and VARIABLE contains 0xf (15 ) the result (14 ) will be in the W register.
 
I think he meant the wrong way round in that it the two subtract commands have the operands in different orders in their names, and therefore however you look at it, one of them is "backwards". i.e. you have subwf and sublw, in both cases it is W which is being subtracted, but in one it is listed first in the name and in the other second.

I imagine that as a moderator on the forum he probably has enough experience to be familiar with the destination bit :)
 
Both subwf and sublw do it the non intuitive way. I.E. if W contains 23 then sublw 2 does 2-23 not 23-2 - likewise if the value is in a file it does file-W.

Mike.
 
Last edited:
I imagine that as a moderator on the forum he probably has enough experience to be familiar with the destination bit

I wouldn't count on it..... I make as many mistakes as everyone else... But this.... I do know. As Mike said.... It reads Sub "W" "F" assuming subtract f from w...
 
Sorry Ian, I didnt meant to learn you anything actually I was just making myself clear with your comment.......
 
Sorry Ian, I didnt meant to learn you anything actually I was just making myself clear with your comment.......

I know!! It didn't even enter my mind... All that matters is we don't confuse the OP......
 
When i was doing a lot of PIC asm programming I made my own macros;
SKPWGT (skip if W Greater Than F)
SKPWLE (skip if W is Less than or Equal to F)

I pasted the macro #defines in the top of the asm source code then could have the convenience of simple logical mnemonics for all the program control flow (and not have to manually look at STATUS register bits etc).

If you like I can dig up an old project and provide the macros (which will be for normal PIC 16F asm).
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top