is incorrect , it will round the number up alwaysfrac= ((posn1+posn2)//100 +10)/10 ;round it up
is the way to round the number up if the last digit is 5 or morefrac= ((posn1+posn2)//100 +5)/10 ;round it up
is incorrect , it will round the number up alwaysfrac= ((posn1+posn2)//100 +10)/10 ;round it up
is the way to round the number up if the last digit is 5 or morefrac= ((posn1+posn2)//100 +5)/10 ;round it up
Warning I'm not a teacher
using n-bit math
result
ready v3
turns 23 f 489
33609 3664.3145
turns 23 f 486
33606 3663.9875
turns 23 f 484
33604 3663.7694
Code:Include "N-Bit_MATH.pbp" enc_new VAR BYTE bank0 enc_old VAR BYTE bank0 enc_counter VAR WORD bank0 Flag var BYTE bank0 tmp var byte[4] res var byte[4] tmp1 var byte[4] posn var WORD turns var word frac var WORD ; Set variable value @ startup Flag = 1 ;preset a value for testing enc_new = 0 enc_old= 0 enc_counter = 33609 ;preset a value for testing Main_Loop: if Flag = 1 then turns= enc_counter /1440 frac= enc_counter //1440 serout2 PORTa.0,84, [13,10,"turns ",#turns,9,"f ",#frac ] if frac then @ MOVE?CP 157000, _tmp @ MOVE?WP _frac, _tmp1 @ MATH_MUL _tmp1, _tmp, _res @ MOVE?CP 1440000, _tmp @ MATH_DIV _res, _tmp, _tmp1 @ MOVE?PW _tmp1, _posn @ MOVE?CP 144, _tmp @ MATH_DIV REG_Z, _tmp, _tmp1 @ MOVE?PW _tmp1, _frac endif posn=posn+turns*157 serout2 PORTa.0,84, [13,10,#enc_counter,9,dec4 posn,".",dec4 frac ] Flag = 0 endif goto Main_Loop end
Warning I'm not a teacher
I try to compile your code. I downloaded the N-Bit_MATH.pbp include, but the compiler stops with this error message:
"Bad data type"
If I try to compile only the N-Bit_MATH.pbp (only for test) and its do not compile. Stops on first line:
REG_X VAR BYTE[PRECISION] BANK0 SYSTEM
with the same error message MCU is the same 16F628A, For test I try also 16F688 with the same result. Something I'am doing wrong, I think.
Last edited by louislouis; - 11th October 2016 at 14:05.
You need to define the constant PRECISION and assign it a value equal to the number of bytes to use for the values in the math routine. And you need to do this before including N-Bit. In this case it looks like Richard opted for 32bit values (4 bytes)If you open the N-Bit file you'll find a link to the thread on this forum: http://www.picbasic.co.uk/forum/showthread.php?t=12433Code:PRECISION CON 4 SYSTEM '32bit values Include "N-Bit_Math.pbp"
/Henrik.
Yes, right. I forgot this line. After putting this line as you say the code compiles OK. Now I do some testing and trying to understand the code.
Bookmarks