PDA

View Full Version : Toggle command



mr.sneezy
- 19th February 2009, 02:07
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
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 ?

Darrel Taylor
- 19th February 2009, 04:10
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>

mr.sneezy
- 19th February 2009, 05:01
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

Darrel Taylor
- 19th February 2009, 05:25
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. :eek:

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.

mr.sneezy
- 19th February 2009, 05:58
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

Darrel Taylor
- 19th February 2009, 06:15
Luck always runs out eventually. The more variables you have in the program ... the more likely to see the problem.


Mode = Mode ^ %00000001 ' Reverse state of bit var Mode
That's valid!

So is ...

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>

retepsnikrep
- 14th December 2011, 20:18
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?



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?

mackrackit
- 15th December 2011, 00:06
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...

Darrel Taylor
- 15th December 2011, 03:30
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.

mackrackit
- 15th December 2011, 12:26
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!

Darrel Taylor
- 15th December 2011, 15:42
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.

retepsnikrep
- 15th December 2011, 21:06
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?

Darrel Taylor
- 16th December 2011, 00:57
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.

mackrackit
- 16th December 2011, 02:07
Once again Darrell, thanks for the lesson!!!