Wierd result when inverting an input


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    May 2008
    Location
    Italy
    Posts
    825

    Default Wierd result when inverting an input

    Bell var portB.0
    Report var byte

    (PortB.0 is pulled up)

    Report = !Bell + 48 or Report = (!Bell) + 48

    The value of variable Report = "0" (ascii 48) when portB.0 = True
    The value of variable Report = "/" (ascii 47) when portB.0 = False (I was expecting ascii 49)

    Is this a bug in PBP3 or I am using wrongly the not notation?

    Cheers

    Al
    All progress began with an idea

  2. #2
    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

    Al, I believe you have to use the BITWISE operator of "~" for that operation.
    Dave Purola,
    N8NTA
    EN82fn

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    Thank you Dave for the suggestion, but no luck it doesn't work either. With "~" I get ascii 46 when portB.0 is false and the same ascii 47 when the portB.0 is True.

    Looking into the manual the character for the NOT is "!", I could not find any reference to "~" as a NOT notation.

    Cheers

    Al.
    All progress began with an idea

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    Quote Originally Posted by aratti View Post
    Thank you Dave for the suggestion, but no luck it doesn't work either. With "~" I get ascii 46 when portB.0 is false and the same ascii 47 when the portB.0 is True.

    Looking into the manual the character for the NOT is "!", I could not find any reference to "~" as a NOT notation.

    Cheers

    Al.
    The "tilde" character ~ is a BITWISE NOT. pp35 in the old green book, have misplaced my PBP3 book.

    This works:
    Code:
    
    Bell var portB.6
    Report var byte
    main:
    Report = ~~(Bell + 48) 
    debug report
    pause 500
    
    goto main
    Last edited by Archangel; - 19th June 2015 at 18:02.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    Code:
    Bell var portB.6
    Report var byte
    main:
    Report = ~~(Bell + 48) 
    debug report
    pause 500
    
    goto main
    Thank you Archangel for your snippet.
    I tried with one tilde character ~(Bell + 48) and it does't work. (Just strange characters)
    I tried with two tildes, as per your example, and what I get is the original Logic state of the pin. It seems that two tildes are simply ignored.

    So, at the moment I have used the IF/THEN/ELSE/ENDIF workaround. Conclusion we cannot invert a pin alias and convert it to ascii using the "!" or the bitwise "~"

    Cheers
    Al.
    Last edited by aratti; - 19th June 2015 at 18:57.
    All progress began with an idea

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    It seems that two tildes are simply ignored.
    not at all !

    PORTB = ~ PORTB ' Invert every bit in PORTB
    result = ~ (PORTB & %00001111)
    'Invert the result of the & operation
    manual $ 3.2.4 ...

    if you invert twice ... you get the original value !!!

    did you try ...
    Code:
    Report = (~ Bell ) + 48 ...
    probably not !
    Alain
    Last edited by Acetronics2; - 19th June 2015 at 19:51.
    ************************************************** ***********************
    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 " !!!
    *****************************************

  7. #7
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    Alain, the result of your example : Report = (~ Bell ) + 48 gives 46 when input is high and 47 when the input is low.

    Cheers

    Al.
    All progress began with an idea

  8. #8
    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

    Al, It makes me wonder if the TRIS register is set correctly for the port? You always get the same response no mater what state you place on it.
    Dave Purola,
    N8NTA
    EN82fn

  9. #9
    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

    Just thinking here....

    What you might be seeing is that in a single statement "Report = (~Bell) + 48", is that PBP is treating the combination of the alias and the assignment to "Report" as a byte output computation.

    If that is the case, then the output is as expected.
    Byte variables are unsigned, so a bitwise not of 0 = 255 and 1 = 254.
    Which would give you the 47 and 46 values you are seeing. 255+48=47, 254+48=46

    This is most likely also happening for the Logical Not as well.

    To test:

    Bell var PORTB.6
    Report var byte
    MyPin var bit

    MyPin = Bell
    MyPin = ~MyPin 'this will force the bitwise Not at the bit variable level
    Report = MyPin + 48

    MyPin = Bell
    MyPin = !Mypin 'This will force the logical Not at the bit variable level
    Report = MyPin + 48

    Output the two operations and see what you get then.
    Regards,
    TABSoft

  10. #10
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    Dave, the tris register is set correctly and the pic18f2620 just do what it should do when the inputs change Logic state. I need to invert the Logic state of the display (it is much more simple to have a "True" when the external sensor is close instead having a "False".
    As I said in a previous post, I solved the problem using the following workaround:
    Code:
    If Bell = false then
    Report = "1"
    Else
    Report = "0"
    ENDIF
    Definetly less elegant than the "NOT" notation but at least it works!

    Since I always toggle the flashing led with led = !led and it works fine I wrongly assumed that it could work also with an alias. Very likely I should copy the pin state in a bit variable to have it working. I will give a tray (NOT NOW)

    Cheers
    Al.
    All progress began with an idea

  11. #11
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    Code:
    MyPin = Bell
    MyPin = !Mypin 'This will force the logical Not at the bit variable level
    Report = MyPin + 48
    Thank you Tabsoft for the above snippet. Tested and it works. I have already replaced the IF/THEN/ELSE code that I could not stand with.

    Now that a more elegant workaround has been found, the question remains: PBP3 cannot handle pin alias. Is it a bug?

    Cheers

    Al
    All progress began with an idea

  12. #12
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: Wierd result when inverting an input

    Hi, Al

    I played somewhat with that and end with ...

    Code:
    Report = 47 - (~Bell)
    found funny things like ~N = 65535 - N ...

    Have a good night
    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 " !!!
    *****************************************

  13. #13
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    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 " !!!
    *****************************************

  14. #14
    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

  15. #15
    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?

  16. #16
    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

  17. #17
    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 : 1

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