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.
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!