Syntax in Spin?
cavelamb
Posts: 720
in Propeller 1
Hi guys,
I've been playing with Spin again and trying to figure out how to form this expression..
I'd like to use a variable to indicate a range of pins -
as in outa [8..11] := Mot_RoR
so as outa[motorpins] := MotRoR
The individual pins named as below ... MotA1, MotA2, MotB1, MotB2.
But how to express all four at once?
Richard
I've been playing with Spin again and trying to figure out how to form this expression..
I'd like to use a variable to indicate a range of pins -
as in outa [8..11] := Mot_RoR
so as outa[motorpins] := MotRoR
The individual pins named as below ... MotA1, MotA2, MotB1, MotB2.
But how to express all four at once?
Richard
' Pin Definitions:
' LSB MSB
' 000000000 01111111 11122222 2222233
' 012345678 90123456 78901234 5678901
PADpins = %11111111_00000000_00000000_00000000 ' 0..7 Touchpads
MOTPins = %00000000_11110000_00000000_00000000 ' 8..11 Motor Control
PWMpins = %00000000_00001100_00000000_00000000 ' 12..13 Motor PWM
LCDpins = %00000000_00000010_00000000_00000000 ' 14 LCD Display
IRCpins = %00000000_00000001_00000000_00000000 ' 15 Infra Red Controller
LEDpins = %00000000_00000000_11111111_00000000 ' 16..23 QS LEDs
OBXpins = %00000000_00000000_00000000_11110000 ' 24..27 Obsticle sensors
EPCpins = %00000000_00000000_00000000_00001000 ' 28 EEPROM Clock
EPDpins = %00000000_00000000_00000000_00000100 ' 29 EEPROM Data
USBpins = %00000000_00000000_00000000_00000011 ' 30..31 USB
OUTpins = MOTpins| PWMpins| LEDpins| LCDpins
'======================================
' Motor control pins for TB6612FNG
MotA1 = 8
MotA2 = 9
MotB1 = 10
MotB2 = 11
PWMA = 12
PWMB = 13
' Motor Direction Masks for TB6612FNG
Mot_Fwd = %0110 ' Go fwd
Mot_Aft = %1001 ' Go Aft
Mot_Rit = %0010 ' Turn Right
Mot_Lft = %0100 ' Turn Left
Mot_RoR = %1010 ' Rotate Right
Mot_RoL = %0101 ' Rotate Left
Mot_Off = %0000 ' All Stop
Comments
Cheers
Is it possible to set a pattern for them (that is not contiguous)?
%00000000_00000000_00000000_11111111
-Phil
replacing Mot_Fwd with whichever other constant...essentially affects the entire outa register
See attached serial demo for 'illustration' (just spits out numbers, doesn't affect the outa register)
EDIT:
Regarding what Phil said, the '20' would need to be adjusted to the least significant motor pin, if your pin constants are indeed backwards
I had them the way you show but couldn't get it working until I turned the whole mess around.
No I wonder if I'm running backwards???
Why does this work???
I'll study it next.
I've been trying to un-confuse myself, but it just seems to get worse...
Program is set up to run on a QuickStart with a two-line LCD display for cross check
and an IR remote control decoder for input.
Just walking through the numbers (1 to 7) for LED.
This test was cobbled together to try to get to the bottom of it.
LED1 through LED8 are intended to be bit patterns for the LEDs.
That seems to work as expected - when setting the directions via dira[16..23] := %11111111
But setting directions via dira[LEDpins] := LEDpins (commented out below) doesn't work
with the pin mask going either way...
LEDpins = %00000000_00000000_11111111_00000000 ' 16..23 QS LEDs
' LEDpins = %00000000_11111111_00000000_00000000 ' 16..23 QS LEDs
So something between me, the compiler and the Propeller is badly disconnected!
Simple remote control software so far, but
I'd like to better understand why I don't
understand some of this mess...
https://youtube.com/watch?v=OsBPsCHbUa8
LEDpins = %00000000_00000000_11111111_00000000 ' 16..23 QS LEDs
' LEDpins = %00000000_11111111_00000000_00000000 ' 16..23 QS LEDs
dira[LEDpins] := LEDpins ' make LED pins outputs - that's all 32 bits
My question really is - Which end is which?
verses
dira[16..23] := %11111111 ' make LED pins outputs
But that won't help out-of-sequence pins.
OR with the contents of PIN register?
Then AND with mask to be set?
I need to go back to the documentation.
As in KISS...
VAR byte ledstate
Ledstate := %00000000 ‘or as needed
dira[16..23] := %11111111
Outa[16..23] := ledstate
OUTA[pin] accesses one single pin, with the number in brackets.
OUTA[Highpin..Lowpin] accesses a contiguous range of pins. Always write the higher pin first in the brackets, otherwise the bits get written in reverse order. If you have a range of 8 bits (ie. 23..16), the lower 8 bits of the value (7..0) gets written to that bit range.
The bracket syntax does not allow to write to a range of pins in random order (non contiguous).
I do believe you figured it out for me.
I was writing Low bit to high bit... outa [16..23] := backwards_bits
Reverse order...
Which worked mo betta with the mask backwards?
I'll see if I can straighten that out (and still have running code).
Gracias,
PS:
Would you show me a syntax example of no brackets?
Outa pinmask := bit mask ???
Richardo
Here some snippets with that use DIRA and OUTA direct as 32bit registers:
Andy
Lovely examples of addressing with and, or and shifting.
Thank you!
Richard
Cross check.
I've never had much luck with the terminal function.
I like getting it straight from the Prop.