Wierd result when inverting an input


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    BTW,

    I remember Darrel had written a post about this kind of behaviour.
    he used a "!!" to get rid of the problem.

    does somebody remember where it was shown ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  2. #2
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    That post would be interesting to read.
    I don't see how it would alleviate the issue.
    I think without an interim step to force the assignment to a bit type variable it will perform the operation as a byte.

    I might take a look through the PBP library macros for Logical and bitwise NOTs to see if there is another macro that would hand the situation. If there is, it would be a matter of what syntax would cause the compiler to pick that macro instead.
    Regards,
    TABSoft

  3. #3
    Join Date
    Oct 2011
    Location
    East Coast USA
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    It would seem that the simple way around this is:

    Report = 48 + !Mypin

    Wouln't that force
    Picbasic to evaluate Report as a byte, not as a bit?

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    No, not really.
    That has been the point of this discussion.

    We would not want PBP to do the interim calculations as a byte, we would want PBP to do them as a bit.

    The issue is that in this statement "Report = 48 + !Mypin"
    Report is a byte variable, 48 will be treated as a constant and Mypin is a bit.
    Sounds good so far, but, when PBP looks at this statement it will treat the !Mypin as a byte output in the calculation step.
    It does this because "Report" on the left side is a byte variable.

    What we want is if Mypin = 0 then Report = 49; If Mypin = 1 then Report = 48.

    But here is what happens.

    Pseudocode for the PBP ASM Macros used for the following Statement: Report = 48 + !Mypin
    Code:
    Step 1. Perform a Logical NOT on Mypin.
        Clear the W register (Set it to 0)
        If Mypin is a zero then 
            Store 255 decimal in the W register  'This is because Report will ultimately get the value, so PBP starts treating all the output variables as bytes
        else
            Store 0 in W register
        endif
        Move W register to a temporary "byte" variable T1
        
    Step 2. Add 48 to temporary "byte" variable T1
            
    Step 3. Move Temporary "byte" variable T1 to "byte" variable Report
    So if you start with Mypin = 0, then
    Code:
    Step 1. T1 = 255
    Step 2. T1 = 255 + 48 = 47   ' This is because a byte variable is unsigned in PBP
    Step 3. Report = 47 'We wanted Report to equal 49 not 47
    
    If you start with Mypin = 1
    Step 1. T1 = 0
    Step 2. T1 = 0 + 48 = 48
    Step 3. Report = 48 'This is what we wanted.
    So the issue as I can see is that to perform this logic as we "want" it to work, you need to manually perform an interim step
    and store the current value of Mypin in a "bit" variable. Then perform a logical NOT on the "bit" variable. Then add 48 and the "bit" variable
    and store the answer in report.

    myBit var bit
    Mypin var PORTB.6
    Report var byte

    myBit = Mypin
    myBit = !myBit
    Report = 48 + myBit

    This works.
    Regards,
    TABSoft

  5. #5
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    And the answer is....
    Mypin var Portb.6
    Report var byte

    Report = 48 + (Mypin ^ 1)

    There is NO need to be changing variable types. Mypin just needs to be inverted. So why not invert it as if it is a bit. Wow all of this typing for something so simple......
    Dave Purola,
    N8NTA
    EN82fn

Similar Threads

  1. 16F628 PortA pin connections - wierd
    By john meiers in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 10th December 2011, 17:28
  2. Wierd sleep issue
    By orca in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th March 2006, 22:06
  3. Inverting Input Values
    By Peter1960 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st August 2005, 08:09
  4. wierd problem with 12f629 Need Help Despretly
    By Nadeem in forum Off Topic
    Replies: 1
    Last Post: - 15th June 2005, 20:59
  5. Very wierd problem
    By jswayze in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 15th November 2004, 04:56

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