PDA

View Full Version : Method for 4 bit parallel outout?

T Chap
05-26-2006, 01:51 AM
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

or this

if button1 = 1 then A 'sets 4 bit word to 0001

Thanks for any tips

Paul Baker
05-26-2006, 02:08 AM
Sure,
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

T Chap
05-26-2006, 02:20 AM
OK great, just confused as to how to associate OUTA with those specific 4 pins?

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
05-26-2006, 02:33 AM
If you look in the memory and variables section of the Help File or BASIC Stamp Manual it explains how the ports are broken down.ˇ OUTA always refers to P0 through P3, OUTB refers to P4 through P7, etc.ˇ OUTL refers to P0 through P7.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

T Chap
05-26-2006, 02:52 AM
AHA!

Chris Savage
05-26-2006, 04:38 AM
Got it!ˇ Okay, so you see you can output in groups to 1, 4, 8 or 16 pins at the same time.ˇ The pins cannot cross boundries when using nibbles or byte sized ports.ˇ For example you couldn't (easily) output to P6 through P13 at the same time.ˇ Instead you would use P8 through P15.ˇ I hope this helps.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

T Chap
05-26-2006, 05:29 AM
Yes I have gotten the concept sort of. I think you are saying that once you set OUTA for a VAR, you dont want to try to an output arrangement outside the 4 or 8 bit preset words relative to their pin assignments, that makes sense. But can you affect one of the pins individually ie out0 1 and at other times call the outputs by OUTA = %0001 for example?

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
05-26-2006, 07:36 AM
Okay, maybe I have over complicated it for you and led you off the path.ˇ I was trying to be general, not specific.ˇ You can always address any pin or group directly regardless of having assigned it as a VAR.ˇ In fact you couldˇsay:

OUTA = %1001
Temp = INB
OUTD = INC

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

allanlane5
05-26-2006, 08:18 AM
"OutA" is a pre-defined 'variable', which refers to bits 0,1,2, and 3.

It enables you to set all 4 bits at the same time.

T Chap
05-26-2006, 08:29 AM
Yep, I got it now. Thanks!