Toggle command


Closed Thread
Results 1 to 14 of 14

Thread: Toggle command

Hybrid View

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

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

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

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

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

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Toggle command

    Brilliant forum as usual just the answer I was looking for. I was about to start TOGGLING variable bits !!!

    Which is the smallest code to toogle a bit out of the ones you gave?

    Code:
    Mode.0 = !Mode.0
    Mode.0 = NOT Mode.0
    Mode.0 = Mode.0 ^ 1
    Mode.0[0] = ~Mode.0[0]
    ...
    And an unrelated question about the STR modifier when recieving SERIAL2 data into an array.

    I use SKIP that's great and /STR 10 or whatever and it loads data into my array starting at array index[0]

    Can you make it start at a different point, say array index [10] without overwriting anything in the [0]-[9] positions?

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Toggle command

    Can you make it start at a different point, say array index [10] without overwriting anything in the [0]-[9] positions?
    Not that I know of.
    You could save to data to another array and make bitX of one equal the other...
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default Re: Toggle command

    Quote Originally Posted by retepsnikrep View Post
    Can you make it start at a different point, say array index [10] without overwriting anything in the [0]-[9] positions?
    As long as the offset into the array is always the same, you can just use STR MyArray[10]\10 and it will start adding data at that position without affecting data before or after that range.
    The offset must be a constant. Variables will not work inside the brackets.

    And the smallest code of the previous examples is either ! or NOT, which both compile to the exact same code.
    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, 21:52
  2. Active low input?
    By CosMecc in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 8th August 2010, 21:31
  3. toggle command w/ multiple switches
    By earltyso in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 15th February 2007, 18:47
  4. PIC16F88 problem with TOGGLE command?
    By russman613 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th September 2006, 00:31
  5. Can I do this???
    By noobie in forum mel PIC BASIC
    Replies: 2
    Last Post: - 10th June 2006, 19:57

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