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.
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!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.