please dont rip this site

PIC Microcontoller Range Check Math Methods


Test if a value is in a certain range

W = value

addlw 255 - Hi
addlw (Hi - Lo) + 1

Carry is set if W is in range Lo - Hi

It works for all values of Hi and Lo although it is not optimal for 255 and 0. It even works for ranges that wrap around 255 back to 0. For instance, if you set Lo to 240 and Hi to 15, it will match 0..15 and also 240..255. And it works for signed numbers. You can set Lo to -3 and Hi to 5 to match -3..+5.

The only requirement is that your assembler must truncate literals to eight bits. If not, you can still use it by masking the literals yourself:

        addlw (255 - Hi) & 0ffh
        addlw ((Hi - Lo) + 1) & 0ffh

For example:

Lower = 50
Upper = 60

addlw 255 - Hi equates to addlw 195
addlw (Hi - Lo) + 1 equates to addlw 11

W = 50          ; lower limit

addlw 195       ; w = 245
addlw 11        ; w = 0, C = 1, result = OK

W = 55          ; in range

addlw 195       ; w = 250
addlw 11        ; w = 5, C = 1, result = OK

W = 60          ; upper limit

addlw 195       ; w = 255
addlw 11        ; w = 11, C = 1, result = OK

W = 49          ; under minimum

addlw 195       ; w = 244
addlw 11        ; w = 255, C = 0, result = Not OK

W = 61          ; over maximum

addlw 195       ; w = 0
addlw 11        ; w = 11, C = 0, result = Not OK



Ondrej Zoubek Says: " please note, that 12-bit core (PIC16C5x, 12C5xx) has no addlw instruction " +

Code:

See also:


file: /techref/microchip/rangechk.htm, 4KB, , updated: 2009/4/10 12:37, local time: 2009/11/7 17:41,
TOP NEW HELP FIND: 
38.107.191.110:LOG IN
©2009 PLEASE DON'T RIP! DO: LINK / DIGG! / MAKE!

 ©2009 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it!
<A HREF="http://piclist.com/techref/microchip/rangechk.htm"> PIC Microcontoller Range Check Math Methods </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  LCD Front Panel Set: $8.99!
- 2x16 LCD HD44780 controller
- 4 push buttons - 3 LEDs - 2x12 header
 

Robotics nuts!

Check out http://www.verinet.com/~dlc/ email: dlc@verinet.com... This guy ROCKS! He has made (and sells but also releases code, docs, etc...) for a number of cool little robotic modules including whiskers, IR proximity detect and remote control, Sonar proximity detect, PWM, Servo, compass. Most of these use the little PIC 12C508 controller which costs basically nothing and is soooo tiny.
The 4 servos, 2400 baud serial servo controller is a wonder of magic and he sells the programmed chip for $8. Wow!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .