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,648


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

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

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

  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

    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

  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

    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

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

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

    Glad it worked.

    I have a theory, but will want to test it and look at the disassembled code to see.

    I think it is the fact that the NOT operator (logical or bitwise) against the alias is being evaluated in an assignment statement to a byte.
    This may be having the compiler use a generic macro that is handling the NOT operation as a byte or word output.
    The test code above should force a two-step process.
    1. Force the NOT operation/assignment to a bit variable.
    2. Assign the value + 48 to the Report byte variable.

    My guess is that since "Report = !Bell + 48" has the Report byte variable on the left, PBP might be assuming that the interim steps are at a byte (or word) level.

    We will see, or I could be totally wrong. :-0

    Either way I don't think we should have to burn another variable and cycles to perform what should be a simple function.
    Regards,
    TABSoft

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

    Well, it was as I expected.

    It's all about the assignment statement.

    Whatever "Type" the left side of the assignment statement is drives the operation.
    If you are assigning the value to a Byte, then the compiler does the steps as a byte.
    If you are assigning the value to a BIT, then the compiler does the steps as a bit.

    Here is a simple test program.
    The "Destination" of the value is tested against a Byte variable or a Bit variable.
    The "Source" of the value is tested against a Byte variable, a Port Alias and a Bit Alias.


    Code:
    myBit var bit
    myByte var byte
    
    TRISB = $FF
    
    TestPin var PORTB.6
    TestByte var byte
    TestBit var TestByte.6
    
    
    'Test Variable Alias with direct assignment to byte variable
    TestByte = %01000000
    
    'Assign Logical NOT (!) bit value to byte variable
    myByte = !TestBit
    
    'Assign Bitwise NOT (~) bit value to byte variable
    myByte = ~TestBit
    
    'Test Port Alias with direct assignment to byte variable
    
    'Assign Logical NOT (!) bit value to byte variable
    myByte = !TestPin
    
    'Assign Bitwise NOT (~) bit value to byte variable
    myByte = ~TestPin
    
    'Test Variable Alias with direct assignment to bit variable
    TestByte = %01000000
    
    'Assign Logical NOT (!) bit value to bit variable
    myBit = !TestBit
    
    'Assign Bitwise NOT (~) bit value to bit variable
    myBit = ~TestBit
    
    'Test Port Alias with direct assignment to bit variable
    
    'Assign Logical NOT (!) bit value to byte variable
    myBit = !TestPin
    
    'Assign Bitwise NOT (~) bit value to byte variable
    myBit = ~TestPin

    Here is the PBP assembler listing for the above code.
    Notice that there are (2) macros for each operator type (! or ~)
    LNOT?TB and LNOT?TT (Logical NOT !)
    NOT?TB and NOT?TT (Bitwise NOT ~)

    The TB stands for biT in and Byte out. The input is a Bit type and the Output is a Byte type.
    This is when you get the results that are not what you might expect.

    The TT stands for biT in and biT out. The input is a Bit type and the Output is a Bit type.


    Code:
    ;assembler variables
    _myByte          		EQU	RAM_START + 01Ah
    PB01            		EQU	RAM_START + 01Bh
    _TestByte        		EQU	RAM_START + 01Ch
    
    ;assembler defines
    #define _myBit           	 PB01, 000h
    #define _TestPin         	_PORTB??6
    #define _TestBit         	_TestByte??6
    #define _PORTB??6        	 PORTB, 006h
    #define _TestByte??6     	_TestByte, 006h
    
    
    ; 00022	TestByte = %01000000
    	MOVE?CB	040h, _TestByte
    
    ; 00025	myByte = !TestBit
    	LNOT?TB	_TestBit, _myByte
    
    ; 00028	myByte = ~TestBit
    	NOT?TB	_TestBit, _myByte
    
    ; 00033	myByte = !TestPin
    	LNOT?TB	_TestPin, _myByte
    
    ; 00036	myByte = ~TestPin
    	NOT?TB	_TestPin, _myByte
    
    ; 00039	TestByte = %01000000
    	MOVE?CB	040h, _TestByte
    
    ; 00042	myBit = !TestBit
    	LNOT?TT	_TestBit, _myBit
    
    ; 00045	myBit = ~TestBit
    	NOT?TT	_TestBit, _myBit
    
    ; 00050	myBit = !TestPin
    	LNOT?TT	_TestPin, _myBit
    
    ; 00053	myBit = ~TestPin
    	NOT?TT	_TestPin, _myBit
    Here are the actual macros that are called above.
    I made some comments, not for everything but some.

    Code:
    NOT?TB macro Regin, Bitin, Bout
            CHK?RP  Regin
            movlw   -1  ;load dec 255 into w register
            btfsc   Regin, Bitin    ;If Regin.Bitin = 0, skip the next instruction
            movlw   -2  ;load dec 254 into w register
            MOVE?AB Bout    ;Move W to Bout
        endm
    
    NOT?TT macro Regin, Bitin, Regout, Bitout
            CHK?RP  Regin
            clrw    ;Clear the w register
            btfss   Regin, Bitin    ;If Regin.Bitin = 1, skip the next instruction
            addlw   1   ;Load dec 1 into the w Register
            CHK?RP  Regout
            btfsc   STATUS, Z
            bcf     Regout, Bitout  ;Clear Regout.Bitout
            btfss   STATUS, Z
            bsf     Regout, Bitout  ;Set Regout.Bitout
        endm
    
    LNOT?TB macro Regin, Bitin, Bout
            CHK?RP  Regin
            clrw
            btfss   Regin, Bitin
            movlw   -1
            MOVE?AB Bout
        endm
    
    LNOT?TT macro Regin, Bitin, Regout, Bitout
            CHK?RP  Regin
            clrw
            btfss   Regin, Bitin
            addlw   1
            CHK?RP  Regout
            btfsc   STATUS, Z
            bcf     Regout, Bitout
            btfss   STATUS, Z
            bsf     Regout, Bitout
        endm
    Regards,
    TABSoft

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