A routine to limit range of 16bit variable

Status
Not open for further replies.

Lourens

Member
I am sure there are many more interesting pieces of code out there.... This one is for PIC18's to limit 16bit signed variables to a spesific range. Note that if the least significant byte is located at n then the most significant is at n+1.
 
Code:
if(var>567){var=567;}

This will make sure var is never over 567. If you want it never over 567 and never under 235

Code:
if(var>567){var=567;}
if(var<235){var=235;}

Is this what you were looking for?

I am not sure what it is exactly in asm but if you know C this may help you
 
Hi secretagent, i was not particularly looking for ways to limit a variable, this was rather aimed at the (older like me?) assembler users that require a fast way to limit a variable to a particular range, which was the case in my pid routine . Thanks for the comment.
 
Old thread I know, but if you can choose your range sensibly like between 512 & 256 then in assembler language it's just a case of clearing (in this case) all bits above bit 9
 
Hi WTP, you are correct. In many a case i want to make full use of the 16 bits because of accuracy (and when using fixed point). Some values (can be + or - ) in a calculation can however upset things if they are out of a particular range. This routine is then handy to keep things within limits.
Glad there are still some assembler programmers out there!
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…