setting values and percentages


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    I wrote this little bit of code to experiment with this thread its interesting to note that to */ method fails when your percentage goes past 1600 in lieu of 50
    but the div32 method is quit sound up until the result exceeds 2^16 (65535 )
    you need to remember that */ is the middle 16 bits and if you overflow that your still in trouble
    Code:
    ****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 7/2/2014                                          *
    '*  Version : 1.0                                               *
    '*  Notes   : 16f684                                                  *
    '*          :                                                   *
    '****************************************************************
      
    #CONFIG
         __config _INTRC_OSC_NOCLKOUT & _CP_OFF & _WDT_ON  &  _PWRTE_ON  &  _MCLRE_OFF 
    #ENDCONFIG
     include "alldigital.pbp"
     
     
     
     
     
     
                  OPTION_REG.7=0
                  trisa = %11111110
                 
                  DEFINE OSC 4
                  so var porta.0
                  dq var portc.1
                  Nposn var word
                  ss var word
                  s2 var word    
                  s1 var word
                  
                   
        high so         
        ss=4095 
        s1=1600
        s2=100      
             
        pause 4000       
        serout2 so,84,[ "ready ",13,10] 
        lop: 
        pause 2000
        nposn=s1*ss
        nposn = div32 s2          
        serout2 so,84,["1 " ,#Nposn,13,10   ]   
        nposn=1600*4095/100          ; you need to adjust this line too
        serout2 so,84,["2 " ,#Nposn,13,10   ]
        nposn =  s1  */ 10484
        serout2 so,84,["3 ", #Nposn,13,10   ]
        nposn=s1*ss/100
        serout2 so,84,["4 ", #Nposn,13,10   ] 
        
        goto lop

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by richard View Post
    the div32 method is quit sound up until the result exceeds 2^16 (65535 )
    you need to remember that */ is the middle 16 bits and if you overflow that your still in trouble
    Is this because of the word variable being used?
    Is this different in PBPL?

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    yes only because the result needs to fit a word var


    all of this only applies to pbpw only , pbpl is another story
    Last edited by richard; - 4th July 2014 at 08:49. Reason: misunderstood question

  4. #4
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by richard View Post
    no its not because of the word var its just the way */ works .

    all of this only applies to pbpw only , pbpl is another story
    Yes */ can only result in a word the same applies to DIV32 in PBP2.xx as LONGs are not supported.

    PBP3 supports LONG variables and */ returns the top 32 bits of a 48 bits internal result, ignoring the lower 16 bits or dividing by 65536 which ever you prefer.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Hi,

    With PBPW a multiplication results in an internal 32bit result. The */ operator returns the middle 16 bits (inherent division by 256) while the ** operator returns the top 16bits (inherent division by 65536) of this 32bit intermediate result. You need a WORD variable to accommodate the "full" result.

    With PBPL a multiplication results in an internal 48bit result. The */ operator returns the middle 32 bits (inherent division by 256) while the ** operator returns the top 32bits (inherent division by 65536) of this 48bit intermediate result. You need a LONG variable to accommodate the full result.

    Yes */ can only result in a word the same applies to DIV32 in PBP2.xx as LONGs are not supported.
    You don't need PBP3 to use LONGs. Support for that was introduced in PBP2.50, 7 years ago. But you DO need to use an 18F part.

    And, finally, DIV32 isn't supported (not needed) with PBPL since it uses a 32bit division routine by default.

    All this according to the manual.

    EDIT: Both the */ and ** operators supports variables on both sides of the operator while DIV32 requires a constant on the right side. You can't do
    Code:
    Dummy = 1000*1000
    Result = DIV32 myVAR   ' Won't work, DIV32 requires division by a constant.
    EDIT again, about that DIV32 requiring a constant.....I'm not sure any more. Must try it on actual hardware so you may want to disregard that for the moment :-)

    /Henrik.
    Last edited by HenrikOlsson; - 4th July 2014 at 10:21.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Aargh, not allowed to edit my post....
    Regarding the DIV32 - I was wrong, it's more the other way around. Doing Dummy = 1000*1000 won't work because both are constants. At least one of the vaules must be a variable or the compiler will replace the multiplication with a constant resulting in the internal system variables not containing the value for DIV32 to operate on. See here and here for more details.

    Sorry for the mixup in my previous reply.

    /Henrik.

Similar Threads

  1. POT part values
    By Michael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th January 2009, 10:41
  2. Port Values
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd January 2007, 20:44
  3. t in POT, c's values
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th August 2006, 23:30
  4. Numeric Values
    By Armando Herjim in forum General
    Replies: 8
    Last Post: - 28th June 2006, 01:54
  5. Wrong values with COSINE!
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 8th May 2006, 16:59

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