Toggle command
+ Reply to Thread
Results 1 to 14 of 14

Thread: Toggle command

  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

    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

    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

    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

    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

    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

  7. #7

    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?

  8. #8
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    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.

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

    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

  10. #10
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    Default Re: Toggle command

    Quote Originally Posted by Darrel Taylor View Post
    you can just use STR MyArray[10]\10 and it will start adding data at that position
    That is something good to know, but where have I missed this in the docs? Thanks Darrel!
    Dave
    Always wear safety glasses while programming.

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

    Default Re: Toggle command

    Although it doesn't give the exact example I showed above. The concepts are covered in the PBP3 manual section 7.6.
    In particular ... 7.6.2 and 7.6.3.
    DT

  12. #12

    Default Re: Toggle command

    It's good that it works with a constant, but what's the issue with variables, why can't it work with them?
    You can have variables for the Index pointer in arrays in general :?

    Is that going to be added in a future pbp upgrade, or is there some fundemental reason it's difficult/impossible to implement?

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

    Default Re: Toggle command

    Arrays with a variable index are evaluated at run-time to return/assign the value of that array location.

    Arrays with a constant index are evaluated at compile-time, and through "Constant Folding" is reduced to a scaler variable.
    As with any other scaler variable, it can be used as the start of an array.

    With a variable index, there's no way for the compiler to know you wanted the array location, instead of the value at that location.
    The language just doesn't allow for it.
    If the language was changed, it would break everyone's existing programs.
    Last edited by Darrel Taylor; - 16th December 2011 at 02:07.
    DT

  14. #14
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    Default Re: Toggle command

    Once again Darrell, thanks for the lesson!!!
    Dave
    Always wear safety glasses while programming.

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, 20: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: - 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