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.

PIC instruction: SUBLW

Status
Not open for further replies.

eblc1388

Active Member
Hi,

On PIC programming.

Can anyone gives an explanation of what is actually meant by "to reverse a sequence of numbers" from the following:

Using the PIC Subtract Instructions

Code:
In general, any time it is necessary to reverse
a sequence of numbers from A to B (where A and B
are both in the range of 0 to 255), you can use

SUBLW	A+B

which will work even if the sum A+B is greater than 255.

Thanks
 
If you have two numbers that you want to switch between, such as 15 and 47 then a simple way to do it is to calculate 62 (47+15) - N.

EG

62 - 47 = 15
62 - 15 = 47

It's a quirk of how the SUBLW works that makes this possible. The instruction subtracts W from a literal and so in the above example, W will flip between 47 and 15 every time the instruction SUBLW 62 is executed.

I don't think I explained that very well.

Mike.
 
Thanks, Pommie.

Just wonder as mentioned in the above code, whether the compiler would complaint if the value of the literal is larger than 255, says 199 & 201:

SUBLW 400 ;A+B larger than 255
 
You would just use the 8 bit value, the Pic will treat it as 9 bit any way - the 9th bit being the carry.

To switch between 200 and 100 do

SUBLW 44

Because

44-200 = - 156 = 100

and

44-100 = -56 = 200

HTH

Mike.
 
It will do,

W=(Speed_Warning - Speed_Warn_Hysteresis) - Valid_Speed

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top