Toggle command


Closed Thread
Results 1 to 14 of 14

Thread: Toggle command

Hybrid View

  1. #1
    Join Date
    Nov 2008
    Posts
    96

    Default Toggle command

    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.

    Code:
    Mode var bit
    Mode_Butn var PortB.1
    
    If mode_Butn = 0 then
            Toggle Mode
            LCDout $FE,$C8,"Mode",DEC1 mode
            Pause 500
        Endif
    OK, the PBP manuals description is:
    TOGGLE Pin

    Invert the state of the specified Pin. Pin is automatically made an output. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

    So what does the 'variable that contains a number 0-15 (e.g. B0)' mean ?

    Does that mean that you can toggle any bit in a word, or just a number up to #15, or 0-15 variables ?

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    TOGGLE should ONLY be used on PIN's!

    Yes, it does toggle the bit of any Register.Bit combination that you pass to it.
    What's not so obvious is that it also attempts to set the TRIS register associated with that PIN.

    On a 16F, it adds 80h to the specified PORT's register.
    On an 18F, it adds 12h, and in both cases it clears the matching bit to insure the PIN is set to output.
    It doesn't care if it's not really a TRIS register. It clears it anyway.

    If you aren't actually toggling a PIN, the results can be RANDOM because it's overwriting BIT's in other variables.

    A better method of toggling a BIT in any register is to use the Bitwise NOT operator (~) or the Logical NOT (! or NOT).

    So what does the 'variable that contains a number 0-15 (e.g. B0)' mean ?
    The PIN numbers are a Basic Stamp compatibility issue.
    On some PIC's 0 = PORTB.0, on other PIC's it's PORTA.0, on 12F's It's GPIO.0.
    <br>
    DT

  3. #3
    Join Date
    Nov 2008
    Posts
    96


    Did you find this post helpful? Yes | No

    Default

    Hi again Darrel,

    I think we crossed wires somewhere. I'm not toggling a port register, I toggled one bit inside a ram register, assigned by PBP (Mode var Bit). The 0-1-0-1 was the bit variable result displayed via the LCD routine, nothing to do with a port at all other than I use a pin to make the bit value toggle once...

    Does that change your response to toggling a RAM variable ?

    Anyway. If you don't use Toggle to swap a ram 'bit variable' value from 0 to 1 and vice versa, how would you do it in minimal code ?
    Martin

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mr.sneezy View Post
    I think we crossed wires somewhere. I'm not toggling a port register, I toggled one bit inside a ram register, assigned by PBP (Mode var Bit) ...

    Does that change your response to toggling a RAM variable ?
    There's probably some wires crossed, but I'd place a large bet that it's not on this end.

    That's exactly what I'm talking about.

    If you use TOGGLE on any BIT other than a PIN, you will be inadvertently clearing a BIT in another RAM location. The results will be unpredictable.
    DT

  5. #5
    Join Date
    Nov 2008
    Posts
    96


    Did you find this post helpful? Yes | No

    Default

    I see. Yes, crossed at my end (as always). Interesting. In my application it's working well and seems to be stable thus far... Lucky I guess.

    I should change it. So you use this, if Mode is a bit var ?

    Mode = Mode ^ %00000001 ' Reverse state of bit var Mode

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Luck always runs out eventually. The more variables you have in the program ... the more likely to see the problem.

    Quote Originally Posted by mr.sneezy View Post
    Mode = Mode ^ %00000001 ' Reverse state of bit var Mode
    That's valid!

    So is ...
    Code:
    Mode.0 = !Mode.0
    Mode.0 = NOT Mode.0
    Mode.0 = Mode.0 ^ 1
    Mode.0[0] = ~Mode.0[0]
    ...
    Anything but TOGGLE, unless it's a PIN.
    <br>
    DT

Similar Threads

  1. My code for TV remote and MIBAM for RGB control
    By idtat in forum Code Examples
    Replies: 4
    Last Post: - 12th January 2013, 20:52
  2. Active low input?
    By CosMecc in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 8th August 2010, 20:31
  3. toggle command w/ multiple switches
    By earltyso in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 15th February 2007, 17:47
  4. PIC16F88 problem with TOGGLE command?
    By russman613 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th September 2006, 23:31
  5. Can I do this???
    By noobie in forum mel PIC BASIC
    Replies: 2
    Last Post: - 10th June 2006, 18:57

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