No announcement yet.

toggle command used on a Varable Bit

  • Filter
  • Time
  • Show
Clear All
new posts

  • toggle command used on a Varable Bit

    was going though some code i need to reuse , and remember notes from darral on how this command can be a problematic when used on a variable bit

    and looked up link shown

    charles can you advise if this is still a possible problem for the current PBP versions , as this info was some time back


    I recently (ok, today) discovered that the PBP TOGGLE command can toggle a variable, as well as output pins (which I used regularly). I just tried a bit var, yep works. Goes 0-1-0-1. Mode var bit Mode_Butn var PortB.1 If mode_Butn = 0 then Toggle Mode LCDout $FE,$C8,"Mode",DEC1 mode

  • #2
    not a good idea , toggle clears bit in var x [banksel var] +1,var x, should lead to very hard to bugs .it can only be user on "port" regs


    • #3
      yep richard i agree , old code used and redouing the chip it (* 7 years back )had not reviewed that prior )


      • #4
        As Richard points out, TOGGLE, HIGH and LOW will attempt to clear the TRIS bit so that the PORT bit is set to an output. When you use the commands on something other than a PORT bit, they will clear another bit in memory that isn't a TRIS bit. This can lead to some entertaining behavior, depending on the function of the bit that got cleared by mistake.

        To toggle bits other than ports, use the bitwise inversion operator (~):

        my_var.0 = ~my_var.0
        Charles Leo
        ME Labs, Inc.


        • #5
          cheers charles , a reminder for those that use the toggle command


          • #6
            having some syntax errors when doing a replacement for the toggle command that was used incorrectly in old code , but its bit wise not (~) or ( ! ) wont compile and i prefer if possible not to use the long method of " If then else " to get the result of toggle

            my_array var byte[10]
            toggle my_array[3] &  $01  ' toggle bit 0 of the array byte 3     - compiles but that not  good as we know
            ~  ( my_array[3] & $01 )     ' bit wise not - wont compile - s\yntax error
            ! ( my_array[3] & $01 )     '  syntax error
            if my_array[3] &  $01 =1 then    ' yes it works but not great when you have a lot of bits in various arrays to toggle
               my_array[3] &  $01 =0
              my_array[3] &  $01 =1


            • #7

              my_array[3]= my_array[3] ^ 1


              • #8
                not seen that before whats it fuction


                • #9
                  bit wise Xor it seems


                  • #10
                    it compiles but it seems like it would change the enitre byte , not the bit 0 only i need to toggle ???


                    • #11
                      nope it inverts only the bits selected in this case bit 0 ,, so if i wanted bit 3 inverted my_array[3]= my_array[3] ^ 8