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
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 " +
Dear Piclist, re. Range check when limits are in RAM I have an app. where speed of execution is paramount while looking for the range but the range limits can be calculated at leisure in which case... ...the sums 255-hi and hi-lo+1 can be evaluated and stored in 2 RAM locns. The ADDLW are replaced with ADDWF and I have a 2 instr. range test. It still fails if hi is 255 and lo is 0 which is a shame. It would be a simple way of including an always true condition. regards, Colin Smith firstname.lastname@example.org+
+The above window detector is nice and simple for those applications where the window limits are constants. Here's a macro that does window detecting when the limits are in RAM. WindowTest macro value, min, max ; Call as WindowTest value, min, max. Carry is set if value is within min-max (wtpass), ; and cleared if not (wtfail). ; The key to this is the subwf instruction. With subwf, the destination=f-w and ; carry =1 if destination=>0 (carry=1 is positive result). So, ; carry=1 if f=>w ; carry=0 if f<w or w>f local wtexit ; local labels in macro ; For min, we want value=>min, so we'll put value in f and min in w movfw min ; Get minimum subwf value,w ; w=f-w , w=value-min. Result positive (carry set) if value=>min bnc wtexit ; If no carry, we're below min. Exit with fail (carry clear) ; For max, we want max=>value, so we'll put max in f, value in w movfw value ; Get current value in w subwf max,w ; w=f-w = max-value. Result positive (carry set) if max=>value. Exit with carry set wtexit endm
|file: /Techref/microchip/rangechk.htm, 4KB, , updated: 2009/4/10 12:37, local time: 2017/1/20 12:21,
|©2017 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?|
<A HREF="http://www.piclist.com/techref/microchip/rangechk.htm"> PIC Microcontoller Range Check Math Methods </A>
|Did you find what you needed?|
PICList 2017 contributors:
o List host: MIT, Site host massmind.org, Top posters @20170120 Van Horn, David, RussellMc, David Duffy (AVD), IVP, Bob Blick, Sean Breheny, speff, Justin Richards, Richard Prosser, Brent Brown,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
Welcome to www.piclist.com!