Method for 4 bit parallel outout?
T Chap
Posts: 4,223
I have a motor controller that uses a 4 bit word to determine one of 16 positions that it will go to. First you send it the address, then the START bit. Each position has its own programmable position, speed, acceleration, etc.
I am brand new to basic, so here are my first thoughts, but surely there is a better way, i.e. one variable or constant for each of the 16 outputs?
Lets use out0 for input A, out1 as B, out2 as C, and out3 as D to produce 0000 output:
input 11
input 12
button1 var in11
button2 var in12
output 0 ' A
output 1 ' B
output 2 ' C
output 3 ' D
output 4 'start bit
A var out0
B var out1
C var out2
D var out3
START var out4
Main:
GOSUB BUT1
GOSUB BUT2
BUT1:
if button1 = 0 then BUT1_0
if button1 = 1 then
d = 0
c = 0
b = 0
a = 1 '0001 position
START = 1 'sends start command for 20ms
Pause 20
START = 0
d = 0 'resets to 0
c = 0 'resets to 0
b = 0 'resets to 0
a = 0 'resets to 0
BUT1_0:
RETURN
BUT2:
if button2 = 0 then BUT2_0
if button2 = 1 then
d = 0
c = 0
b = 1 '0010 position
a = 0
START = 1
pause20
START = 0
d = 0 'resets to 0
c = 0 'resets to 0
b = 0 'resets to 0
a = 0 'resets to 0
BUT2_0:
RETURN
goto main
The better way would be like this:
if button1 = 1 then
ADDRESS = 0001
or this
if button1 = 1 then A 'sets 4 bit word to 0001
Thanks for any tips
I am brand new to basic, so here are my first thoughts, but surely there is a better way, i.e. one variable or constant for each of the 16 outputs?
Lets use out0 for input A, out1 as B, out2 as C, and out3 as D to produce 0000 output:
input 11
input 12
button1 var in11
button2 var in12
output 0 ' A
output 1 ' B
output 2 ' C
output 3 ' D
output 4 'start bit
A var out0
B var out1
C var out2
D var out3
START var out4
Main:
GOSUB BUT1
GOSUB BUT2
BUT1:
if button1 = 0 then BUT1_0
if button1 = 1 then
d = 0
c = 0
b = 0
a = 1 '0001 position
START = 1 'sends start command for 20ms
Pause 20
START = 0
d = 0 'resets to 0
c = 0 'resets to 0
b = 0 'resets to 0
a = 0 'resets to 0
BUT1_0:
RETURN
BUT2:
if button2 = 0 then BUT2_0
if button2 = 1 then
d = 0
c = 0
b = 1 '0010 position
a = 0
START = 1
pause20
START = 0
d = 0 'resets to 0
c = 0 'resets to 0
b = 0 'resets to 0
a = 0 'resets to 0
BUT2_0:
RETURN
goto main
The better way would be like this:
if button1 = 1 then
ADDRESS = 0001
or this
if button1 = 1 then A 'sets 4 bit word to 0001
Thanks for any tips
Comments
use OUTA which are the four output pins out0, out1, out2 and out3. So something like
MCTL VAR OUTA
then you can set the value directly like:
MCTL = %0000
MCTL = %0010
etc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1+1=10
Is there a method to asign OUTA resectively to pins 0, 1, 2, 3 for MCTL to then apply the %0001 to derive this example of 0001 where :
pin 0 = 0
pin 1 = 0
pin 2 = 0
pin 3 = 1?
Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Just curious if there is a speed advantage to this method? Obviously there are less lines to read, but for the chip to change the outs with outa is that faster as well? It would seem so.
Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
It enables you to set all 4 bits at the same time.