some help with math - Page 2


Closed Thread
Page 2 of 2 FirstFirst 12
Results 41 to 65 of 65

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    You might also explore the use of MAX and MIN for setting bounds in situations like:

    if Event1_OT_Hour >23 then Event1_OT_Hour = 23
    or
    if Event1_OT_Hour <1 then Event1_OT_Hour = 0
    Last edited by Amoque; - 10th September 2015 at 13:27. Reason: Sorry, missed Richard's post above.

  2. #2
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    hi thanks for the input guys

    to get it to do what i want i used longs ;(
    which i would like to change to 2 x word variables for this one long variable "Event_Dist_Speed "

    i thinking perhaps the nbit math may work but not sure how it may be done with the calculations i have now


    Code:
    ' routine to calc speed based on sec under 18 hours 
    Distance_Calc_Speed0:            
                  
      if Event_Dist_Timebase = 0 then Event_Dist_Speed = Event_Dist_Value                      ' calculate speed with base in secs 
      if Event_Dist_Timebase = 1 then Event_Dist_Speed = Event_Dist_Value*60                   ' calculate speed with base in mins
      Event_Dist_Speed = Event_Dist_Speed*100/(EL1_Seconds + (EL1_Minutes + EL1_Hours*60)*60)  
      if Event_Dist_Timebase = 2 then gosub Distance_Speed_Calc2                               ' calculate speed with base in hours - gosub becuase it is called as a seprate display 
     ' HSEROUT [" Event_Dist_Speed = ",dec Event_Dist_Speed, 10,13] ' debug 
                 
     return   
    ' ----------------------------------------------------------------
     ' routine to calc speed based on sec 's 
    ' used when time is >18 hours so that value is < word max size            
    Distance_Calc_Speed1:            
           
    
     return   
    
    ' ------------------------------------------------------------------
    ' show KPH or MPH on every base reading formular 
    ' calculate speed with hours base 
    ' called as 2nd speed display details 
    Distance_Speed_Calc2:
        
        Event_Dist_Speed = Event_Dist_Value*360 
        if Event_Dist_Measure  = 0 then  Event_Dist_Speed = (Event_Dist_Speed*1000/5280)/(EL1_Seconds + (EL1_Minutes + EL1_Hours*60)*60)       ' if feet distance base 
        if Event_Dist_Measure  = 1 then  Event_Dist_Speed =  Event_Dist_Speed*100       /(EL1_Seconds + (EL1_Minutes + EL1_Hours*60)*60)/100   ' if meters distance base 
        if Event_Dist_Measure  > 1 then  Event_Dist_Speed =  Event_Dist_Speed*1000      /(EL1_Seconds + (EL1_Minutes + EL1_Hours*60)*60)       ' if Miles or KM  distance base 
       
      '  HSEROUT [" hour reading Event_Dist_Speed = ",dec Event_Dist_Speed ,10,13] ' debug 
    return                     
    
    '------------------------------------------------------------------
    
    Show_Speed_Value:
       gl_x = 0
       gl_y = 48  
       gl_k = 0 
       for gl_k = 0 to 7
            lookup gl_k,["    x.xx"],Sml_font               ' 8 chrs 
            gosub Get_Sml_font                                    ' Get Font in K9_Fonts_banners.bas    
            gl_x = gl_x+8
       next gl_k      
      
       gl_x = 0                 ' puts 5 digits of speed 00000.xx 
       g_num = Event_Dist_Speed /100  ' get the base value 00000.xx 
       gosub Sml_put_5dig
                 
       
       gl_x = 48                ' put first 2 digits of speed xxxxx.00 
       g_num = Event_Dist_Speed 
       gosub Sml_put_2dig
       
      
       
              
      
     return

  3. #3
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    varable ranges information

    Event_Dist_Speed input = 1 - 4096
    time = combined values of EL1 varables - time range = 1sec - 8 days ' note days varable not used as yet , base time calculations based on secs
    expect that if hours >18 then the secs will be droped and time caclulation base of mins - not tested yet

  4. #4
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    Event1_OT_Hour=Event1_OT_Hour min 23
    yes have a done this - saved some space there - thanks

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    can i store the data as a word and then get it back as a byte or if needed a word if so how
    http://www.picbasic.co.uk/forum/showthread.php?t=19949

  6. #6
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    DT - ALL math done with these routines are done at the PRECISION level, and all variables used with the MATH functions must be (PRECISION) bytes in length.
    re reading the post make it clear

  7. #7
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    looking at the Nmath caclualtions

    can a math action be done directly without copy a pbp var to a Pvar first ?

    A var byte

    Tmp32bit_1 var byte[PRECISION]
    Tmp32bit_2 var byte[PRECISION]
    Result var byte[PRECISION]

    eg - direct method
    @ MATH_ADD A, Tmp32bit_2, Result ; A+B =Res


    or do i always need to get the standard PBP var byte into a Pvar before the math calculation

    eg

    @ MOVE?BP A , Tmp32bit_1 ' copy byte PBP to Pvar
    @ MATH_ADD Tmp32bit_1, Tmp32bit_2, Result ; A+B =Res


    cheers

    sheldon

  8. #8
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    thanks richard
    i am using darrels hi and low interupts services

  9. #9
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    If it’s bigger now, it won’t be if you put all of them in the same big array and skip to each group
    with an outer loop around that.

    Code:
    Event_OT var byte[8] ‘ or however many there are
    
    Event_OT_Day var Event_OT[0] ‘create alias to keep those names used elsewhere
    Event_OT_AllThoseOtherThings var Event_OT[1] ‘create alias
    …
    …
    
    index var byte ‘ this is re-useable for all of them
    
    if Event1_OT[0] >7 then 
    for index = 0 to 4
    Event_OT[index] = 0
    next index
    ENDIF

    You could check it isn’t cheaper to check for 0.
    It might not be now, but an earlier PBP, it would be cheaper.
    Code:
    'if Event_Penalty_Sec <1
    if Event_Penalty_Sec =0
    if your program uses the watchdog timer, there will be some buried in all of that.
    Code:
    DEFINE NO_CLRWDT
    You should get away with only one for that entire slab,
    and sprinkle any others only where needed
    Code:
    @ clrwdt
    definitely if there are any nops in code they can be replaced with them.
    Last edited by Art; - 11th September 2015 at 20:42.

  10. #10
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    this:
    Code:
       if val < 6 AND Menu_Select = 211 then 
                lookup val,[1,2,4,8,16,32],val      '  reasign  val to 
                gosub Show_current_sel_2            ' place "*    *" on the selected channel in menu using bit pattern sleection 
             endif 
             
             if val =>6  and val <= 11 and Menu_Select = 2112 then 
                lookup val-6,[1,2,4,8,16,32],val 
                gosub Show_current_sel_2 
             endif
             if val =>12 and Menu_Select = 2113 then
                lookup val-12,[1,2,4,8,16,32],val 
                gosub Show_current_sel_2
            endif
    this should work instead (directly replaced) :


    Code:
    thingyousubtract var byte ‘ new variable
    
    
       if val < 6 AND Menu_Select = 211 then 
                thingyousubtract = 0 : gosub memorysavelookup      '  reasign  val to 
                gosub Show_current_sel_2   ' place "*    *" on the selected channel
             endif 
             if val =>6  and val <= 11 and Menu_Select = 2112 then 
                thingyousubtract = 6 : gosub memorysavelookup 
                gosub Show_current_sel_2 
             endif
             if val =>12 and Menu_Select = 2113 then
                thingyousubtract = 12 : gosub memorysavelookup 
                gosub Show_current_sel_2
            endif
    
    
    memorysavelookup:
    lookup val-thingyousubtract,[1,2,4,8,16,32],val
    return
    and if that still works, you might as well:

    Code:
    thingyousubtract var byte ‘ new variable
    
       if val < 6 AND Menu_Select = 211 then 
                thingyousubtract = 0 : gosub memorysavelookup      '  reasign  val to 
             endif 
             if val =>6  and val <= 11 and Menu_Select = 2112 then 
                thingyousubtract = 6 : gosub memorysavelookup 
             endif
             if val =>12 and Menu_Select = 2113 then
                thingyousubtract = 12 : gosub memorysavelookup 
            endif
    
    memorysavelookup:
    lookup val-thingyousubtract,[1,2,4,8,16,32],val
    gosub Show_current_sel_2
    return
    then if it still works, you should be able to:

    Code:
    memorysavelookup:
    lookup val-thingyousubtract,[1,2,4,8,16,32],val
    goto Show_current_sel_2
    Now this is still assuming I haven’t broken it yet, you can:

    Code:
    thingyousubtract var byte ‘ new variable
    thingyousubtract = 0
    
       if val < 6 AND Menu_Select = 211 then 
             gosub memorysavelookup
             endif 
             if val =>6  and val <= 11 and Menu_Select = 2112 then 
             gosub memorysavelookup 
             endif
             if val =>12 and Menu_Select = 2113 then
             gosub memorysavelookup 
            endif
    
    memorysavelookup:
    lookup val-thingyousubtract,[1,2,4,8,16,32],val
    thingyousubtract = thingyousubtract + 6
    goto Show_current_sel_2
    Last edited by Art; - 11th September 2015 at 20:56.

  11. #11
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    But wait there’s more!
    If you call within the next 20 minutes I’ll also throw in this rotating bit
    (but also found I broke it and got to steal some memory back)

    Code:
    thingyousubtract var byte ‘ new variable
    thingyousubtract = 0
    
       if val < 6 AND Menu_Select = 211 then 
       thingyousubtract = 0 : gosub memorysaverotate
       endif 
       if val =>6  and val <= 11 and Menu_Select = 2112 then 
       thingyousubtract = 6 : gosub memorysaverotate 
       endif
       if val =>12 and Menu_Select = 2113 then
       thingyousubtract = 12 : gosub memorysaverotate 
       endif
    
    memorysaverotate:
    val = 1<<(val-thingyousubtract)
    goto Show_current_sel_2
    Last edited by Art; - 11th September 2015 at 21:29.

  12. #12
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    well i ended up changing all the posted code that needed the long varable to work out speed to Nbit math

    it took some work time ,but the rewards are there in size of the reduced compile size - in my case i saved 8.5k over using longs

    although i needed only 1 long varable - the speed value - selecting longs added 10.5k to compile size as soon as you tick the box , and that is a very big overhead when flash space is very tight

    Nbit math has it high overheads as well - the include is compiled about 700bytes , but the real overhead is in having to use multi 32bit PRESISON varables( 4 were needed) to do the step by step math operations that can be avoided when using longs , as a result more ram space is used , and it is noticeably slower than using longs when showing the results, even when at 64Mhz.

    Also having to do multi divides , the separate remainder value needs to be reincorporated into any following math which adds extra code and where possible should be avoided until you need the result reading back into PBP

    anyone having to add longs , where only1 or 2 longs may be need should look at Nbit math as a possible alternative , more so when flash space is tight and longs are an expensive overhead in compile space.

    cheers

    Sheldon

  13. #13
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    thanks art that works well
    i used an exiting variable for the temp one

    i also did multi if /then statements , which reduced the compile further - those "and " do add up in compile

    i have not seen the command ":" to allow you to put on the same line before btw

    here what works

    cheers

    Sheldon

    Code:
     Show_RF_CH_Select:
     
             gl_y = 8                                           ' show current channel in use on menu heading line  - gl_y = 8  , gl_x= 162,168 
             gl_x= 160                                             
             read $09,val                                       ' get channel ref 
             g_num = val   
             if g_num dig 1 = 0  then                           ' leading 0 supression 
                 gosub put_1dig
                 gl_x = gl_x +6 
                 g_char = " "       ' clear 2nd digit space, case it was used 
                 gosub put_char 
             else
                 gosub put_2dig 
             endif                     
            g_num = 0                                 ' use as temp verable 
             if val < 6 then                          ' if current RF ch = 0-5
                if Menu_Select = 211 then             ' and menu 211
                  g_num = 0                           ' value offset for ch
                  gosub Show_RF_CH_Select_common      ' show  *     * on the selected ch
                endif
             endif    
                                                     
             if val =>6 then                           ' if current RF ch = 6-11
                 if val <= 11 then                     
                   if Menu_Select = 2112 then          ' and on menu 2112
                      g_num = 6                        ' value offset for ch 
                      gosub Show_RF_CH_Select_common
                   endif
                 endif
             endif     
             if val =>12 then                           ' if current RF ch = 6-11
               if Menu_Select = 2113 then           
                   g_num = 12                           ' value offset for ch
                   gosub Show_RF_CH_Select_common       ' show  *     * on the selected ch
               endif
             endif   
     return
     '------------------------------
     Show_RF_CH_Select_common:
        val = 1<<(val-g_num)      ' val = shift right 1 of VAL- Offset RF menu to get   1,2,4,8,16,32 in bit pattern
        gosub Show_current_sel_2  ' show "*   * on selected menu / ch using the bit pattern of VAL 
     return

  14. #14
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    I’m glad if it saved some memory... I made a bigger post but it looks to have disappeared.

  15. #15
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    yes i am the point that every bit really counts even a large change to get a small result has its merits

  16. #16
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    if i do write data to the flash mem using the the write table asm , how do reserve the space so the compiler does not use it , write all over the top of it

  17. #17
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    13 words

    Code:
    g_num var byte 
        g_num = 10   
             if g_num <10  then                           ' leading 0 supression 
                 gosub d1
                 
             else
                 gosub d2
             endif                     
     d1:
     return
     d2:
     return

    84 words
    Code:
    g_num var byte 
        g_num = 10   
             if g_num dig 1 =0 then                           ' leading 0 supression 
                 gosub d1
                 
             else
                 gosub d2
             endif                     
     d1:
     return
     d2:
     return

    if i do write data to the flash mem using the the write table asm , how do reserve the space so the compiler does not use it , write all over the top of it

    do u mean with TBLWT OR DT

  18. #18
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    thanks richard that little change got back 600 byte over all

  19. #19
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    in relation to using the peek/poke code routine i have is to put font tables into cpu flash , but that uses 2 times the space in flash than what is need for the data alone as it takes it sotres a command + data byte

    i would like to use the same arrangment from @getaddress which is incorporated in to darrel's int-18 routines
    but not store the instruction as the pokecode does

    Code:
    LG_Font_Base:
              ' Note gl_x char space need be 12 , not normal 24 when using chr else too big a gap space _
              ' Code for "-"  Asci 45   : note -gl_i(0) is ASCI CHR CODE , 90 real values _
    pokecode $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_    
                 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$3F,$FC,$00,$3F,$FC,$00,$3F,$FC,_   
                 $00,$3F,$FC,$00,$3F,$FC,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
                 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00

    Code:
    
    '=================== FONT COMMON ROUTINE - pokecode use ========================== 
    ' Assumes gl_x, gl_y,X_scan,Y_scan,Font_lookup   
    ' ALL FONTS MUST BE gl_x = 8 , 16 , 24 etc 
    ' Inv_font = 0 = normal 1= inverse   
     Font_Common: 
       x_save = gl_x                ' save the gl_x start point value 
       y_save = gl_y                ' save the gl_y start point value
       gl_yb  = y_save + Y_scan     ' number of y scans for the char from y starting point(0) 
       gl_xb  = 0 
       Address = 0 
       if Font_lookup = 1  then 
    @ GetAddress _LG_Font_Base, _Font_Base_Address                 ; gets base start address of LG_font into  Font_Base_Address varable 
          Font_Index = (LG_font - 45)* (90*2)                       ' remove the ascii chr value 45 offset  0 * font chr length x 2 cos of RETLW INSTRUCTION for each byte read  in index 
       endif
            
       if Font_lookup = 2  then                                    ' remove the offset of the first symbol $2D / 45  so start at 0 
    @ GetAddress _Med_Font_Base, _Font_Base_Address
            Font_Index = (Med_font - 45)* (40*2)                   ' remove the ascii chr value 45 offset * font chr length in index
       endif                                        
       Address = Font_Base_Address + Font_Index                    'gets you to the start of the glyph
     
       for gl_y = y_save to gl_yb      ' y scan lines, start to finish values
           gl_x = x_save               ' clear x position to start point (keep it an 8bit multiple)
           gosub gl_gaddr              ' Set address
           
          for gl_xb = 1 to X_scan        ' gl_x byte x  X scan bytes on each line
              peekcode Address,gl_byte   ' read the indexed byte in
              glcd_msb = gl_byte         
              glcd_cmd = DATA_WR_INC     ' write and increment
              gosub send_1               ' send to glcd
              Address = Address+2        ' inc address here +2 cos each byte includes , as data start at 0 not 1 as per lookup tables previously  
          next gl_xb                     ' next byte of font
       next gl_y                       ' next scan line of font
       gl_y = y_save                   ' Restore the Y location    
       gl_x = x_save                   ' Restore the X location
     return

  20. #20
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    Code:
    LG_Font_Base:
      
    @ db      0x00,0x00,...................................
    16 bytes to a line is good

    good
    Code:
    @ GetAddress _LG_Font_Base, _Font_Base_Address
    note 64k address limit


    Code:
    @ GetAddress _Med_Font_Base, _Font_Base_Address ;note  64k address limit
            Font_Index = (Med_font - 45)* (40*2)                   ' remove the ascii chr value 45 offset * font chr length in index
       endif                                        
       Address = Font_Base_Address + Font_Index                    'gets you to the start of the glyph
     
       for gl_y = y_save to gl_yb      ' y scan lines, start to finish values
           gl_x = x_save               ' clear x position to start point (keep it an 8bit multiple)
           gosub gl_gaddr              ' Set address
           
          for gl_xb = 1 to X_scan        ' gl_x byte x  X scan bytes on each line
              readcode Address,gl_byte   ' read the indexed byte in   ;note  64k address limit for readcode
              glcd_msb = gl_byte         
              glcd_cmd = DATA_WR_INC     ' write and increment
              gosub send_1               ' send to glcd
              Address = Address+1        ' inc address here +2 cos each byte includes , as data start at 0 not 1 as per lookup tables previously  
          next gl_xb                     ' next byte of font
       next gl_y                       ' next scan line of font

    look at my tft font and tft-spi.pbpmod (gcga: subroutine)

    there are other ways to get around the 64k limitation in that code too

  21. #21
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    What am I missing here?
    PBP integer math, if something is less than 1 is another way to check if it’s zero.
    and should be able to be deleted without any consequence.
    ie.. if something = 0 then something = 0.

    Code:
     if Event2_OT_Min <1        then Event2_OT_Min     = 0

  22. #22
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    yes that was stuff up - it was to stop it going passed a 0 value when a key was pushed and it never worked cos it was checked after the key had removed the value, so then value was the max value and so it never worked
    so it was changed in the key routines to

    if key = x then
    if value <> 0 then value = value - 1
    endif

  23. #23
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    the problem i am having with the @ db 0x?? , 0x?? is it takes the 1st byte as the low byte , the next as the high byte , so the data is out of sequance when stored into the flash

    is there away to reverce the seqaunce ( eg high byte 1st , lowbyte 2nd m etc , without changing the font data

  24. #24
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    or i guess readcode can reads in a word value and i swap it in the routine some how

  25. #25
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: some help with math

    unfortunalty the for /next loop of x_scan can be 2 or 3 so not easy to break into even words -mm darn

Similar Threads

  1. Help with A/D math!
    By dbodenheimer in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 19th March 2011, 00:51
  2. Math help please...
    By Ioannis in forum General
    Replies: 2
    Last Post: - 20th June 2008, 10:18
  3. PBPL Math...new math takes more cycles...Always?
    By skimask in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th February 2008, 10:22
  4. Math help please!!!
    By jbirnsch in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 10th August 2007, 14:45
  5. math help
    By houa in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 30th January 2006, 16:58

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts